Remove unused raw-pointer read helper from univalue

This commit is contained in:
MarcoFalke 2023-07-27 12:17:50 +02:00
parent dfe2dc1d84
commit fa940f41ea
No known key found for this signature in database
13 changed files with 27 additions and 26 deletions

View file

@ -68,7 +68,7 @@
<RawTestFile Include="..\..\src\test\data\*.raw" /> <RawTestFile Include="..\..\src\test\data\*.raw" />
</ItemGroup> </ItemGroup>
<HeaderFromHexdump RawFilePath="%(RawTestFile.FullPath)" HeaderFilePath="%(RawTestFile.FullPath).h" SourceHeader="static unsigned const char %(RawTestFile.Filename)_raw[] = {" SourceFooter="};" /> <HeaderFromHexdump RawFilePath="%(RawTestFile.FullPath)" HeaderFilePath="%(RawTestFile.FullPath).h" SourceHeader="static unsigned const char %(RawTestFile.Filename)_raw[] = {" SourceFooter="};" />
<HeaderFromHexdump RawFilePath="%(JsonTestFile.FullPath)" HeaderFilePath="%(JsonTestFile.FullPath).h" SourceHeader="namespace json_tests{ static unsigned const char %(JsonTestFile.Filename)[] = {" SourceFooter="};}" /> <HeaderFromHexdump RawFilePath="%(JsonTestFile.FullPath)" HeaderFilePath="%(JsonTestFile.FullPath).h" SourceHeader="#include &lt;string&gt;&#x0D;&#x0A;namespace json_tests{ static const std::string %(JsonTestFile.Filename){" SourceFooter="};}" />
</Target> </Target>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" /> <Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" /> <Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />

View file

@ -427,8 +427,9 @@ endif
%.json.h: %.json %.json.h: %.json
@$(MKDIR_P) $(@D) @$(MKDIR_P) $(@D)
$(AM_V_GEN) { \ $(AM_V_GEN) { \
echo "#include <string>" && \
echo "namespace json_tests{" && \ 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' && \ $(HEXDUMP) -v -e '8/1 "0x%02x, "' -e '"\n"' $< | $(SED) -e 's/0x ,//g' && \
echo "};};"; \ echo "};};"; \
} > "$@.new" && mv -f "$@.new" "$@" } > "$@.new" && mv -f "$@.new" "$@"

View file

