File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff 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
319334BOOST_AUTO_TEST_SUITE_END ()
Original file line number Diff line number Diff 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 ;
You can’t perform that action at this time.
0 commit comments