mirror of
https://github.com/open-source-parsers/jsoncpp.git
synced 2025-05-22 04:34:02 +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:
parent
ee4ea0ec3f
commit
717b08695e
@ -13,6 +13,10 @@
|
|||||||
#include "config.h"
|
#include "config.h"
|
||||||
#endif // if !defined(JSON_IS_AMALGAMATION)
|
#endif // if !defined(JSON_IS_AMALGAMATION)
|
||||||
|
|
||||||
|
/** It should not be possible for a maliciously designed file to
|
||||||
|
* cause an abort() or seg-fault, so these macros are used only
|
||||||
|
* for pre-condition violations and internal logic errors.
|
||||||
|
*/
|
||||||
#if JSON_USE_EXCEPTION
|
#if JSON_USE_EXCEPTION
|
||||||
#include <stdexcept>
|
#include <stdexcept>
|
||||||
#define JSON_ASSERT(condition) \
|
#define JSON_ASSERT(condition) \
|
||||||
@ -27,7 +31,7 @@
|
|||||||
#define JSON_ASSERT(condition) assert(condition)
|
#define JSON_ASSERT(condition) assert(condition)
|
||||||
|
|
||||||
// The call to assert() will show the failure message in debug builds. In
|
// The call to assert() will show the failure message in debug builds. In
|
||||||
// release bugs we abort, for a core-dump or debugger.
|
// release builds we abort, for a core-dump or debugger.
|
||||||
#define JSON_FAIL_MESSAGE(message) \
|
#define JSON_FAIL_MESSAGE(message) \
|
||||||
{ \
|
{ \
|
||||||
std::ostringstream oss; oss << message; \
|
std::ostringstream oss; oss << message; \
|
||||||
|
@ -46,7 +46,7 @@ public:
|
|||||||
/** Write Value into document as configured in sub-class.
|
/** Write Value into document as configured in sub-class.
|
||||||
Do not take ownership of sout, but maintain a reference during function.
|
Do not take ownership of sout, but maintain a reference during function.
|
||||||
\pre sout != NULL
|
\pre sout != NULL
|
||||||
\return zero on success
|
\return zero on success (For now, we always return zero, so check the stream instead.)
|
||||||
\throw std::exception possibly, depending on configuration
|
\throw std::exception possibly, depending on configuration
|
||||||
*/
|
*/
|
||||||
virtual int write(Value const& root, std::ostream* sout) = 0;
|
virtual int write(Value const& root, std::ostream* sout) = 0;
|
||||||
|
@ -1994,7 +1994,7 @@ std::istream& operator>>(std::istream& sin, Value& root) {
|
|||||||
"Error from reader: %s",
|
"Error from reader: %s",
|
||||||
errs.c_str());
|
errs.c_str());
|
||||||
|
|
||||||
JSON_FAIL_MESSAGE("reader error");
|
throw std::runtime_error("reader error");
|
||||||
}
|
}
|
||||||
return sin;
|
return sin;
|
||||||
}
|
}
|
||||||
|
@ -87,9 +87,11 @@ static inline char* duplicateStringValue(const char* value,
|
|||||||
length = Value::maxInt - 1;
|
length = Value::maxInt - 1;
|
||||||
|
|
||||||
char* newString = static_cast<char*>(malloc(length + 1));
|
char* newString = static_cast<char*>(malloc(length + 1));
|
||||||
JSON_ASSERT_MESSAGE(newString != 0,
|
if (newString == NULL) {
|
||||||
|
throw std::runtime_error(
|
||||||
"in Json::Value::duplicateStringValue(): "
|
"in Json::Value::duplicateStringValue(): "
|
||||||
"Failed to allocate string value buffer");
|
"Failed to allocate string value buffer");
|
||||||
|
}
|
||||||
memcpy(newString, value, length);
|
memcpy(newString, value, length);
|
||||||
newString[length] = 0;
|
newString[length] = 0;
|
||||||
return newString;
|
return newString;
|
||||||
@ -108,9 +110,11 @@ static inline char* duplicateAndPrefixStringValue(
|
|||||||
"length too big for prefixing");
|
"length too big for prefixing");
|
||||||
unsigned actualLength = length + sizeof(unsigned) + 1U;
|
unsigned actualLength = length + sizeof(unsigned) + 1U;
|
||||||
char* newString = static_cast<char*>(malloc(actualLength));
|
char* newString = static_cast<char*>(malloc(actualLength));
|
||||||
JSON_ASSERT_MESSAGE(newString != 0,
|
if (newString == 0) {
|
||||||
|
throw std::runtime_error(
|
||||||
"in Json::Value::duplicateAndPrefixStringValue(): "
|
"in Json::Value::duplicateAndPrefixStringValue(): "
|
||||||
"Failed to allocate string value buffer");
|
"Failed to allocate string value buffer");
|
||||||
|
}
|
||||||
*reinterpret_cast<unsigned*>(newString) = length;
|
*reinterpret_cast<unsigned*>(newString) = length;
|
||||||
memcpy(newString + sizeof(unsigned), value, length);
|
memcpy(newString + sizeof(unsigned), value, length);
|
||||||
newString[actualLength - 1U] = 0; // to avoid buffer over-run accidents by users later
|
newString[actualLength - 1U] = 0; // to avoid buffer over-run accidents by users later
|
||||||
|
Loading…
x
Reference in New Issue
Block a user