mirror of
				https://github.com/open-source-parsers/jsoncpp.git
				synced 2025-10-22 08:02:06 +02:00 
			
		
		
		
	clarify errors
* use macros for logic errors, not input errors * throw on parsing failure in `operator>>()`, not assert * throw on malloc, not assert
This commit is contained in:
		| @@ -1994,7 +1994,7 @@ std::istream& operator>>(std::istream& sin, Value& root) { | ||||
|             "Error from reader: %s", | ||||
|             errs.c_str()); | ||||
|  | ||||
|     JSON_FAIL_MESSAGE("reader error"); | ||||
|     throw std::runtime_error("reader error"); | ||||
|   } | ||||
|   return sin; | ||||
| } | ||||
|   | ||||
| @@ -87,9 +87,11 @@ static inline char* duplicateStringValue(const char* value, | ||||
|     length = Value::maxInt - 1; | ||||
|  | ||||
|   char* newString = static_cast<char*>(malloc(length + 1)); | ||||
|   JSON_ASSERT_MESSAGE(newString != 0, | ||||
|                       "in Json::Value::duplicateStringValue(): " | ||||
|                       "Failed to allocate string value buffer"); | ||||
|   if (newString == NULL) { | ||||
|     throw std::runtime_error( | ||||
|         "in Json::Value::duplicateStringValue(): " | ||||
|         "Failed to allocate string value buffer"); | ||||
|   } | ||||
|   memcpy(newString, value, length); | ||||
|   newString[length] = 0; | ||||
|   return newString; | ||||
| @@ -108,9 +110,11 @@ static inline char* duplicateAndPrefixStringValue( | ||||
|                       "length too big for prefixing"); | ||||
|   unsigned actualLength = length + sizeof(unsigned) + 1U; | ||||
|   char* newString = static_cast<char*>(malloc(actualLength)); | ||||
|   JSON_ASSERT_MESSAGE(newString != 0, | ||||
|                       "in Json::Value::duplicateAndPrefixStringValue(): " | ||||
|                       "Failed to allocate string value buffer"); | ||||
|   if (newString == 0) { | ||||
|     throw std::runtime_error( | ||||
|         "in Json::Value::duplicateAndPrefixStringValue(): " | ||||
|         "Failed to allocate string value buffer"); | ||||
|   } | ||||
|   *reinterpret_cast<unsigned*>(newString) = length; | ||||
|   memcpy(newString + sizeof(unsigned), value, length); | ||||
|   newString[actualLength - 1U] = 0; // to avoid buffer over-run accidents by users later | ||||
|   | ||||
		Reference in New Issue
	
	Block a user
	 Christopher Dunn
					Christopher Dunn