Skip to content

Commit 84b2f35

Browse files
committed
walletdb: Consistently clear key and value streams before writing
Before writing data to the output key and value streams, make sure they are cleared.
1 parent 7130048 commit 84b2f35

File tree

3 files changed

+8
-0
lines changed

3 files changed

+8
-0
lines changed

src/wallet/bdb.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -777,6 +777,7 @@ bool BerkeleyBatch::ReadKey(DataStream&& key, DataStream& value)
777777
SafeDbt datValue;
778778
int ret = pdb->get(activeTxn, datKey, datValue, 0);
779779
if (ret == 0 && datValue.get_data() != nullptr) {
780+
value.clear();
780781
value.write({AsBytePtr(datValue.get_data()), datValue.get_size()});
781782
return true;
782783
}

src/wallet/sqlite.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -409,6 +409,7 @@ bool SQLiteBatch::ReadKey(DataStream&& key, DataStream& value)
409409
// Leftmost column in result is index 0
410410
const std::byte* data{AsBytePtr(sqlite3_column_blob(m_read_stmt, 0))};
411411
size_t data_size(sqlite3_column_bytes(m_read_stmt, 0));
412+
value.clear();
412413
value.write({data, data_size});
413414

414415
sqlite3_clear_bindings(m_read_stmt);
@@ -495,6 +496,9 @@ DatabaseCursor::Status SQLiteCursor::Next(DataStream& key, DataStream& value)
495496
return Status::FAIL;
496497
}
497498

499+
key.clear();
500+
value.clear();
501+
498502
// Leftmost column in result is index 0
499503
const std::byte* key_data{AsBytePtr(sqlite3_column_blob(m_cursor_stmt, 0))};
500504
size_t key_data_size(sqlite3_column_bytes(m_cursor_stmt, 0));

src/wallet/test/util.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,8 @@ DatabaseCursor::Status MockableCursor::Next(DataStream& key, DataStream& value)
100100
if (m_cursor == m_cursor_end) {
101101
return Status::DONE;
102102
}
103+
key.clear();
104+
value.clear();
103105
const auto& [key_data, value_data] = *m_cursor;
104106
key.write(key_data);
105107
value.write(value_data);
@@ -117,6 +119,7 @@ bool MockableBatch::ReadKey(DataStream&& key, DataStream& value)
117119
if (it == m_records.end()) {
118120
return false;
119121
}
122+
value.clear();
120123
value.write(it->second);
121124
return true;
122125
}

0 commit comments

Comments
 (0)