Skip to content

Commit

Permalink
Merge pull request #6576
Browse files Browse the repository at this point in the history
e938122 Stop parsing JSON after first finished construct. (Daniel Kraft)
  • Loading branch information
laanwj committed Aug 24, 2015
2 parents 561f8af + e938122 commit da9beb2
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 6 deletions.
15 changes: 15 additions & 0 deletions src/test/univalue_tests.cpp
Expand Up @@ -314,6 +314,21 @@ BOOST_AUTO_TEST_CASE(univalue_readwrite)
BOOST_CHECK(obj["key3"].isObject());

BOOST_CHECK_EQUAL(strJson1, v.write());

/* Check for (correctly reporting) a parsing error if the initial
JSON construct is followed by more stuff. Note that whitespace
is, of course, exempt. */

BOOST_CHECK(v.read(" {}\n "));
BOOST_CHECK(v.isObject());
BOOST_CHECK(v.read(" []\n "));
BOOST_CHECK(v.isArray());

BOOST_CHECK(!v.read("@{}"));
BOOST_CHECK(!v.read("{} garbage"));
BOOST_CHECK(!v.read("[]{}"));
BOOST_CHECK(!v.read("{}[]"));
BOOST_CHECK(!v.read("{} 42"));
}

BOOST_AUTO_TEST_SUITE_END()
Expand Down
14 changes: 8 additions & 6 deletions src/univalue/univalue_read.cpp
Expand Up @@ -244,16 +244,16 @@ bool UniValue::read(const char *raw)
bool expectColon = false;
vector<UniValue*> stack;

string tokenVal;
unsigned int consumed;
enum jtokentype tok = JTOK_NONE;
enum jtokentype last_tok = JTOK_NONE;
while (1) {
do {
last_tok = tok;

string tokenVal;
unsigned int consumed;
tok = getJsonToken(tokenVal, consumed, raw);
if (tok == JTOK_NONE || tok == JTOK_ERR)
break;
return false;
raw += consumed;

switch (tok) {
Expand Down Expand Up @@ -377,9 +377,11 @@ bool UniValue::read(const char *raw)
default:
return false;
}
}
} while (!stack.empty ());

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

return true;
Expand Down

0 comments on commit da9beb2

Please sign in to comment.