Skip to content

Commit c7272a5

Browse files
committed
Merge pull request #6226
4e157fc json: fail read_string if string contains trailing garbage (Wladimir J. van der Laan)
2 parents 87406aa + 4e157fc commit c7272a5

File tree

2 files changed

+21
-3
lines changed

2 files changed

+21
-3
lines changed

src/json/json_spirit_reader_template.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -521,12 +521,11 @@ namespace json_spirit
521521

522522
const spirit_namespace::parse_info< Iter_type > info =
523523
spirit_namespace::parse( begin, end,
524-
Json_grammer< Value_type, Iter_type >( semantic_actions ),
524+
Json_grammer< Value_type, Iter_type >( semantic_actions ) >> spirit_namespace::end_p,
525525
spirit_namespace::space_p );
526526

527527
if( !info.hit )
528528
{
529-
assert( false ); // in theory exception should already have been thrown
530529
throw_error( info.stop, "error" );
531530
}
532531

@@ -570,7 +569,8 @@ namespace json_spirit
570569
{
571570
typename String_type::const_iterator begin = s.begin();
572571

573-
return read_range( begin, s.end(), value );
572+
bool success = read_range( begin, s.end(), value );
573+
return success && begin == s.end();
574574
}
575575

576576
template< class Istream_type >

src/test/rpc_tests.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,24 @@ BOOST_AUTO_TEST_CASE(rpc_parse_monetary_values)
140140
BOOST_CHECK_EQUAL(AmountFromValue(ValueFromString("20999999.99999999")), 2099999999999999LL);
141141
}
142142

143+
BOOST_AUTO_TEST_CASE(json_parse_errors)
144+
{
145+
Value value;
146+
// Valid
147+
BOOST_CHECK_EQUAL(read_string(std::string("1.0"), value), true);
148+
// Valid, with trailing whitespace
149+
BOOST_CHECK_EQUAL(read_string(std::string("1.0 "), value), true);
150+
// Invalid, initial garbage
151+
BOOST_CHECK_EQUAL(read_string(std::string("[1.0"), value), false);
152+
BOOST_CHECK_EQUAL(read_string(std::string("a1.0"), value), false);
153+
// Invalid, trailing garbage
154+
BOOST_CHECK_EQUAL(read_string(std::string("1.0sds"), value), false);
155+
BOOST_CHECK_EQUAL(read_string(std::string("1.0]"), value), false);
156+
// BTC addresses should fail parsing
157+
BOOST_CHECK_EQUAL(read_string(std::string("175tWpb8K1S7NmH4Zx6rewF9WQrcZv245W"), value), false);
158+
BOOST_CHECK_EQUAL(read_string(std::string("3J98t1WpEZ73CNmQviecrnyiWrnqRhWNL"), value), false);
159+
}
160+
143161
BOOST_AUTO_TEST_CASE(rpc_boostasiotocnetaddr)
144162
{
145163
// Check IPv4 addresses

0 commit comments

Comments
 (0)