From 4162f42f3140d6f73bf643a141bc6451220854f1 Mon Sep 17 00:00:00 2001 From: Paul Calabrese Date: Sun, 10 Dec 2017 09:04:14 -0600 Subject: [PATCH] STAT-273: Fix json parsing issues (cherry picked from commit 9aa838e9a7ebdc69766ed1f1e465465d52f921e9) --- libraries/fc/src/io/json.cpp | 9 ++++----- tests/tests/misc_tests.cpp | 20 ++++++++++++++++++++ 2 files changed, 24 insertions(+), 5 deletions(-) diff --git a/libraries/fc/src/io/json.cpp b/libraries/fc/src/io/json.cpp index e7a690539..5980cbf73 100644 --- a/libraries/fc/src/io/json.cpp +++ b/libraries/fc/src/io/json.cpp @@ -96,9 +96,8 @@ namespace fc "Expected '\"' but read '${char}'", ("char", string(&c, (&c) + 1) ) ); in.get(); - while( true ) + while( !in.eof() ) { - switch( c = in.peek() ) { case '\\': @@ -128,7 +127,7 @@ namespace fc { char c = in.peek(); - while( true ) + while( !in.eof() ) { switch( c = in.peek() ) { @@ -359,7 +358,7 @@ namespace fc return variant(); if( str == "true" ) return true; - if( str == "false" ) + if( str == "false" ) return false; else { @@ -453,7 +452,7 @@ namespace fc FC_ASSERT( open_object < 100 && open_array < 100, "object graph too deep", ("object depth",open_object)("array depth", open_array) ); } } - + variant json::from_string( const std::string& utf8_str, parse_type ptype ) { try { check_string_depth( utf8_str ); diff --git a/tests/tests/misc_tests.cpp b/tests/tests/misc_tests.cpp index c0f26ccb3..f95eb3b0d 100644 --- a/tests/tests/misc_tests.cpp +++ b/tests/tests/misc_tests.cpp @@ -22,6 +22,26 @@ using namespace std; BOOST_AUTO_TEST_SUITE(misc_tests) +/// Test processing of unbalanced strings +BOOST_AUTO_TEST_CASE(json_from_string_test) +{ + bool exc_found = false; + try { + auto val = fc::json::from_string("{\"}"); + } catch(...) { + exc_found = true; + } + BOOST_CHECK_EQUAL(exc_found, true); + + exc_found = false; + try { + auto val = fc::json::from_string("{\"block_num_or_id\":5"); + } catch(...) { + exc_found = true; + } + BOOST_CHECK_EQUAL(exc_found, true); +} + /// Test calculation of median values of blockchain operation properties BOOST_AUTO_TEST_CASE(median_properties_test) { try { -- GitLab