From fa940f41eaffa4b2a28c465a10a4c12d4b8976b8 Mon Sep 17 00:00:00 2001 From: MarcoFalke <*~=`'#}+{/-|&$^_@721217.xyz> Date: Thu, 27 Jul 2023 12:17:50 +0200 Subject: [PATCH] Remove unused raw-pointer read helper from univalue --- build_msvc/test_bitcoin/test_bitcoin.vcxproj | 2 +- src/Makefile.test.include | 3 ++- src/test/base58_tests.cpp | 4 ++-- src/test/blockfilter_tests.cpp | 4 +--- src/test/key_io_tests.cpp | 6 +++--- src/test/rpc_tests.cpp | 2 +- src/test/script_standard_tests.cpp | 2 +- src/test/script_tests.cpp | 6 +++--- src/test/sighash_tests.cpp | 2 +- src/test/transaction_tests.cpp | 4 ++-- src/univalue/include/univalue.h | 8 ++++---- src/univalue/lib/univalue_read.cpp | 8 +++++--- src/univalue/test/unitester.cpp | 2 +- 13 files changed, 27 insertions(+), 26 deletions(-) diff --git a/build_msvc/test_bitcoin/test_bitcoin.vcxproj b/build_msvc/test_bitcoin/test_bitcoin.vcxproj index c0c84fc6f18..de836bc01d5 100644 --- a/build_msvc/test_bitcoin/test_bitcoin.vcxproj +++ b/build_msvc/test_bitcoin/test_bitcoin.vcxproj @@ -68,7 +68,7 @@ - + diff --git a/src/Makefile.test.include b/src/Makefile.test.include index 224f1fe301a..f2a82ce73b2 100644 --- a/src/Makefile.test.include +++ b/src/Makefile.test.include @@ -427,8 +427,9 @@ endif %.json.h: %.json @$(MKDIR_P) $(@D) $(AM_V_GEN) { \ + echo "#include " && \ echo "namespace json_tests{" && \ - echo "static unsigned const char $(*F)[] = {" && \ + echo "static const std::string $(*F){" && \ $(HEXDUMP) -v -e '8/1 "0x%02x, "' -e '"\n"' $< | $(SED) -e 's/0x ,//g' && \ echo "};};"; \ } > "$@.new" && mv -f "$@.new" "$@" diff --git a/src/test/base58_tests.cpp b/src/test/base58_tests.cpp index 7f3ca6bf931..bb3defb93ef 100644 --- a/src/test/base58_tests.cpp +++ b/src/test/base58_tests.cpp @@ -23,7 +23,7 @@ BOOST_FIXTURE_TEST_SUITE(base58_tests, BasicTestingSetup) // Goal: test low-level base58 encoding functionality BOOST_AUTO_TEST_CASE(base58_EncodeBase58) { - UniValue tests = read_json(std::string(json_tests::base58_encode_decode, json_tests::base58_encode_decode + sizeof(json_tests::base58_encode_decode))); + UniValue tests = read_json(json_tests::base58_encode_decode); for (unsigned int idx = 0; idx < tests.size(); idx++) { const UniValue& test = tests[idx]; std::string strTest = test.write(); @@ -43,7 +43,7 @@ BOOST_AUTO_TEST_CASE(base58_EncodeBase58) // Goal: test low-level base58 decoding functionality BOOST_AUTO_TEST_CASE(base58_DecodeBase58) { - UniValue tests = read_json(std::string(json_tests::base58_encode_decode, json_tests::base58_encode_decode + sizeof(json_tests::base58_encode_decode))); + UniValue tests = read_json(json_tests::base58_encode_decode); std::vector result; for (unsigned int idx = 0; idx < tests.size(); idx++) { diff --git a/src/test/blockfilter_tests.cpp b/src/test/blockfilter_tests.cpp index 9388b4c96a4..dfeac6ca42e 100644 --- a/src/test/blockfilter_tests.cpp +++ b/src/test/blockfilter_tests.cpp @@ -128,9 +128,7 @@ BOOST_AUTO_TEST_CASE(blockfilter_basic_test) BOOST_AUTO_TEST_CASE(blockfilters_json_test) { UniValue json; - std::string json_data(json_tests::blockfilters, - json_tests::blockfilters + sizeof(json_tests::blockfilters)); - if (!json.read(json_data) || !json.isArray()) { + if (!json.read(json_tests::blockfilters) || !json.isArray()) { BOOST_ERROR("Parse error."); return; } diff --git a/src/test/key_io_tests.cpp b/src/test/key_io_tests.cpp index 92bdbae3c4f..66b4e09ebf8 100644 --- a/src/test/key_io_tests.cpp +++ b/src/test/key_io_tests.cpp @@ -22,7 +22,7 @@ BOOST_FIXTURE_TEST_SUITE(key_io_tests, BasicTestingSetup) // Goal: check that parsed keys match test payload BOOST_AUTO_TEST_CASE(key_io_valid_parse) { - UniValue tests = read_json(std::string(json_tests::key_io_valid, json_tests::key_io_valid + sizeof(json_tests::key_io_valid))); + UniValue tests = read_json(json_tests::key_io_valid); CKey privkey; CTxDestination destination; SelectParams(ChainType::MAIN); @@ -83,7 +83,7 @@ BOOST_AUTO_TEST_CASE(key_io_valid_parse) // Goal: check that generated keys match test vectors BOOST_AUTO_TEST_CASE(key_io_valid_gen) { - UniValue tests = read_json(std::string(json_tests::key_io_valid, json_tests::key_io_valid + sizeof(json_tests::key_io_valid))); + UniValue tests = read_json(json_tests::key_io_valid); for (unsigned int idx = 0; idx < tests.size(); idx++) { const UniValue& test = tests[idx]; @@ -121,7 +121,7 @@ BOOST_AUTO_TEST_CASE(key_io_valid_gen) // Goal: check that base58 parsing code is robust against a variety of corrupted data BOOST_AUTO_TEST_CASE(key_io_invalid) { - UniValue tests = read_json(std::string(json_tests::key_io_invalid, json_tests::key_io_invalid + sizeof(json_tests::key_io_invalid))); // Negative testcases + UniValue tests = read_json(json_tests::key_io_invalid); // Negative testcases CKey privkey; CTxDestination destination; diff --git a/src/test/rpc_tests.cpp b/src/test/rpc_tests.cpp index 2f783a4b95a..4cad3ec68ec 100644 --- a/src/test/rpc_tests.cpp +++ b/src/test/rpc_tests.cpp @@ -20,7 +20,7 @@ static UniValue JSON(std::string_view json) { UniValue value; - BOOST_CHECK(value.read(json.data(), json.size())); + BOOST_CHECK(value.read(json)); return value; } diff --git a/src/test/script_standard_tests.cpp b/src/test/script_standard_tests.cpp index 7bebadf224a..884e3d0634f 100644 --- a/src/test/script_standard_tests.cpp +++ b/src/test/script_standard_tests.cpp @@ -394,7 +394,7 @@ BOOST_AUTO_TEST_CASE(bip341_spk_test_vectors) using control_set = decltype(TaprootSpendData::scripts)::mapped_type; UniValue tests; - tests.read((const char*)json_tests::bip341_wallet_vectors, sizeof(json_tests::bip341_wallet_vectors)); + tests.read(json_tests::bip341_wallet_vectors); const auto& vectors = tests["scriptPubKey"]; diff --git a/src/test/script_tests.cpp b/src/test/script_tests.cpp index c89f2c1f5bf..411924496c3 100644 --- a/src/test/script_tests.cpp +++ b/src/test/script_tests.cpp @@ -890,7 +890,7 @@ BOOST_AUTO_TEST_CASE(script_build) std::set tests_set; { - UniValue json_tests = read_json(std::string(json_tests::script_tests, json_tests::script_tests + sizeof(json_tests::script_tests))); + UniValue json_tests = read_json(json_tests::script_tests); for (unsigned int idx = 0; idx < json_tests.size(); idx++) { const UniValue& tv = json_tests[idx]; @@ -929,7 +929,7 @@ BOOST_AUTO_TEST_CASE(script_json_test) // scripts. // If a witness is given, then the last value in the array should be the // amount (nValue) to use in the crediting tx - UniValue tests = read_json(std::string(json_tests::script_tests, json_tests::script_tests + sizeof(json_tests::script_tests))); + UniValue tests = read_json(json_tests::script_tests); for (unsigned int idx = 0; idx < tests.size(); idx++) { const UniValue& test = tests[idx]; @@ -1743,7 +1743,7 @@ BOOST_AUTO_TEST_CASE(script_assets_test) BOOST_AUTO_TEST_CASE(bip341_keypath_test_vectors) { UniValue tests; - tests.read((const char*)json_tests::bip341_wallet_vectors, sizeof(json_tests::bip341_wallet_vectors)); + tests.read(json_tests::bip341_wallet_vectors); const auto& vectors = tests["keyPathSpending"]; diff --git a/src/test/sighash_tests.cpp b/src/test/sighash_tests.cpp index 68ef719c714..d1c0e1349e8 100644 --- a/src/test/sighash_tests.cpp +++ b/src/test/sighash_tests.cpp @@ -162,7 +162,7 @@ BOOST_AUTO_TEST_CASE(sighash_test) // Goal: check that SignatureHash generates correct hash BOOST_AUTO_TEST_CASE(sighash_from_data) { - UniValue tests = read_json(std::string(json_tests::sighash, json_tests::sighash + sizeof(json_tests::sighash))); + UniValue tests = read_json(json_tests::sighash); for (unsigned int idx = 0; idx < tests.size(); idx++) { const UniValue& test = tests[idx]; diff --git a/src/test/transaction_tests.cpp b/src/test/transaction_tests.cpp index 7511dff9e8d..34dd2c3e9ff 100644 --- a/src/test/transaction_tests.cpp +++ b/src/test/transaction_tests.cpp @@ -190,7 +190,7 @@ BOOST_AUTO_TEST_CASE(tx_valid) { BOOST_CHECK_MESSAGE(CheckMapFlagNames(), "mapFlagNames is missing a script verification flag"); // Read tests from test/data/tx_valid.json - UniValue tests = read_json(std::string(json_tests::tx_valid, json_tests::tx_valid + sizeof(json_tests::tx_valid))); + UniValue tests = read_json(json_tests::tx_valid); for (unsigned int idx = 0; idx < tests.size(); idx++) { const UniValue& test = tests[idx]; @@ -278,7 +278,7 @@ BOOST_AUTO_TEST_CASE(tx_valid) BOOST_AUTO_TEST_CASE(tx_invalid) { // Read tests from test/data/tx_invalid.json - UniValue tests = read_json(std::string(json_tests::tx_invalid, json_tests::tx_invalid + sizeof(json_tests::tx_invalid))); + UniValue tests = read_json(json_tests::tx_invalid); for (unsigned int idx = 0; idx < tests.size(); idx++) { const UniValue& test = tests[idx]; diff --git a/src/univalue/include/univalue.h b/src/univalue/include/univalue.h index 94f80f9c27b..acbce25741b 100644 --- a/src/univalue/include/univalue.h +++ b/src/univalue/include/univalue.h @@ -7,13 +7,15 @@ #define BITCOIN_UNIVALUE_INCLUDE_UNIVALUE_H #include +#include #include -#include #include #include #include #include +#include #include +#include #include class UniValue { @@ -94,9 +96,7 @@ public: std::string write(unsigned int prettyIndent = 0, unsigned int indentLevel = 0) const; - bool read(const char *raw, size_t len); - bool read(const char *raw) { return read(raw, strlen(raw)); } - bool read(std::string_view raw) { return read(raw.data(), raw.size()); } + bool read(std::string_view raw); private: UniValue::VType typ; diff --git a/src/univalue/lib/univalue_read.cpp b/src/univalue/lib/univalue_read.cpp index 2f2385383c1..40d465f4382 100644 --- a/src/univalue/lib/univalue_read.cpp +++ b/src/univalue/lib/univalue_read.cpp @@ -5,10 +5,11 @@ #include #include -#include #include +#include #include #include +#include #include /* @@ -259,7 +260,7 @@ enum expect_bits : unsigned { #define setExpect(bit) (expectMask |= EXP_##bit) #define clearExpect(bit) (expectMask &= ~EXP_##bit) -bool UniValue::read(const char *raw, size_t size) +bool UniValue::read(std::string_view str_in) { clear(); @@ -270,7 +271,8 @@ bool UniValue::read(const char *raw, size_t size) unsigned int consumed; enum jtokentype tok = JTOK_NONE; enum jtokentype last_tok = JTOK_NONE; - const char* end = raw + size; + const char* raw{str_in.data()}; + const char* end{raw + str_in.size()}; do { last_tok = tok; diff --git a/src/univalue/test/unitester.cpp b/src/univalue/test/unitester.cpp index 6344a5a0ab8..d27e647e5cf 100644 --- a/src/univalue/test/unitester.cpp +++ b/src/univalue/test/unitester.cpp @@ -153,7 +153,7 @@ void no_nul_test() { char buf[] = "___[1,2,3]___"; UniValue val; - assert(val.read(buf + 3, 7)); + assert(val.read({buf + 3, 7})); } int main (int argc, char *argv[])