Skip to content

Commit fac2c79

Browse files
author
MacroFake
committed
Bump univalue subtree
2 parents dd9f61a + f403531 commit fac2c79

File tree

12 files changed

+74
-96
lines changed

12 files changed

+74
-96
lines changed

src/univalue/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ This class is aligned with the JSON standard, [RFC
1515
## Library usage
1616

1717
This is a fork of univalue used by Bitcoin Core. It is not maintained for usage
18-
by other projects. Notably, the API may break in non-backward-compatible ways.
18+
by other projects. Notably, the API is broken in non-backward-compatible ways.
1919

2020
Other projects looking for a maintained library should use the upstream
2121
univalue at https://github.com/jgarzik/univalue.

src/univalue/configure.ac

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,8 @@ AC_SUBST(LIBUNIVALUE_AGE)
4545
LT_INIT
4646
LT_LANG([C++])
4747

48-
dnl Require C++11 compiler (no GNU extensions)
49-
AX_CXX_COMPILE_STDCXX([11], [noext], [mandatory], [nodefault])
48+
dnl Require C++17 compiler (no GNU extensions)
49+
AX_CXX_COMPILE_STDCXX([17], [noext], [mandatory], [nodefault])
5050

5151
case $host in
5252
*mingw*)

src/univalue/gen/gen.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,11 @@
88
// $ ./gen > univalue_escapes.h
99
//
1010

11-
#include <stdio.h>
12-
#include <string.h>
13-
#include "univalue.h"
11+
#include <univalue.h>
12+
13+
#include <cstdio>
14+
#include <cstring>
15+
#include <string>
1416

1517
static bool initEscapes;
1618
static std::string escapes[256];

src/univalue/include/univalue.h

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,14 @@
66
#ifndef __UNIVALUE_H__
77
#define __UNIVALUE_H__
88

9-
#include <stdint.h>
10-
#include <string.h>
11-
9+
#include <charconv>
10+
#include <cstdint>
11+
#include <cstring>
12+
#include <map>
13+
#include <stdexcept>
1214
#include <string>
15+
#include <type_traits>
1316
#include <vector>
14-
#include <map>
15-
#include <cassert>
1617

