Browse Source

Ensure backupwallet fails when attempting to backup to source file

Previous behaviour was to destroy the wallet (to zero-length)

Github-Pull: #11376
Rebased-From: 5d465e3962
tags/v0.15.1
Tomas van der Wansem 3 years ago
parent
commit
fd79ed6b20
3 changed files with 16 additions and 1 deletions
  1. 5
    0
      src/wallet/db.cpp
  2. 1
    1
      src/wallet/db.h
  3. 10
    0
      test/functional/walletbackup.py

+ 5
- 0
src/wallet/db.cpp View File

@@ -673,6 +673,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;

+ 1
- 1
src/wallet/db.h View File

@@ -45,7 +45,7 @@ public:
void Reset();

void MakeMock();
bool IsMock() { return fMockDb; }
bool IsMock() const { return fMockDb; }

/**
* Verify that database file strFile is OK. If it is not,

+ 10
- 0
test/functional/walletbackup.py View File

@@ -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()

Loading…
Cancel
Save