Skip to content

Commit da9beb2

Browse files
committed
Merge pull request #6576
e938122 Stop parsing JSON after first finished construct. (Daniel Kraft)
2 parents 561f8af + e938122 commit da9beb2

File tree

2 files changed

+23
-6
lines changed

2 files changed

+23
-6
lines changed

src/test/univalue_tests.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,21 @@ BOOST_AUTO_TEST_CASE(univalue_readwrite)
314314
BOOST_CHECK(obj["key3"].isObject());
315315

316316
BOOST_CHECK_EQUAL(strJson1, v.write());
317+
318+
/* Check for (correctly reporting) a parsing error if the initial
319+
JSON construct is followed by more stuff. Note that whitespace
320+
is, of course, exempt. */
321+
322+
BOOST_CHECK(v.read(" {}\n "));
323+
BOOST_CHECK(v.isObject());
324+
BOOST_CHECK(v.read(" []\n "));
325+
BOOST_CHECK(v.isArray());
326+
327+
BOOST_CHECK(!v.read("@{}"));
328+
BOOST_CHECK(!v.read("{} garbage"));
329+
BOOST_CHECK(!v.read("[]{}"));
330+
BOOST_CHECK(!v.read("{}[]"));
331+
BOOST_CHECK(!v.read("{} 42"));
317332
}
318333

319334
BOOST_AUTO_TEST_SUITE_END()

src/univalue/univalue_read.cpp

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -244,16 +244,16 @@ bool UniValue::read(const char *raw)
244244
bool expectColon = false;
245245
vector<UniValue*> stack;
246246

247+
string tokenVal;
248+
unsigned int consumed;
247249
enum jtokentype tok = JTOK_NONE;
248250
enum jtokentype last_tok = JTOK_NONE;
249-
while (1) {
251+
do {
250252
last_tok = tok;
251253

252-
string tokenVal;
253-
unsigned int consumed;
254254
tok = getJsonToken(tokenVal, consumed, raw);
255255
if (tok == JTOK_NONE || tok == JTOK_ERR)
256-
break;
256+
return false;
257257
raw += consumed;
258258

259259
switch (tok) {
@@ -377,9 +377,11 @@ bool UniValue::read(const char *raw)
377377
default:
378378
return false;
379379
}
380-
}
380+
} while (!stack.empty ());
381381

382-
if (stack.size() != 0)
382+
/* Check that nothing follows the initial construct (parsed above). */
383+
tok = getJsonToken(tokenVal, consumed, raw);
384+
if (tok != JTOK_NONE)
383385
return false;
384386

385387
return true;

0 commit comments

Comments
 (0)