From ea0797351fbabd3ec9dc98304c7c12c6cd1c61dd Mon Sep 17 00:00:00 2001 From: Christopher Dunn Date: Sat, 19 Apr 2014 06:37:23 +0000 Subject: [PATCH] JSON_ASSERT -> JSON_ASSERT_MESSAGE This way, assertions can produce exceptions. https://sourceforge.net/p/jsoncpp/bugs/67/ --- src/lib_json/json_value.cpp | 27 ++++++++++++++------------- src/test_lib_json/main.cpp | 15 +++++++++++++++ 2 files changed, 29 insertions(+), 13 deletions(-) diff --git a/src/lib_json/json_value.cpp b/src/lib_json/json_value.cpp index 6fdb2d7..b925681 100644 --- a/src/lib_json/json_value.cpp +++ b/src/lib_json/json_value.cpp @@ -1,3 +1,4 @@ +// vim: ts=3 sts=3 sw=3 tw=0 // Copyright 2011 Baptiste Lepilleur // Distributed under MIT license, or public domain if desired and // recognized in your jurisdiction. @@ -91,7 +92,7 @@ duplicateStringValue( const char *value, length = Value::maxInt - 1; char *newString = static_cast( malloc( length + 1 ) ); - JSON_ASSERT_MESSAGE( newString != 0, "Failed to allocate string value buffer" ); + JSON_ASSERT_MESSAGE( newString != 0, "in Json::Value::duplicateStringValue(): Failed to allocate string value buffer" ); memcpy( newString, value, length ); newString[length] = 0; return newString; @@ -155,7 +156,7 @@ Value::CommentInfo::setComment( const char *text ) if ( comment_ ) releaseStringValue( comment_ ); JSON_ASSERT( text != 0 ); - JSON_ASSERT_MESSAGE( text[0]=='\0' || text[0]=='/', "Comments must start with /"); + JSON_ASSERT_MESSAGE( text[0]=='\0' || text[0]=='/', "in Json::Value::setComment(): Comments must start with /"); // It seems that /**/ style comments are acceptable as well. comment_ = duplicateStringValue( text ); } @@ -692,7 +693,7 @@ Value::operator !=( const Value &other ) const const char * Value::asCString() const { - JSON_ASSERT( type_ == stringValue ); + JSON_ASSERT_MESSAGE( type_ == stringValue, "in Json::Value::asCString(): requires stringValue" ); return value_.string_; } @@ -1026,7 +1027,7 @@ Value::operator!() const void Value::clear() { - JSON_ASSERT( type_ == nullValue || type_ == arrayValue || type_ == objectValue ); + JSON_ASSERT_MESSAGE( type_ == nullValue || type_ == arrayValue || type_ == objectValue, "in Json::Value::clear(): requires complex value" ); switch ( type_ ) { @@ -1051,7 +1052,7 @@ Value::clear() void Value::resize( ArrayIndex newSize ) { - JSON_ASSERT( type_ == nullValue || type_ == arrayValue ); + JSON_ASSERT_MESSAGE( type_ == nullValue || type_ == arrayValue, "in Json::Value::resize(): requires arrayValue" ); if ( type_ == nullValue ) *this = Value( arrayValue ); #ifndef JSON_VALUE_USE_INTERNAL_MAP @@ -1077,7 +1078,7 @@ Value::resize( ArrayIndex newSize ) Value & Value::operator[]( ArrayIndex index ) { - JSON_ASSERT( type_ == nullValue || type_ == arrayValue ); + JSON_ASSERT_MESSAGE( type_ == nullValue || type_ == arrayValue, "in Json::Value::operator[](ArrayIndex): requires arrayValue" ); if ( type_ == nullValue ) *this = Value( arrayValue ); #ifndef JSON_VALUE_USE_INTERNAL_MAP @@ -1098,7 +1099,7 @@ Value::operator[]( ArrayIndex index ) Value & Value::operator[]( int index ) { - JSON_ASSERT( index >= 0 ); + JSON_ASSERT_MESSAGE( index >= 0, "in Json::Value::operator[](int index): index cannot be negative" ); return (*this)[ ArrayIndex(index) ]; } @@ -1106,7 +1107,7 @@ Value::operator[]( int index ) const Value & Value::operator[]( ArrayIndex index ) const { - JSON_ASSERT( type_ == nullValue || type_ == arrayValue ); + JSON_ASSERT_MESSAGE( type_ == nullValue || type_ == arrayValue, "in Json::Value::operator[](ArrayIndex)const: requires arrayValue" ); if ( type_ == nullValue ) return null; #ifndef JSON_VALUE_USE_INTERNAL_MAP @@ -1125,7 +1126,7 @@ Value::operator[]( ArrayIndex index ) const const Value & Value::operator[]( int index ) const { - JSON_ASSERT( index >= 0 ); + JSON_ASSERT_MESSAGE( index >= 0, "in Json::Value::operator[](int index) const: index cannot be negative" ); return (*this)[ ArrayIndex(index) ]; } @@ -1141,7 +1142,7 @@ Value & Value::resolveReference( const char *key, bool isStatic ) { - JSON_ASSERT( type_ == nullValue || type_ == objectValue ); + JSON_ASSERT_MESSAGE( type_ == nullValue || type_ == objectValue, "in Json::Value::resolveReference(): requires objectValue" ); if ( type_ == nullValue ) *this = Value( objectValue ); #ifndef JSON_VALUE_USE_INTERNAL_MAP @@ -1181,7 +1182,7 @@ Value::isValidIndex( ArrayIndex index ) const const Value & Value::operator[]( const char *key ) const { - JSON_ASSERT( type_ == nullValue || type_ == objectValue ); + JSON_ASSERT_MESSAGE( type_ == nullValue || type_ == objectValue, "in Json::Value::operator[](char const*)const: requires objectValue" ); if ( type_ == nullValue ) return null; #ifndef JSON_VALUE_USE_INTERNAL_MAP @@ -1259,7 +1260,7 @@ Value::get( const std::string &key, Value Value::removeMember( const char* key ) { - JSON_ASSERT( type_ == nullValue || type_ == objectValue ); + JSON_ASSERT_MESSAGE( type_ == nullValue || type_ == objectValue, "in Json::Value::removeMember(): requires objectValue" ); if ( type_ == nullValue ) return null; #ifndef JSON_VALUE_USE_INTERNAL_MAP @@ -1323,7 +1324,7 @@ Value::isMember( const CppTL::ConstString &key ) const Value::Members Value::getMemberNames() const { - JSON_ASSERT( type_ == nullValue || type_ == objectValue ); + JSON_ASSERT_MESSAGE( type_ == nullValue || type_ == objectValue, "in Json::Value::getMemberNames(), value must be objectValue" ); if ( type_ == nullValue ) return Value::Members(); Members members; diff --git a/src/test_lib_json/main.cpp b/src/test_lib_json/main.cpp index 31f103c..c1a5e20 100644 --- a/src/test_lib_json/main.cpp +++ b/src/test_lib_json/main.cpp @@ -1,3 +1,4 @@ +// vim: ts=4 sts=4 sw=4 tw=0 // Copyright 2007-2010 Baptiste Lepilleur // Distributed under MIT license, or public domain if desired and // recognized in your jurisdiction. @@ -1461,6 +1462,19 @@ ValueTest::checkIsEqual( const Json::Value &x, const Json::Value &y ) } +JSONTEST_FIXTURE( ValueTest, checkInteger ) +{ + try { + Json::Value x = 1; + x["key"]; // SIGABRT? + // regression for https://sourceforge.net/p/jsoncpp/bugs/67/ + JSONTEST_ASSERT( 0 ); + } catch (std::runtime_error const&) { + JSONTEST_ASSERT( 1 ); // good + } +} + + struct WriterTest : JsonTest::TestCase { }; @@ -1498,6 +1512,7 @@ int main( int argc, const char *argv[] ) JSONTEST_REGISTER_FIXTURE( runner, ValueTest, compareArray ); JSONTEST_REGISTER_FIXTURE( runner, ValueTest, compareObject ); JSONTEST_REGISTER_FIXTURE( runner, ValueTest, compareType ); + JSONTEST_REGISTER_FIXTURE( runner, ValueTest, checkInteger ); JSONTEST_REGISTER_FIXTURE( runner, WriterTest, dropNullPlaceholders ); return runner.runCommandLine( argc, argv ); }