Skip to content

Commit ea07973

Browse files
committed
JSON_ASSERT -> JSON_ASSERT_MESSAGE
This way, assertions can produce exceptions. https://sourceforge.net/p/jsoncpp/bugs/67/
1 parent 94d17e9 commit ea07973

2 files changed

Lines changed: 29 additions & 13 deletions

File tree

src/lib_json/json_value.cpp

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// vim: ts=3 sts=3 sw=3 tw=0
12
// Copyright 2011 Baptiste Lepilleur
23
// Distributed under MIT license, or public domain if desired and
34
// recognized in your jurisdiction.
@@ -91,7 +92,7 @@ duplicateStringValue( const char *value,
9192
length = Value::maxInt - 1;
9293

9394
char *newString = static_cast<char *>( malloc( length + 1 ) );
94-
JSON_ASSERT_MESSAGE( newString != 0, "Failed to allocate string value buffer" );
95+
JSON_ASSERT_MESSAGE( newString != 0, "in Json::Value::duplicateStringValue(): Failed to allocate string value buffer" );
9596
memcpy( newString, value, length );
9697
newString[length] = 0;
9798
return newString;
@@ -155,7 +156,7 @@ Value::CommentInfo::setComment( const char *text )
155156
if ( comment_ )
156157
releaseStringValue( comment_ );
157158
JSON_ASSERT( text != 0 );
158-
JSON_ASSERT_MESSAGE( text[0]=='\0' || text[0]=='/', "Comments must start with /");
159+
JSON_ASSERT_MESSAGE( text[0]=='\0' || text[0]=='/', "in Json::Value::setComment(): Comments must start with /");
159160
// It seems that /**/ style comments are acceptable as well.
160161
comment_ = duplicateStringValue( text );
161162
}
@@ -692,7 +693,7 @@ Value::operator !=( const Value &other ) const
692693
const char *
693694
Value::asCString() const
694695
{
695-
JSON_ASSERT( type_ == stringValue );
696+
JSON_ASSERT_MESSAGE( type_ == stringValue, "in Json::Value::asCString(): requires stringValue" );
696697
return value_.string_;
697698
}
698699

