Skip to content

Commit 9827091

Browse files
author
MarcoFalke
committed
Squashed 'src/univalue/' changes from 5839ac3..2740c4f
2740c4f Merge branch '2015_11_escape_plan' into bitcoin 7482163 Add new testcase to Makefile.am 46098ee Version 1.0.1. ccf3575 parser: Ensure multiple values cannot follow each other eb6cd64 Omit Obj/Arr open token from jsonTokenIsValue() test bfef9e2 Makefile.am: list recently added test data, fail{35,36}.json 3e319f3 parser: Tighten array, object syntax checks. c74185c parser: transform C++ variables into bitmask f2568bc Prefer C++ STL vector .at() for accessing object values. 8eafa26 travis: run parallel 'make distcheck' fd448da test: Improve tester diagnostics. Add failing test case from dashpay#15 2158205 Use internal, locale-independent isspace(), isdigit() implementations. 2ab9ad4 travis: Make 'make distcheck' for more comprehensive checks. 3339191 Escape all control characters git-subtree-dir: src/univalue git-subtree-split: 2740c4f
1 parent 313e7f5 commit 9827091

16 files changed

+205
-83
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ univalue-config.h*
1818
test-driver
1919
libtool
2020
ltmain.sh
21+
test-suite.log
2122

2223
*.a
2324
*.la

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ script:
3636
- ./configure --cache-file=config.cache $UNIVALUE_CONFIG_ALL $UNIVALUE_CONFIG || ( cat config.log && false)
3737
- make -s $MAKEJOBS $GOAL || ( echo "Build failure. Verbose build follows." && make $GOAL ; false )
3838
- export LD_LIBRARY_PATH=$TRAVIS_BUILD_DIR/depends/$HOST/lib
39-
- if [ "$RUN_TESTS" = "true" ]; then make check; fi
39+
- if [ "$RUN_TESTS" = "true" ]; then make $MAKEJOBS distcheck; fi
4040

4141
matrix:
4242
fast_finish: true

Makefile.am

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,9 @@ TEST_FILES = \
7070
$(TEST_DATA_DIR)/fail32.json \
7171
$(TEST_DATA_DIR)/fail33.json \
7272
$(TEST_DATA_DIR)/fail34.json \
73+
$(TEST_DATA_DIR)/fail35.json \
74+
$(TEST_DATA_DIR)/fail36.json \
75+
$(TEST_DATA_DIR)/fail37.json \
7376
$(TEST_DATA_DIR)/fail3.json \
7477
$(TEST_DATA_DIR)/fail4.json \
7578
$(TEST_DATA_DIR)/fail5.json \
@@ -79,6 +82,7 @@ TEST_FILES = \
7982
$(TEST_DATA_DIR)/fail9.json \
8083
$(TEST_DATA_DIR)/pass1.json \
8184
$(TEST_DATA_DIR)/pass2.json \
82-
$(TEST_DATA_DIR)/pass3.json
85+
$(TEST_DATA_DIR)/pass3.json \
86+
$(TEST_DATA_DIR)/round1.json
8387

8488
EXTRA_DIST=$(TEST_FILES) $(GEN_SRCS)

