Skip to content

Commit 4e7b5c0

Browse files
committed
wallet: bdb, fix ErasePrefix read-write operation error
1 parent 757d651 commit 4e7b5c0

File tree

1 file changed

+14
-2
lines changed

1 file changed

+14
-2
lines changed

src/wallet/bdb.cpp

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -897,15 +897,27 @@ bool BerkeleyBatch::ErasePrefix(Span<const std::byte> prefix)
897897
// because we are not using the DB_DBT_USERMEM flag, so BDB will allocate
898898
// and return a different output data pointer
899899
Dbt prefix_key{const_cast<std::byte*>(prefix.data()), static_cast<uint32_t>(prefix.size())}, prefix_value{};
900+
// First, execute the read operations
900901
int ret{cursor->dbc()->get(&prefix_key, &prefix_value, DB_SET_RANGE)};
902+
std::vector<DataStream> to_remove;
901903
for (int flag{DB_CURRENT}; ret == 0; flag = DB_NEXT) {
902904
SafeDbt key, value;
903905
ret = cursor->dbc()->get(key, value, flag);
904906
if (ret != 0 || key.get_size() < prefix.size() || memcmp(key.get_data(), prefix.data(), prefix.size()) != 0) break;
905-
ret = cursor->dbc()->del(0);
907+
908+
Span<const std::byte> raw_key = SpanFromDbt(key);
909+
DataStream stream;
910+
stream.write(raw_key);
911+
to_remove.emplace_back(stream);
906912
}
907913
cursor.reset();
908-
return ret == 0 || ret == DB_NOTFOUND;
914+
if (ret != 0 && ret != DB_NOTFOUND) return false;
915+
916+
// Secondly, execute the write operations
917+
for (DataStream& key : to_remove) {
918+
if (!EraseKey(std::move(key))) return false;
919+
}
920+
return true;
909921
}
910922

911923
void BerkeleyDatabase::AddRef()

0 commit comments

Comments
 (0)