diff options
Diffstat (limited to 'include/json/assertions.h')
-rw-r--r-- | include/json/assertions.h | 56 |
1 files changed, 38 insertions, 18 deletions
diff --git a/include/json/assertions.h b/include/json/assertions.h index 5ef7e7b..666fa7f 100644 --- a/include/json/assertions.h +++ b/include/json/assertions.h @@ -1,41 +1,61 @@ -// Copyright 2007-2010 Baptiste Lepilleur +// Copyright 2007-2010 Baptiste Lepilleur and The JsonCpp Authors // Distributed under MIT license, or public domain if desired and // recognized in your jurisdiction. // See file LICENSE for detail or copy at http://jsoncpp.sourceforge.net/LICENSE -#ifndef CPPTL_JSON_ASSERTIONS_H_INCLUDED -#define CPPTL_JSON_ASSERTIONS_H_INCLUDED +#ifndef JSON_ASSERTIONS_H_INCLUDED +#define JSON_ASSERTIONS_H_INCLUDED -#include <stdlib.h> +#include <cstdlib> +#include <sstream> #if !defined(JSON_IS_AMALGAMATION) #include "config.h" #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 -#include <stdexcept> + +// @todo <= add detail about condition in exception #define JSON_ASSERT(condition) \ - assert(condition); // @todo <= change this into an exception throw -#define JSON_FAIL_MESSAGE(message) throw std::runtime_error(message); + do { \ + if (!(condition)) { \ + Json::throwLogicError("assert json failed"); \ + } \ + } while (0) + +#define JSON_FAIL_MESSAGE(message) \ + do { \ + OStringStream oss; \ + oss << message; \ + Json::throwLogicError(oss.str()); \ + abort(); \ + } while (0) + #else // JSON_USE_EXCEPTION -#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 -// release bugs we write to invalid memory in order to crash hard, so that a -// debugger or crash reporter gets the chance to take over. We still call exit() -// afterward in order to tell the compiler that this macro doesn't return. +// release builds we abort, for a core-dump or debugger. #define JSON_FAIL_MESSAGE(message) \ { \ - assert(false&& message); \ - strcpy(reinterpret_cast<char*>(666), message); \ - exit(123); \ + OStringStream oss; \ + oss << message; \ + assert(false && oss.str().c_str()); \ + abort(); \ } #endif #define JSON_ASSERT_MESSAGE(condition, message) \ - if (!(condition)) { \ - JSON_FAIL_MESSAGE(message) \ - } + do { \ + if (!(condition)) { \ + JSON_FAIL_MESSAGE(message); \ + } \ + } while (0) -#endif // CPPTL_JSON_ASSERTIONS_H_INCLUDED +#endif // JSON_ASSERTIONS_H_INCLUDED |