@ -23,7 +23,7 @@ BOOST_FIXTURE_TEST_SUITE(base58_tests, BasicTestingSetup)
// Goal: test low-level base58 encoding functionality // Goal: test low-level base58 encoding functionality
BOOST_AUTO_TEST_CASE(base58_EncodeBase58) 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++) { for (unsigned int idx = 0; idx < tests.size(); idx++) {
const UniValue& test = tests[idx]; const UniValue& test = tests[idx];
std::string strTest = test.write(); std::string strTest = test.write();
@ -43,7 +43,7 @@ BOOST_AUTO_TEST_CASE(base58_EncodeBase58)
// Goal: test low-level base58 decoding functionality // Goal: test low-level base58 decoding functionality
BOOST_AUTO_TEST_CASE(base58_DecodeBase58) 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<unsigned char> result; std::vector<unsigned char> result;
for (unsigned int idx = 0; idx < tests.size(); idx++) { for (unsigned int idx = 0; idx < tests.size(); idx++) {

View file

@ -128,9 +128,7 @@ BOOST_AUTO_TEST_CASE(blockfilter_basic_test)
BOOST_AUTO_TEST_CASE(blockfilters_json_test) BOOST_AUTO_TEST_CASE(blockfilters_json_test)
{ {
UniValue json; UniValue json;
std::string json_data(json_tests::blockfilters, if (!json.read(json_tests::blockfilters) || !json.isArray()) {
json_tests::blockfilters + sizeof(json_tests::blockfilters));
if (!json.read(json_data) || !json.isArray()) {
BOOST_ERROR("Parse error."); BOOST_ERROR("Parse error.");
return; return;
} }

View file

@ -22,7 +22,7 @@ BOOST_FIXTURE_TEST_SUITE(key_io_tests, BasicTestingSetup)
// Goal: check that parsed keys match test payload // Goal: check that parsed keys match test payload
BOOST_AUTO_TEST_CASE(key_io_valid_parse) 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; CKey privkey;
CTxDestination destination; CTxDestination destination;
SelectParams(ChainType::MAIN); SelectParams(ChainType::MAIN);
@ -83,7 +83,7 @@ BOOST_AUTO_TEST_CASE(key_io_valid_parse)
// Goal: check that generated keys match test vectors // Goal: check that generated keys match test vectors
BOOST_AUTO_TEST_CASE(key_io_valid_gen) 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++) { for (unsigned int idx = 0; idx < tests.size(); idx++) {
const UniValue& test = tests[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 // Goal: check that base58 parsing code is robust against a variety of corrupted data
BOOST_AUTO_TEST_CASE(key_io_invalid) 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; CKey privkey;
CTxDestination destination; CTxDestination destination;

View file

@ -20,7 +20,7 @@
static UniValue JSON(std::string_view json) static UniValue JSON(std::string_view json)
{ {
UniValue value; UniValue value;
BOOST_CHECK(value.read(json.data(), json.size())); BOOST_CHECK(value.read(json));
return value; return value;
} }

View file

@ -394,7 +394,7 @@ BOOST_AUTO_TEST_CASE(bip341_spk_test_vectors)
using control_set = decltype(TaprootSpendData::scripts)::mapped_type; using control_set = decltype(TaprootSpendData::scripts)::mapped_type;
UniValue tests; 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"]; const auto& vectors = tests["scriptPubKey"];

View file

@ -890,7 +890,7 @@ BOOST_AUTO_TEST_CASE(script_build)
std::set<std::string> tests_set; std::set<std::string> 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++) { for (unsigned int idx = 0; idx < json_tests.size(); idx++) {
const UniValue& tv = json_tests[idx]; const UniValue& tv = json_tests[idx];
@ -929,7 +929,7 @@ BOOST_AUTO_TEST_CASE(script_json_test)
// scripts. // scripts.
// If a witness is given, then the last value in the array should be the // If a witness is given, then the last value in the array should be the
// amount (nValue) to use in the crediting tx // 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++) { for (unsigned int idx = 0; idx < tests.size(); idx++) {
const UniValue& test = tests[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) BOOST_AUTO_TEST_CASE(bip341_keypath_test_vectors)
{ {
UniValue tests; 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"]; const auto& vectors = tests["keyPathSpending"];

View file

@ -162,7 +162,7 @@ BOOST_AUTO_TEST_CASE(sighash_test)
// Goal: check that SignatureHash generates correct hash // Goal: check that SignatureHash generates correct hash
BOOST_AUTO_TEST_CASE(sighash_from_data) 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++) { for (unsigned int idx = 0; idx < tests.size(); idx++) {
const UniValue& test = tests[idx]; const UniValue& test = tests[idx];

View file

@ -190,7 +190,7 @@ BOOST_AUTO_TEST_CASE(tx_valid)
{ {
BOOST_CHECK_MESSAGE(CheckMapFlagNames(), "mapFlagNames is missing a script verification flag"); BOOST_CHECK_MESSAGE(CheckMapFlagNames(), "mapFlagNames is missing a script verification flag");
// Read tests from test/data/tx_valid.json // 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++) { for (unsigned int idx = 0; idx < tests.size(); idx++) {
const UniValue& test = tests[idx]; const UniValue& test = tests[idx];
@ -278,7 +278,7 @@ BOOST_AUTO_TEST_CASE(tx_valid)
BOOST_AUTO_TEST_CASE(tx_invalid) BOOST_AUTO_TEST_CASE(tx_invalid)
{ {
// Read tests from test/data/tx_invalid.json // 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++) { for (unsigned int idx = 0; idx < tests.size(); idx++) {
const UniValue& test = tests[idx]; const UniValue& test = tests[idx];

View file

@ -7,13 +7,15 @@
#define BITCOIN_UNIVALUE_INCLUDE_UNIVALUE_H #define BITCOIN_UNIVALUE_INCLUDE_UNIVALUE_H
#include <charconv> #include <charconv>
#include <cstddef>
#include <cstdint> #include <cstdint>
#include <cstring>
#include <map> #include <map>
#include <stdexcept> #include <stdexcept>
#include <string> #include <string>
#include <string_view> #include <string_view>
#include <system_error>
#include <type_traits> #include <type_traits>
#include <utility>
#include <vector> #include <vector>
class UniValue { class UniValue {
@ -94,9 +96,7 @@ public:
std::string write(unsigned int prettyIndent = 0, std::string write(unsigned int prettyIndent = 0,
unsigned int indentLevel = 0) const; unsigned int indentLevel = 0) const;
bool read(const char *raw, size_t len); bool read(std::string_view raw);
bool read(const char *raw) { return read(raw, strlen(raw)); }
bool read(std::string_view raw) { return read(raw.data(), raw.size()); }
private: private:
UniValue::VType typ; UniValue::VType typ;

View file

@ -5,10 +5,11 @@
#include <univalue.h> #include <univalue.h>
#include <univalue_utffilter.h> #include <univalue_utffilter.h>
#include <cstdio>
#include <cstdint> #include <cstdint>
#include <cstdio>
#include <cstring> #include <cstring>
#include <string> #include <string>
#include <string_view>
#include <vector> #include <vector>
/* /*
@ -259,7 +260,7 @@ enum expect_bits : unsigned {
#define setExpect(bit) (expectMask |= EXP_##bit) #define setExpect(bit) (expectMask |= EXP_##bit)
#define clearExpect(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(); clear();
@ -270,7 +271,8 @@ bool UniValue::read(const char *raw, size_t size)
unsigned int consumed; unsigned int consumed;
enum jtokentype tok = JTOK_NONE; enum jtokentype tok = JTOK_NONE;
enum jtokentype last_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 { do {
last_tok = tok; last_tok = tok;

View file

@ -153,7 +153,7 @@ void no_nul_test()
{ {
char buf[] = "___[1,2,3]___"; char buf[] = "___[1,2,3]___";
UniValue val; UniValue val;
assert(val.read(buf + 3, 7)); assert(val.read({buf + 3, 7}));
} }
int main (int argc, char *argv[]) int main (int argc, char *argv[])