JSON_ASSERT -> JSON_ASSERT_MESSAGE

This way, assertions can produce exceptions.
https://sourceforge.net/p/jsoncpp/bugs/67/



git-svn-id: http://jsoncpp.svn.sourceforge.net/svnroot/jsoncpp/trunk/jsoncpp@277 1f120ed1-78a5-a849-adca-83f0a9e25bb6
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 @@
       length = Value::maxInt - 1;
 
    char *newString = static_cast<char *>( 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 @@
    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 @@
 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 @@
 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 @@
 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 &
 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 &
 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 @@
 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 @@
 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::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 @@
 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
 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::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 @@
 }
 
 
+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 @@
    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 );
 }