configure.ac

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ m4_define([libunivalue_age], [m4_eval(libunivalue_binary_age - libunivalue_inter
1414
m4_define([libunivalue_version], [libunivalue_major_version().libunivalue_minor_version().libunivalue_micro_version()libunivalue_extraversion()])
1515

1616

17-
AC_INIT([univalue], [1.0.0],
17+
AC_INIT([univalue], [1.0.1],
1818
[http://github.com/jgarzik/univalue/])
1919

2020
dnl make the compilation flags quiet unless V=1 is used

gen/gen.cpp

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

11-
#include <ctype.h>
1211
#include <stdio.h>
1312
#include <string.h>
1413
#include "univalue.h"
1514

1615
using namespace std;
1716

1817
static bool initEscapes;
19-
static const char *escapes[256];
18+
static std::string escapes[256];
2019

2120
static void initJsonEscape()
2221
{
22+
// Escape all lower control characters (some get overridden with smaller sequences below)
23+
for (int ch=0x00; ch<0x20; ++ch) {
24+
char tmpbuf[20];
25+
snprintf(tmpbuf, sizeof(tmpbuf), "\\u%04x", ch);
26+
escapes[ch] = std::string(tmpbuf);
27+
}
28+
2329
escapes[(int)'"'] = "\\\"";
2430
escapes[(int)'\\'] = "\\\\";
2531
escapes[(int)'\b'] = "\\b";
2632
escapes[(int)'\f'] = "\\f";
2733
escapes[(int)'\n'] = "\\n";
2834
escapes[(int)'\r'] = "\\r";
2935
escapes[(int)'\t'] = "\\t";
36+
escapes[(int)'\x7f'] = "\\u007f"; // U+007F DELETE
3037

3138
initEscapes = true;
3239
}
@@ -39,13 +46,13 @@ static void outputEscape()
3946
"static const char *escapes[256] = {\n");
4047

4148
for (unsigned int i = 0; i < 256; i++) {
42-
if (!escapes[i]) {
49+
if (escapes[i].empty()) {
4350
printf("\tNULL,\n");
4451
} else {
4552
printf("\t\"");
4653

4754
unsigned int si;
48-
for (si = 0; si < strlen(escapes[i]); si++) {
55+
for (si = 0; si < escapes[i].size(); si++) {
4956
char ch = escapes[i][si];
5057
switch (ch) {
5158
case '"':

include/univalue.h

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -243,8 +243,41 @@ extern enum jtokentype getJsonToken(std::string& tokenVal,
243243
unsigned int& consumed, const char *raw);
244244
extern const char *uvTypeName(UniValue::VType t);
245245

246+
static inline bool jsonTokenIsValue(enum jtokentype jtt)
247+
{
248+
switch (jtt) {
249+
case JTOK_KW_NULL:
250+
case JTOK_KW_TRUE:
251+
case JTOK_KW_FALSE:
252+
case JTOK_NUMBER:
253+
case JTOK_STRING:
254+
return true;
255+
256+
default:
257+
return false;
258+
}
259+
260+
// not reached
261+
}
262+
263+
static inline bool json_isspace(int ch)
264+
{
265+
switch (ch) {
266+
case 0x20:
267+
case 0x09:
268+
case 0x0a:
269+
case 0x0d:
270+
return true;
271+
272+
default:
273+
return false;
274+
}
275+
276+
// not reached
277+
}
278+
246279
extern const UniValue NullUniValue;
247280

248281
const UniValue& find_value( const UniValue& obj, const std::string& name);
249282

250-
#endif // __UNIVALUE_H__
283+
#endif // __UNIVALUE_H__

lib/univalue.cpp

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
55

66
#include <stdint.h>
7-
#include <ctype.h>
87
#include <errno.h>
98
#include <iomanip>
109
#include <limits>
@@ -21,7 +20,7 @@ static bool ParsePrechecks(const std::string& str)
2120
{
2221
if (str.empty()) // No empty string allowed
2322
return false;
24-
if (str.size() >= 1 && (isspace(str[0]) || isspace(str[str.size()-1]))) // No padding allowed
23+
if (str.size() >= 1 && (json_isspace(str[0]) || json_isspace(str[str.size()-1]))) // No padding allowed
2524
return false;
2625
if (str.size() != strlen(str.c_str())) // No embedded NUL characters allowed
2726
return false;
@@ -210,7 +209,7 @@ bool UniValue::pushKVs(const UniValue& obj)
210209

211210
for (unsigned int i = 0; i < obj.keys.size(); i++) {
212211
keys.push_back(obj.keys[i]);
213-
values.push_back(obj.values[i]);
212+
values.push_back(obj.values.at(i));
214213
}
215214

216215
return true;
@@ -234,7 +233,7 @@ bool UniValue::checkObject(const std::map<std::string,UniValue::VType>& t)
234233
if (idx < 0)
235234
return false;
236235

237-
if (values[idx].getType() != it->second)
236+
if (values.at(idx).getType() != it->second)
238237
return false;
239238
}
240239

@@ -250,7 +249,7 @@ const UniValue& UniValue::operator[](const std::string& key) const
250249
if (index < 0)
251250
return NullUniValue;
252251

253-
return values[index];
252+
return values.at(index);
254253
}
255254

256255
const UniValue& UniValue::operator[](unsigned int index) const
@@ -260,7 +259,7 @@ const UniValue& UniValue::operator[](unsigned int index) const
260259
if (index >= values.size())
261260
return NullUniValue;
262261

263-
return values[index];
262+
return values.at(index);
264263
}
265264

266265
const char *uvTypeName(UniValue::VType t)
@@ -278,15 +277,11 @@ const char *uvTypeName(UniValue::VType t)
278277
return NULL;
279278
}
280279

281-
const UniValue& find_value( const UniValue& obj, const std::string& name)
280+
const UniValue& find_value(const UniValue& obj, const std::string& name)
282281
{
283282
for (unsigned int i = 0; i < obj.keys.size(); i++)
284-
{
285-
if( obj.keys[i] == name )
286-
{
287-
return obj.values[i];
288-
}
289-
}
283+
if (obj.keys[i] == name)
284+
return obj.values.at(i);
290285

291286
return NullUniValue;
292287
}

lib/univalue_escapes.h

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -2,38 +2,38 @@
22
#ifndef BITCOIN_UNIVALUE_UNIVALUE_ESCAPES_H
33
#define BITCOIN_UNIVALUE_UNIVALUE_ESCAPES_H
44
static const char *escapes[256] = {
5-
NULL,
6-
NULL,
7-
NULL,
8-
NULL,
9-
NULL,
10-
NULL,
11-
NULL,
12-
NULL,
5+
"\\u0000",
6+
"\\u0001",
7+
"\\u0002",
8+
"\\u0003",
9+
"\\u0004",
10+
"\\u0005",
11+
"\\u0006",
12+
"\\u0007",
1313
"\\b",
1414
"\\t",
1515
"\\n",
16-
NULL,
16+
"\\u000b",
1717
"\\f",
1818
"\\r",
19-
NULL,
20-
NULL,
21-
NULL,
22-
NULL,
23-
NULL,
24-
NULL,
25-
NULL,
26-
NULL,
27-
NULL,
28-
NULL,
29-
NULL,
30-
NULL,
31-
NULL,
32-
NULL,
33-
NULL,
34-
NULL,
35-
NULL,
36-
NULL,
19+
"\\u000e",
20+
"\\u000f",
21+
"\\u0010",
22+
"\\u0011",
23+
"\\u0012",
24+
"\\u0013",
25+
"\\u0014",
26+
"\\u0015",
27+
"\\u0016",
28+
"\\u0017",
29+
"\\u0018",
30+
"\\u0019",
31+
"\\u001a",
32+
"\\u001b",
33+
"\\u001c",
34+
"\\u001d",
35+
"\\u001e",
36+
"\\u001f",
3737
NULL,
3838
NULL,
3939
"\\\"",
@@ -129,7 +129,7 @@ static const char *escapes[256] = {
129129
NULL,
130130
NULL,
131131
NULL,
132-
NULL,
132+
"\\u007f",
133133
NULL,
134134
NULL,
135135
NULL,

0 commit comments

Comments
 (0)