diff --git a/qa/rpc-tests/walletbackup.py b/qa/rpc-tests/walletbackup.py --- a/qa/rpc-tests/walletbackup.py +++ b/qa/rpc-tests/walletbackup.py @@ -199,6 +199,21 @@ assert_equal(self.nodes[1].getbalance(), balance1) assert_equal(self.nodes[2].getbalance(), balance2) + # backup to file self must fail + try: + self.nodes[0].backupwallet(tmpdir + + "/node0/regtest/wallet.dat") + raise AssertionError("Must not backup to file itself!") + except JSONRPCException as e: + assert("backup failed" in e.error['message']) + + # different route to same path also fails + try: + self.nodes[0].backupwallet(tmpdir + + "/node0/./regtest/wallet.dat") + raise AssertionError("Must not backup to file itself!") + except JSONRPCException as e: + assert("backup failed" in e.error['message']) if __name__ == '__main__': WalletBackupTest().main() diff --git a/src/wallet/wallet.cpp b/src/wallet/wallet.cpp --- a/src/wallet/wallet.cpp +++ b/src/wallet/wallet.cpp @@ -4398,6 +4398,13 @@ pathDest /= strWalletFile; } + if (boost::filesystem::equivalent(pathSrc, pathDest)) { + LogPrintf("cannot backup to wallet source file %s\n", + pathDest.string()); + return false; + + } + try { #if BOOST_VERSION >= 104000 boost::filesystem::copy_file(