@@ -1026,7 +1027,7 @@ Value::operator!() const
10261027
void
10271028
Value::clear()
10281029
{
1029-
JSON_ASSERT( type_ == nullValue || type_ == arrayValue || type_ == objectValue );
1030+
JSON_ASSERT_MESSAGE( type_ == nullValue || type_ == arrayValue || type_ == objectValue, "in Json::Value::clear(): requires complex value" );
10301031

10311032
switch ( type_ )
10321033
{
@@ -1051,7 +1052,7 @@ Value::clear()
10511052
void
10521053
Value::resize( ArrayIndex newSize )
10531054
{
1054-
JSON_ASSERT( type_ == nullValue || type_ == arrayValue );
1055+
JSON_ASSERT_MESSAGE( type_ == nullValue || type_ == arrayValue, "in Json::Value::resize(): requires arrayValue" );
10551056
if ( type_ == nullValue )
10561057
*this = Value( arrayValue );
10571058
#ifndef JSON_VALUE_USE_INTERNAL_MAP
@@ -1077,7 +1078,7 @@ Value::resize( ArrayIndex newSize )
10771078
Value &
10781079
Value::operator[]( ArrayIndex index )
10791080
{
1080-
JSON_ASSERT( type_ == nullValue || type_ == arrayValue );
1081+
JSON_ASSERT_MESSAGE( type_ == nullValue || type_ == arrayValue, "in Json::Value::operator[](ArrayIndex): requires arrayValue" );
10811082
if ( type_ == nullValue )
10821083
*this = Value( arrayValue );
10831084
#ifndef JSON_VALUE_USE_INTERNAL_MAP
@@ -1098,15 +1099,15 @@ Value::operator[]( ArrayIndex index )
10981099
Value &
10991100
Value::operator[]( int index )
11001101
{
1101-
JSON_ASSERT( index >= 0 );
1102+
JSON_ASSERT_MESSAGE( index >= 0, "in Json::Value::operator[](int index): index cannot be negative" );
11021103
return (*this)[ ArrayIndex(index) ];
11031104
}
11041105

11051106

11061107
const Value &
11071108
Value::operator[]( ArrayIndex index ) const
11081109
{
1109-
JSON_ASSERT( type_ == nullValue || type_ == arrayValue );
1110+
JSON_ASSERT_MESSAGE( type_ == nullValue || type_ == arrayValue, "in Json::Value::operator[](ArrayIndex)const: requires arrayValue" );
11101111
if ( type_ == nullValue )
11111112
return null;
11121113
#ifndef JSON_VALUE_USE_INTERNAL_MAP
@@ -1125,7 +1126,7 @@ Value::operator[]( ArrayIndex index ) const
11251126
const Value &
11261127
Value::operator[]( int index ) const
11271128
{
1128-
JSON_ASSERT( index >= 0 );
1129+
JSON_ASSERT_MESSAGE( index >= 0, "in Json::Value::operator[](int index) const: index cannot be negative" );
11291130
return (*this)[ ArrayIndex(index) ];
11301131
}
11311132

@@ -1141,7 +1142,7 @@ Value &
11411142
Value::resolveReference( const char *key,
11421143
bool isStatic )
11431144
{
1144-
JSON_ASSERT( type_ == nullValue || type_ == objectValue );
1145+
JSON_ASSERT_MESSAGE( type_ == nullValue || type_ == objectValue, "in Json::Value::resolveReference(): requires objectValue" );
11451146
if ( type_ == nullValue )
11461147
*this = Value( objectValue );
11471148
#ifndef JSON_VALUE_USE_INTERNAL_MAP
@@ -1181,7 +1182,7 @@ Value::isValidIndex( ArrayIndex index ) const
11811182
const Value &
11821183
Value::operator[]( const char *key ) const
11831184
{
1184-
JSON_ASSERT( type_ == nullValue || type_ == objectValue );
1185+
JSON_ASSERT_MESSAGE( type_ == nullValue || type_ == objectValue, "in Json::Value::operator[](char const*)const: requires objectValue" );
11851186
if ( type_ == nullValue )
11861187
return null;
11871188
#ifndef JSON_VALUE_USE_INTERNAL_MAP
@@ -1259,7 +1260,7 @@ Value::get( const std::string &key,
12591260
Value
12601261
Value::removeMember( const char* key )
12611262
{
1262-
JSON_ASSERT( type_ == nullValue || type_ == objectValue );
1263+
JSON_ASSERT_MESSAGE( type_ == nullValue || type_ == objectValue, "in Json::Value::removeMember(): requires objectValue" );
12631264
if ( type_ == nullValue )
12641265
return null;
12651266
#ifndef JSON_VALUE_USE_INTERNAL_MAP
@@ -1323,7 +1324,7 @@ Value::isMember( const CppTL::ConstString &key ) const
13231324
Value::Members
13241325
Value::getMemberNames() const
13251326
{
1326-
JSON_ASSERT( type_ == nullValue || type_ == objectValue );
1327+
JSON_ASSERT_MESSAGE( type_ == nullValue || type_ == objectValue, "in Json::Value::getMemberNames(), value must be objectValue" );
13271328
if ( type_ == nullValue )
13281329
return Value::Members();
13291330
Members members;

src/test_lib_json/main.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// vim: ts=4 sts=4 sw=4 tw=0
12
// Copyright 2007-2010 Baptiste Lepilleur
23
// Distributed under MIT license, or public domain if desired and
34
// recognized in your jurisdiction.
@@ -1461,6 +1462,19 @@ ValueTest::checkIsEqual( const Json::Value &x, const Json::Value &y )
14611462
}
14621463

14631464

1465+
JSONTEST_FIXTURE( ValueTest, checkInteger )
1466+
{
1467+
try {
1468+
Json::Value x = 1;
1469+
x["key"]; // SIGABRT?
1470+
// regression for https://sourceforge.net/p/jsoncpp/bugs/67/
1471+
JSONTEST_ASSERT( 0 );
1472+
} catch (std::runtime_error const&) {
1473+
JSONTEST_ASSERT( 1 ); // good
1474+
}
1475+
}
1476+
1477+
14641478
struct WriterTest : JsonTest::TestCase
14651479
{
14661480
};
@@ -1498,6 +1512,7 @@ int main( int argc, const char *argv[] )
14981512
JSONTEST_REGISTER_FIXTURE( runner, ValueTest, compareArray );
14991513
JSONTEST_REGISTER_FIXTURE( runner, ValueTest, compareObject );
15001514
JSONTEST_REGISTER_FIXTURE( runner, ValueTest, compareType );
1515+
JSONTEST_REGISTER_FIXTURE( runner, ValueTest, checkInteger );
15011516
JSONTEST_REGISTER_FIXTURE( runner, WriterTest, dropNullPlaceholders );
15021517
return runner.runCommandLine( argc, argv );
15031518
}

0 commit comments

Comments
 (0)