From 883d8c5b51a948487a7d62be33ee2aa45c9e4899 Mon Sep 17 00:00:00 2001 From: Cheng Date: Fri, 29 Sep 2023 08:44:49 +0000 Subject: [PATCH] Found the sanity test bug. My hashing code incorrectly ignored the trailing null in std::strings. (but not in c strings) --- src/display_wallet.cpp | 4 ++-- src/serialization.h | 12 +++++++++++- src/unit_test.cpp | 9 ++++++--- 3 files changed, 19 insertions(+), 6 deletions(-) diff --git a/src/display_wallet.cpp b/src/display_wallet.cpp index b10a09b..9c52a99 100644 --- a/src/display_wallet.cpp +++ b/src/display_wallet.cpp @@ -27,11 +27,11 @@ display_wallet::display_wallet(wxWindow* parent, wxFileName& walletfile) : SetSizer(sizer); try { ro::sql read_names_and_keys(m_db, - R"|(SELECT Names.name AS name, Keys.pubkey AS pubkey FROM Names INNER JOIN Keys ON Names.ROWID=Keys.id AND Keys.use=1 ORDER BY LOWER(name), name COLLATE NOCASE;)|"); + R"|(SELECT Names.name AS name, Keys.pubkey AS pubkey FROM Names INNER JOIN Keys ON Names.ROWID=Keys.id AND Keys.use=1 ORDER BY LOWER(name), name COLLATE BINARY;)|"); while (read_names_and_keys.step() == Icompiled_sql::ROW) { std::string name = read_names_and_keys.column(0); auto pubkey = *read_names_and_keys.column(1); -// if (m_MasterSecret(name).timesBase() != pubkey)throw MyException(std::string(sz_public_key_of) + name + sz_fails_to_correspond); + if (m_MasterSecret(name).timesBase() != pubkey)throw MyException(std::string(sz_public_key_of) + name + sz_fails_to_correspond); m_lSizer->Add( new wxStaticText( this, diff --git a/src/serialization.h b/src/serialization.h index a06cb6e..c35a143 100644 --- a/src/serialization.h +++ b/src/serialization.h @@ -65,7 +65,17 @@ sizeof(std::declval()[0]) == 1, std::span > serialize(const T& a) { - return std::span(static_cast(static_cast(&a[0])), std::size(a)); + int l; + const void* pt; + if constexpr (std::is_same_v, std::string>) { + l = a.length() + 1; + pt = a.c_str(); + } + else { + l = std::size(a); + pt = &a[0]; + } + return std::span(static_cast(pt), l); } // Compile time test to see if a type has a blob array member diff --git a/src/unit_test.cpp b/src/unit_test.cpp index 8387083..47403d5 100644 --- a/src/unit_test.cpp +++ b/src/unit_test.cpp @@ -239,9 +239,12 @@ static bool checkDataConversionsProduceExpected(void){ std::string str_pt_a = &(base58(pt_a))[0]; assert(base58::bin(str_pt_a.c_str()) == pt_a); hash<256> hash_b{ hash_a,str_hash_a,scl_a,str_sclr_a,pt_a,str_pt_a,33, 66ull }; - if (base58(hash_b).operator std::string() != - "i22EVNPsKRjdxYTZrPPu9mx6vnrBjosFix5F4gn2mb2kF" - ){ throw MyException("unexpected hash of transformations", __LINE__, __func__, SrcFilename); + auto str_b = base58(hash_b).operator std::string(); + if (str_b != "7cTScjKyUtmbvc28BV3ok51szgrQmaBa2YE5HPBcukC9e" + ) { + throw MyException( + std::format("unexpected hash of transformations: hash256#{}", str_b), + __LINE__, __func__, SrcFilename); } } catch (const MyException& e) {