aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorAndrew Chow <github@achow101.com>2023-05-15 15:50:35 -0400
committerAndrew Chow <github@achow101.com>2023-05-31 15:17:05 -0400
commit84b2f353bbefb9264284e7430863b2fa1d796d38 (patch)
tree31fb3a1fec636bbe336710961261e3b31e2623e2 /src
parent71300489af362c3fed4736de6bffab4d758b6a84 (diff)
downloadbitcoin-84b2f353bbefb9264284e7430863b2fa1d796d38.tar.xz
walletdb: Consistently clear key and value streams before writing
Before writing data to the output key and value streams, make sure they are cleared.
Diffstat (limited to 'src')
-rw-r--r--src/wallet/bdb.cpp1
-rw-r--r--src/wallet/sqlite.cpp4
-rw-r--r--src/wallet/test/util.cpp3
3 files changed, 8 insertions, 0 deletions
diff --git a/src/wallet/bdb.cpp b/src/wallet/bdb.cpp
index 6dce51fc12..dbd80718f6 100644
--- a/src/wallet/bdb.cpp
+++ b/src/wallet/bdb.cpp
@@ -777,6 +777,7 @@ bool BerkeleyBatch::ReadKey(DataStream&& key, DataStream& value)
SafeDbt datValue;
int ret = pdb->get(activeTxn, datKey, datValue, 0);
if (ret == 0 && datValue.get_data() != nullptr) {
+ value.clear();
value.write({AsBytePtr(datValue.get_data()), datValue.get_size()});
return true;
}
diff --git a/src/wallet/sqlite.cpp b/src/wallet/sqlite.cpp
index 77e8a4e9c1..7621f2476b 100644
--- a/src/wallet/sqlite.cpp
+++ b/src/wallet/sqlite.cpp
@@ -409,6 +409,7 @@ bool SQLiteBatch::ReadKey(DataStream&& key, DataStream& value)
// Leftmost column in result is index 0
const std::byte* data{AsBytePtr(sqlite3_column_blob(m_read_stmt, 0))};
size_t data_size(sqlite3_column_bytes(m_read_stmt, 0));
+ value.clear();
value.write({data, data_size});
sqlite3_clear_bindings(m_read_stmt);
@@ -495,6 +496,9 @@ DatabaseCursor::Status SQLiteCursor::Next(DataStream& key, DataStream& value)
return Status::FAIL;
}
+ key.clear();
+ value.clear();
+
// Leftmost column in result is index 0
const std::byte* key_data{AsBytePtr(sqlite3_column_blob(m_cursor_stmt, 0))};
size_t key_data_size(sqlite3_column_bytes(m_cursor_stmt, 0));
diff --git a/src/wallet/test/util.cpp b/src/wallet/test/util.cpp
index eacb70cd69..4fa7adc37d 100644
--- a/src/wallet/test/util.cpp
+++ b/src/wallet/test/util.cpp
@@ -100,6 +100,8 @@ DatabaseCursor::Status MockableCursor::Next(DataStream& key, DataStream& value)
if (m_cursor == m_cursor_end) {
return Status::DONE;
}
+ key.clear();
+ value.clear();
const auto& [key_data, value_data] = *m_cursor;
key.write(key_data);
value.write(value_data);
@@ -117,6 +119,7 @@ bool MockableBatch::ReadKey(DataStream&& key, DataStream& value)
if (it == m_records.end()) {
return false;
}
+ value.clear();
value.write(it->second);
return true;
}