1718
class UniValue {
1819
public:
@@ -168,10 +169,24 @@ class UniValue {
168169
// value is of unexpected type
169170
const std::vector<std::string>& getKeys() const;
170171
const std::vector<UniValue>& getValues() const;
172+
template <typename Int>
173+
auto getInt() const
174+
{
175+
static_assert(std::is_integral<Int>::value);
176+
if (typ != VNUM) {
177+
throw std::runtime_error("JSON value is not an integer as expected");
178+
}
179+
Int result;
180+
const auto [first_nonmatching, error_condition] = std::from_chars(val.data(), val.data() + val.size(), result);
181+
if (first_nonmatching != val.data() + val.size() || error_condition != std::errc{}) {
182+
throw std::runtime_error("JSON integer out of range");
183+
}
184+
return result;
185+
}
171186
bool get_bool() const;
172187
const std::string& get_str() const;
173-
int get_int() const;
174-
int64_t get_int64() const;
188+
auto get_int() const { return getInt<int>(); };
189+
auto get_int64() const { return getInt<int64_t>(); };
175190
double get_real() const;
176191
const UniValue& get_obj() const;
177192
const UniValue& get_array() const;

src/univalue/lib/univalue.cpp

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,15 @@
33
// Distributed under the MIT software license, see the accompanying
44
// file COPYING or https://opensource.org/licenses/mit-license.php.
55

6-
#include <stdint.h>
6+
#include <univalue.h>
7+
78
#include <iomanip>
9+
#include <map>
10+
#include <memory>
811
#include <sstream>
9-
#include <stdlib.h>
10-
11-
#include "univalue.h"
12+
#include <string>
13+
#include <utility>
14+
#include <vector>
1215

1316
const UniValue NullUniValue;
1417

src/univalue/lib/univalue_get.cpp

Lines changed: 10 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,18 @@
33
// Distributed under the MIT software license, see the accompanying
44
// file COPYING or https://opensource.org/licenses/mit-license.php.
55

6-
#include <stdint.h>
7-
#include <errno.h>
8-
#include <string.h>
9-
#include <stdlib.h>
10-
#include <stdexcept>
11-
#include <vector>
6+
#include <univalue.h>
7+
8+
#include <cerrno>
9+
#include <cstdint>
10+
#include <cstdlib>
11+
#include <cstring>
1212
#include <limits>
13-
#include <string>
13+
#include <locale>
1414
#include <sstream>
15-
16-
#include "univalue.h"
15+
#include <stdexcept>
16+
#include <string>
17+
#include <vector>
1718

1819
namespace
1920
{
@@ -28,37 +29,6 @@ static bool ParsePrechecks(const std::string& str)
2829
return true;
2930
}
3031

31-
bool ParseInt32(const std::string& str, int32_t *out)
32-
{
33-
if (!ParsePrechecks(str))
34-
return false;
35-
char *endp = nullptr;
36-
errno = 0; // strtol will not set errno if valid
37-
long int n = strtol(str.c_str(), &endp, 10);
38-
if(out) *out = (int32_t)n;
39-
// Note that strtol returns a *long int*, so even if strtol doesn't report an over/underflow
40-
// we still have to check that the returned value is within the range of an *int32_t*. On 64-bit
41-
// platforms the size of these types may be different.
42-
return endp && *endp == 0 && !errno &&
43-
n >= std::numeric_limits<int32_t>::min() &&
44-
n <= std::numeric_limits<int32_t>::max();
45-
}
46-
47-
bool ParseInt64(const std::string& str, int64_t *out)
48-
{
49-
if (!ParsePrechecks(str))
50-
return false;
51-
char *endp = nullptr;
52-
errno = 0; // strtoll will not set errno if valid
53-
long long int n = strtoll(str.c_str(), &endp, 10);
54-
if(out) *out = (int64_t)n;
55-
// Note that strtoll returns a *long long int*, so even if strtol doesn't report a over/underflow
56-
// we still have to check that the returned value is within the range of an *int64_t*.
57-
return endp && *endp == 0 && !errno &&
58-
n >= std::numeric_limits<int64_t>::min() &&
59-
n <= std::numeric_limits<int64_t>::max();
60-
}
61-
6232
bool ParseDouble(const std::string& str, double *out)
6333
{
6434
if (!ParsePrechecks(str))
@@ -102,26 +72,6 @@ const std::string& UniValue::get_str() const
10272
return getValStr();
10373
}
10474

105-
int UniValue::get_int() const
106-
{
107-
if (typ != VNUM)
108-
throw std::runtime_error("JSON value is not an integer as expected");
109-
int32_t retval;
110-
if (!ParseInt32(getValStr(), &retval))
111-
throw std::runtime_error("JSON integer out of range");
112-
return retval;
113-
}
114-
115-
int64_t UniValue::get_int64() const
116-
{
117-
if (typ != VNUM)
118-
throw std::runtime_error("JSON value is not an integer as expected");
119-
int64_t retval;
120-
if (!ParseInt64(getValStr(), &retval))
121-
throw std::runtime_error("JSON integer out of range");
122-
return retval;
123-
}
124-
12575
double UniValue::get_real() const
12676
{
12777
if (typ != VNUM)

src/univalue/lib/univalue_read.cpp

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,22 @@
22
// Distributed under the MIT software license, see the accompanying
33
// file COPYING or https://opensource.org/licenses/mit-license.php.
44

5-
#include <string.h>
6-
#include <vector>
7-
#include <stdio.h>
8-
#include "univalue.h"
5+
#include <univalue.h>
96
#include "univalue_utffilter.h"
107

8+
#include <cstdio>
9+
#include <cstdint>
10+
#include <cstring>
11+
#include <string>
12+
#include <vector>
13+
1114
/*
1215
* According to stackexchange, the original json test suite wanted
1316
* to limit depth to 22. Widely-deployed PHP bails at depth 512,
1417
* so we will follow PHP's lead, which should be more than sufficient
1518
* (further stackexchange comments indicate depth > 32 rarely occurs).
1619
*/
17-
static const size_t MAX_JSON_DEPTH = 512;
20+
static constexpr size_t MAX_JSON_DEPTH = 512;
1821

1922
static bool json_isdigit(int ch)
2023
{

src/univalue/lib/univalue_write.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@
22
// Distributed under the MIT software license, see the accompanying
33
// file COPYING or https://opensource.org/licenses/mit-license.php.
44

5-
#include <iomanip>
6-
#include <stdio.h>
7-
#include "univalue.h"
5+
#include <univalue.h>
86
#include "univalue_escapes.h"
97

8+
#include <memory>
9+
#include <string>
10+
#include <vector>
11+
1012
static std::string json_escape(const std::string& inS)
1113
{
1214
std::string outS;

src/univalue/test/no_nul.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#include "univalue.h"
1+
#include <univalue.h>
22

33
int main (int argc, char *argv[])
44
{

src/univalue/test/object.cpp

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,15 @@
33
// Distributed under the MIT software license, see the accompanying
44
// file COPYING or https://opensource.org/licenses/mit-license.php.
55

6-
#include <stdint.h>
7-
#include <vector>
8-
#include <string>
9-
#include <map>
6+
#include <univalue.h>
7+
108
#include <cassert>
9+
#include <cstdint>
10+
#include <map>
11+
#include <memory>
1112
#include <stdexcept>
12-
#include <univalue.h>
13+
#include <string>
14+
#include <vector>
1315

1416
#define BOOST_FIXTURE_TEST_SUITE(a, b)
1517
#define BOOST_AUTO_TEST_CASE(funcName) void funcName()

0 commit comments

Comments
 (0)