From 5d465e396249a0e2cc60b16984a2bdbe4c8993c3 Mon Sep 17 00:00:00 2001 From: Tomas van der Wansem Date: Thu, 21 Sep 2017 00:10:46 +0200 Subject: [PATCH] Ensure backupwallet fails when attempting to backup to source file Previous behaviour was to destroy the wallet (to zero-length) --- src/wallet/db.cpp | 5 +++++ test/functional/walletbackup.py | 10 ++++++++++ 2 files changed, 15 insertions(+) diff --git a/src/wallet/db.cpp b/src/wallet/db.cpp index d66ba4842..d49cd8234 100644 --- a/src/wallet/db.cpp +++ b/src/wallet/db.cpp @@ -672,6 +672,11 @@ bool CWalletDBWrapper::Backup(const std::string& strDest) pathDest /= strFile; try { + if (fs::equivalent(pathSrc, pathDest)) { + LogPrintf("cannot backup to wallet source file %s\n", pathDest.string()); + return false; + } + fs::copy_file(pathSrc, pathDest, fs::copy_option::overwrite_if_exists); LogPrintf("copied %s to %s\n", strFile, pathDest.string()); return true; diff --git a/test/functional/walletbackup.py b/test/functional/walletbackup.py index 15ea26afa..85a149793 100755 --- a/test/functional/walletbackup.py +++ b/test/functional/walletbackup.py @@ -190,6 +190,16 @@ class WalletBackupTest(BitcoinTestFramework): assert_equal(self.nodes[1].getbalance(), balance1) assert_equal(self.nodes[2].getbalance(), balance2) + # Backup to source wallet file must fail + sourcePaths = [ + tmpdir + "/node0/regtest/wallet.dat", + tmpdir + "/node0/./regtest/wallet.dat", + tmpdir + "/node0/regtest/", + tmpdir + "/node0/regtest"] + + for sourcePath in sourcePaths: + assert_raises_rpc_error(-4, "backup failed", self.nodes[0].backupwallet, sourcePath) + if __name__ == '__main__': WalletBackupTest().main()