Page MenuHomePhabricator

D16296.id48137.diff
No OneTemporary

D16296.id48137.diff

diff --git a/src/test/base58_tests.cpp b/src/test/base58_tests.cpp
--- a/src/test/base58_tests.cpp
+++ b/src/test/base58_tests.cpp
@@ -22,10 +22,7 @@
// 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++) {
UniValue test = tests[idx];
std::string strTest = test.write();
@@ -42,10 +39,7 @@
// 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<uint8_t> result;
for (unsigned int idx = 0; idx < tests.size(); idx++) {
diff --git a/src/test/blockfilter_tests.cpp b/src/test/blockfilter_tests.cpp
--- a/src/test/blockfilter_tests.cpp
+++ b/src/test/blockfilter_tests.cpp
@@ -138,10 +138,7 @@
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/data/generate_header.py b/src/test/data/generate_header.py
--- a/src/test/data/generate_header.py
+++ b/src/test/data/generate_header.py
@@ -8,9 +8,9 @@
with open(input_file, "rb") as f:
contents = f.read()
- print("#include <cstdint>\n")
+ print("#include <string>\n")
print("namespace json_tests {")
- print(f"static const uint8_t {test_name}[] = {{")
+ print(f"static const std::string {test_name}{{")
print(", ".join(f"0x{x:02x}" for x in contents))
print("};")
print("};")
diff --git a/src/test/key_io_tests.cpp b/src/test/key_io_tests.cpp
--- a/src/test/key_io_tests.cpp
+++ b/src/test/key_io_tests.cpp
@@ -22,9 +22,7 @@
// 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(CBaseChainParams::MAIN);
@@ -97,9 +95,7 @@
// 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++) {
UniValue test = tests[idx];
@@ -138,9 +134,7 @@
// data
BOOST_AUTO_TEST_CASE(key_io_invalid) {
// Negative testcases
- UniValue tests = read_json(std::string(
- json_tests::key_io_invalid,
- json_tests::key_io_invalid + sizeof(json_tests::key_io_invalid)));
+ UniValue tests = read_json(json_tests::key_io_invalid);
CKey privkey;
CTxDestination destination;
diff --git a/src/test/rpc_tests.cpp b/src/test/rpc_tests.cpp
--- a/src/test/rpc_tests.cpp
+++ b/src/test/rpc_tests.cpp
@@ -22,7 +22,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_tests.cpp b/src/test/script_tests.cpp
--- a/src/test/script_tests.cpp
+++ b/src/test/script_tests.cpp
@@ -2362,9 +2362,7 @@
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++) {
const UniValue &tv = json_tests[idx];
@@ -2403,9 +2401,7 @@
// "flags", "expected_scripterror" ]
// ... where scriptSig and scriptPubKey are stringified
// scripts.
- 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++) {
UniValue test = tests[idx];
diff --git a/src/test/sighash_tests.cpp b/src/test/sighash_tests.cpp
--- a/src/test/sighash_tests.cpp
+++ b/src/test/sighash_tests.cpp
@@ -213,9 +213,7 @@
// 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 (size_t idx = 0; idx < tests.size(); idx++) {
UniValue test = tests[idx];
diff --git a/src/test/transaction_tests.cpp b/src/test/transaction_tests.cpp
--- a/src/test/transaction_tests.cpp
+++ b/src/test/transaction_tests.cpp
@@ -64,9 +64,7 @@
//
// verifyFlags is a comma separated list of script verification flags to
// apply, or "NONE"
- 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);
ScriptError err;
for (size_t idx = 0; idx < tests.size(); idx++) {
@@ -164,9 +162,7 @@
//
// verifyFlags is a comma separated list of script verification flags to
// apply, or "NONE"
- 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);
// Initialize to ScriptError::OK. The tests expect err to be changed to a
// value other than ScriptError::OK.
diff --git a/src/univalue/include/univalue.h b/src/univalue/include/univalue.h
--- a/src/univalue/include/univalue.h
+++ b/src/univalue/include/univalue.h
@@ -7,13 +7,15 @@
#define BITCOIN_UNIVALUE_INCLUDE_UNIVALUE_H
#include <charconv>
+#include <cstddef>
#include <cstdint>
-#include <cstring>
#include <map>
#include <stdexcept>
#include <string>
#include <string_view>
+#include <system_error>
#include <type_traits>
+#include <utility>
#include <vector>
namespace {
@@ -109,9 +111,7 @@
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
--- a/src/univalue/lib/univalue_read.cpp
+++ b/src/univalue/lib/univalue_read.cpp
@@ -5,10 +5,11 @@
#include <univalue.h>
#include <univalue_utffilter.h>
-#include <cstdio>
#include <cstdint>
+#include <cstdio>
#include <cstring>
#include <string>
+#include <string_view>
#include <vector>
/*
@@ -259,7 +260,7 @@
#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 @@
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
--- a/src/univalue/test/unitester.cpp
+++ b/src/univalue/test/unitester.cpp
@@ -153,7 +153,7 @@
{
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[])

File Metadata

Mime Type
text/plain
Expires
Sat, Mar 1, 10:37 (11 h, 1 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
5187374
Default Alt Text
D16296.id48137.diff (8 KB)

Event Timeline