cryptonote_basic: fix add_extra_nonce_to_tx_extra() length

Missing unit tests.
This commit is contained in:
jeffro256
2026-02-24 17:39:20 -06:00
parent dbcc7d212c
commit 1e6a55109f
2 changed files with 18 additions and 4 deletions
+10
View File
@@ -125,4 +125,14 @@ namespace tools {
int read_varint(InputIt &&first, InputIt &&last, T &i) { int read_varint(InputIt &&first, InputIt &&last, T &i) {
return read_varint<std::numeric_limits<T>::digits>(std::forward<InputIt>(first), std::forward<InputIt>(last), i); return read_varint<std::numeric_limits<T>::digits>(std::forward<InputIt>(first), std::forward<InputIt>(last), i);
} }
template <typename T, typename = std::enable_if_t<std::is_integral<T>::value && std::is_unsigned<T>::value>>
constexpr std::size_t get_varint_byte_size(T val) {
std::size_t bytes = 0;
do {
++bytes;
val >>= 7;
} while (val);
return bytes;
}
} }
@@ -741,15 +741,19 @@ namespace cryptonote
{ {
CHECK_AND_ASSERT_MES(extra_nonce.size() <= TX_EXTRA_NONCE_MAX_COUNT, false, "extra nonce could be 255 bytes max"); CHECK_AND_ASSERT_MES(extra_nonce.size() <= TX_EXTRA_NONCE_MAX_COUNT, false, "extra nonce could be 255 bytes max");
size_t start_pos = tx_extra.size(); size_t start_pos = tx_extra.size();
tx_extra.resize(tx_extra.size() + 2 + extra_nonce.size()); const std::size_t len_varint_bytes = tools::get_varint_byte_size(extra_nonce.size());
tx_extra.resize(tx_extra.size() + 1 + len_varint_bytes + extra_nonce.size());
//write tag //write tag
tx_extra[start_pos] = TX_EXTRA_NONCE; tx_extra[start_pos] = TX_EXTRA_NONCE;
//write len //write len
++start_pos; ++start_pos;
tx_extra[start_pos] = static_cast<uint8_t>(extra_nonce.size()); unsigned char * vp = tx_extra.data() + start_pos;
tools::write_varint(vp, extra_nonce.size());
assert(vp == tx_extra.data() + tx_extra.size() - extra_nonce.size());
//write data //write data
++start_pos; start_pos += len_varint_bytes;
memcpy(&tx_extra[start_pos], extra_nonce.data(), extra_nonce.size()); if (!extra_nonce.empty())
memcpy(&tx_extra[start_pos], extra_nonce.data(), extra_nonce.size());
return true; return true;
} }
//--------------------------------------------------------------- //---------------------------------------------------------------