Skip to content

Commit 717b086

Browse files
committed
clarify errors
* use macros for logic errors, not input errors * throw on parsing failure in `operator>>()`, not assert * throw on malloc, not assert
1 parent ee4ea0e commit 717b086

File tree

4 files changed

+17
-9
lines changed

4 files changed

+17
-9
lines changed

include/json/assertions.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@
1313
#include "config.h"
1414
#endif // if !defined(JSON_IS_AMALGAMATION)
1515

16+
/** It should not be possible for a maliciously designed file to
17+
* cause an abort() or seg-fault, so these macros are used only
18+
* for pre-condition violations and internal logic errors.
19+
*/
1620
#if JSON_USE_EXCEPTION
1721
#include <stdexcept>
1822
#define JSON_ASSERT(condition) \
@@ -27,7 +31,7 @@
2731
#define JSON_ASSERT(condition) assert(condition)
2832

2933
// The call to assert() will show the failure message in debug builds. In
30-
// release bugs we abort, for a core-dump or debugger.
34+
// release builds we abort, for a core-dump or debugger.
3135
#define JSON_FAIL_MESSAGE(message) \
3236
{ \
3337
std::ostringstream oss; oss << message; \

include/json/writer.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ class JSON_API StreamWriter {
4646
/** Write Value into document as configured in sub-class.
4747
Do not take ownership of sout, but maintain a reference during function.
4848
\pre sout != NULL
49-
\return zero on success
49+
\return zero on success (For now, we always return zero, so check the stream instead.)
5050
\throw std::exception possibly, depending on configuration
5151
*/
5252
virtual int write(Value const& root, std::ostream* sout) = 0;

src/lib_json/json_reader.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1994,7 +1994,7 @@ std::istream& operator>>(std::istream& sin, Value& root) {
19941994
"Error from reader: %s",
19951995
errs.c_str());
19961996

1997-
JSON_FAIL_MESSAGE("reader error");
1997+
throw std::runtime_error("reader error");
19981998
}
19991999
return sin;
20002000
}

src/lib_json/json_value.cpp

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -87,9 +87,11 @@ static inline char* duplicateStringValue(const char* value,
8787
length = Value::maxInt - 1;
8888

8989
char* newString = static_cast<char*>(malloc(length + 1));
90-
JSON_ASSERT_MESSAGE(newString != 0,
91-
"in Json::Value::duplicateStringValue(): "
92-
"Failed to allocate string value buffer");
90+
if (newString == NULL) {
91+
throw std::runtime_error(
92+
"in Json::Value::duplicateStringValue(): "
93+
"Failed to allocate string value buffer");
94+
}
9395
memcpy(newString, value, length);
9496
newString[length] = 0;
9597
return newString;
@@ -108,9 +110,11 @@ static inline char* duplicateAndPrefixStringValue(
108110
"length too big for prefixing");
109111
unsigned actualLength = length + sizeof(unsigned) + 1U;
110112
char* newString = static_cast<char*>(malloc(actualLength));
111-
JSON_ASSERT_MESSAGE(newString != 0,
112-
"in Json::Value::duplicateAndPrefixStringValue(): "
113-
"Failed to allocate string value buffer");
113+
if (newString == 0) {
114+
throw std::runtime_error(
115+
"in Json::Value::duplicateAndPrefixStringValue(): "
116+
"Failed to allocate string value buffer");
117+
}
114118
*reinterpret_cast<unsigned*>(newString) = length;
115119
memcpy(newString + sizeof(unsigned), value, length);
116120
newString[actualLength - 1U] = 0; // to avoid buffer over-run accidents by users later

0 commit comments

Comments
 (0)