Skip to content

Commit

Permalink
Merge pull request #2242 from nlohmann/issue2239
Browse files Browse the repository at this point in the history
Make assert configurable via JSON_ASSERT
  • Loading branch information
nlohmann committed Jul 9, 2020
2 parents d740622 + f774a32 commit 4c7bd01
Show file tree
Hide file tree
Showing 19 changed files with 343 additions and 277 deletions.
2 changes: 2 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ doctest:
# -Wno-exit-time-destructors: warning in json code triggered by NLOHMANN_JSON_SERIALIZE_ENUM
# -Wno-float-equal: not all comparisons in the tests can be replaced by Approx
# -Wno-keyword-macro: unit-tests use "#define private public"
# -Wno-missing-prototypes: for NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE
# -Wno-padded: padding is nothing to warn about
# -Wno-range-loop-analysis: items tests "for(const auto i...)"
# -Wno-switch-enum -Wno-covered-switch-default: pedantic/contradicting warnings about switches
Expand All @@ -113,6 +114,7 @@ pedantic_clang:
-Wno-exit-time-destructors \
-Wno-float-equal \
-Wno-keyword-macro \
-Wno-missing-prototypes \
-Wno-padded \
-Wno-range-loop-analysis \
-Wno-switch-enum -Wno-covered-switch-default \
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1572,7 +1572,7 @@ Here is a related issue [#1924](https://github.com/nlohmann/json/issues/1924).

### Further notes

- The code contains numerous debug **assertions** which can be switched off by defining the preprocessor macro `NDEBUG`, see the [documentation of `assert`](https://en.cppreference.com/w/cpp/error/assert). In particular, note [`operator[]`](https://nlohmann.github.io/json/classnlohmann_1_1basic__json_a233b02b0839ef798942dd46157cc0fe6.html#a233b02b0839ef798942dd46157cc0fe6) implements **unchecked access** for const objects: If the given key is not present, the behavior is undefined (think of a dereferenced null pointer) and yields an [assertion failure](https://github.com/nlohmann/json/issues/289) if assertions are switched on. If you are not sure whether an element in an object exists, use checked access with the [`at()` function](https://nlohmann.github.io/json/classnlohmann_1_1basic__json_a73ae333487310e3302135189ce8ff5d8.html#a73ae333487310e3302135189ce8ff5d8).
- The code contains numerous debug **assertions** which can be switched off by defining the preprocessor macro `NDEBUG`, see the [documentation of `assert`](https://en.cppreference.com/w/cpp/error/assert). In particular, note [`operator[]`](https://nlohmann.github.io/json/classnlohmann_1_1basic__json_a233b02b0839ef798942dd46157cc0fe6.html#a233b02b0839ef798942dd46157cc0fe6) implements **unchecked access** for const objects: If the given key is not present, the behavior is undefined (think of a dereferenced null pointer) and yields an [assertion failure](https://github.com/nlohmann/json/issues/289) if assertions are switched on. If you are not sure whether an element in an object exists, use checked access with the [`at()` function](https://nlohmann.github.io/json/classnlohmann_1_1basic__json_a73ae333487310e3302135189ce8ff5d8.html#a73ae333487310e3302135189ce8ff5d8). Furthermore, you can define `JSON_ASSERT(x)` to replace calls to `assert(x)`.
- As the exact type of a number is not defined in the [JSON specification](https://tools.ietf.org/html/rfc8259.html), this library tries to choose the best fitting C++ number type automatically. As a result, the type `double` may be used to store numbers which may yield [**floating-point exceptions**](https://github.com/nlohmann/json/issues/181) in certain rare situations if floating-point exceptions have been unmasked in the calling code. These exceptions are not caused by the library and need to be fixed in the calling code, such as by re-masking the exceptions prior to calling library functions.
- The code can be compiled without C++ **runtime type identification** features; that is, you can use the `-fno-rtti` compiler flag.
- **Exceptions** are used widely within the library. They can, however, be switched off with either using the compiler flag `-fno-exceptions` or by defining the symbol `JSON_NOEXCEPTION`. In this case, exceptions are replaced by `abort()` calls. You can further control this behavior by defining `JSON_THROW_USER´` (overriding `throw`), `JSON_TRY_USER` (overriding `try`), and `JSON_CATCH_USER` (overriding `catch`). Note that `JSON_THROW_USER` should leave the current scope (e.g., by throwing or aborting), as continuing after it may yield undefined behavior.
Expand Down
81 changes: 40 additions & 41 deletions include/nlohmann/detail/conversions/to_chars.hpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#pragma once

#include <array> // array
#include <cassert> // assert
#include <cmath> // signbit, isfinite
#include <cstdint> // intN_t, uintN_t
#include <cstring> // memcpy, memmove
Expand Down Expand Up @@ -63,8 +62,8 @@ struct diyfp // f * 2^e
*/
static diyfp sub(const diyfp& x, const diyfp& y) noexcept
{
assert(x.e == y.e);
assert(x.f >= y.f);
JSON_ASSERT(x.e == y.e);
JSON_ASSERT(x.f >= y.f);

return {x.f - y.f, x.e};
}
Expand Down Expand Up @@ -140,7 +139,7 @@ struct diyfp // f * 2^e
*/
static diyfp normalize(diyfp x) noexcept
{
assert(x.f != 0);
JSON_ASSERT(x.f != 0);

while ((x.f >> 63u) == 0)
{
Expand All @@ -159,8 +158,8 @@ struct diyfp // f * 2^e
{
const int delta = x.e - target_exponent;

assert(delta >= 0);
assert(((x.f << delta) >> delta) == x.f);
JSON_ASSERT(delta >= 0);
JSON_ASSERT(((x.f << delta) >> delta) == x.f);

return {x.f << delta, target_exponent};
}
Expand All @@ -182,8 +181,8 @@ boundaries.
template <typename FloatType>
boundaries compute_boundaries(FloatType value)
{
assert(std::isfinite(value));
assert(value > 0);
JSON_ASSERT(std::isfinite(value));
JSON_ASSERT(value > 0);

// Convert the IEEE representation into a diyfp.
//
Expand Down Expand Up @@ -463,18 +462,18 @@ inline cached_power get_cached_power_for_binary_exponent(int e)
// k = ceil((kAlpha - e - 1) * 0.30102999566398114)
// for |e| <= 1500, but doesn't require floating-point operations.
// NB: log_10(2) ~= 78913 / 2^18
assert(e >= -1500);
assert(e <= 1500);
JSON_ASSERT(e >= -1500);
JSON_ASSERT(e <= 1500);
const int f = kAlpha - e - 1;
const int k = (f * 78913) / (1 << 18) + static_cast<int>(f > 0);

const int index = (-kCachedPowersMinDecExp + k + (kCachedPowersDecStep - 1)) / kCachedPowersDecStep;
assert(index >= 0);
assert(static_cast<std::size_t>(index) < kCachedPowers.size());
JSON_ASSERT(index >= 0);
JSON_ASSERT(static_cast<std::size_t>(index) < kCachedPowers.size());

const cached_power cached = kCachedPowers[static_cast<std::size_t>(index)];
assert(kAlpha <= cached.e + e + 64);
assert(kGamma >= cached.e + e + 64);
JSON_ASSERT(kAlpha <= cached.e + e + 64);
JSON_ASSERT(kGamma >= cached.e + e + 64);

return cached;
}
Expand Down Expand Up @@ -542,10 +541,10 @@ inline int find_largest_pow10(const std::uint32_t n, std::uint32_t& pow10)
inline void grisu2_round(char* buf, int len, std::uint64_t dist, std::uint64_t delta,
std::uint64_t rest, std::uint64_t ten_k)
{
assert(len >= 1);
assert(dist <= delta);
assert(rest <= delta);
assert(ten_k > 0);
JSON_ASSERT(len >= 1);
JSON_ASSERT(dist <= delta);
JSON_ASSERT(rest <= delta);
JSON_ASSERT(ten_k > 0);

// <--------------------------- delta ---->
// <---- dist --------->
Expand All @@ -570,7 +569,7 @@ inline void grisu2_round(char* buf, int len, std::uint64_t dist, std::uint64_t d
and delta - rest >= ten_k
and (rest + ten_k < dist or dist - rest > rest + ten_k - dist))
{
assert(buf[len - 1] != '0');
JSON_ASSERT(buf[len - 1] != '0');
buf[len - 1]--;
rest += ten_k;
}
Expand Down Expand Up @@ -598,8 +597,8 @@ inline void grisu2_digit_gen(char* buffer, int& length, int& decimal_exponent,
// Grisu2 generates the digits of M+ from left to right and stops as soon as
// V is in [M-,M+].

assert(M_plus.e >= kAlpha);
assert(M_plus.e <= kGamma);
JSON_ASSERT(M_plus.e >= kAlpha);
JSON_ASSERT(M_plus.e <= kGamma);

std::uint64_t delta = diyfp::sub(M_plus, M_minus).f; // (significand of (M+ - M-), implicit exponent is e)
std::uint64_t dist = diyfp::sub(M_plus, w ).f; // (significand of (M+ - w ), implicit exponent is e)
Expand All @@ -620,7 +619,7 @@ inline void grisu2_digit_gen(char* buffer, int& length, int& decimal_exponent,
//
// Generate the digits of the integral part p1 = d[n-1]...d[1]d[0]

assert(p1 > 0);
JSON_ASSERT(p1 > 0);

std::uint32_t pow10;
const int k = find_largest_pow10(p1, pow10);
Expand Down Expand Up @@ -656,7 +655,7 @@ inline void grisu2_digit_gen(char* buffer, int& length, int& decimal_exponent,
// M+ = buffer * 10^n + (d * 10^(n-1) + r) + p2 * 2^e
// = (buffer * 10 + d) * 10^(n-1) + (r + p2 * 2^e)
//
assert(d <= 9);
JSON_ASSERT(d <= 9);
buffer[length++] = static_cast<char>('0' + d); // buffer := buffer * 10 + d
//
// M+ = buffer * 10^(n-1) + (r + p2 * 2^e)
Expand Down Expand Up @@ -743,7 +742,7 @@ inline void grisu2_digit_gen(char* buffer, int& length, int& decimal_exponent,
//
// and stop as soon as 10^-m * r * 2^e <= delta * 2^e

assert(p2 > delta);
JSON_ASSERT(p2 > delta);

int m = 0;
for (;;)
Expand All @@ -754,7 +753,7 @@ inline void grisu2_digit_gen(char* buffer, int& length, int& decimal_exponent,
// = buffer * 10^-m + 10^-m * (1/10 * (10 * p2) ) * 2^e
// = buffer * 10^-m + 10^-m * (1/10 * ((10*p2 div 2^-e) * 2^-e + (10*p2 mod 2^-e)) * 2^e
//
assert(p2 <= (std::numeric_limits<std::uint64_t>::max)() / 10);
JSON_ASSERT(p2 <= (std::numeric_limits<std::uint64_t>::max)() / 10);
p2 *= 10;
const std::uint64_t d = p2 >> -one.e; // d = (10 * p2) div 2^-e
const std::uint64_t r = p2 & (one.f - 1); // r = (10 * p2) mod 2^-e
Expand All @@ -763,7 +762,7 @@ inline void grisu2_digit_gen(char* buffer, int& length, int& decimal_exponent,
// = buffer * 10^-m + 10^-m * (1/10 * (d + r * 2^e))
// = (buffer * 10 + d) * 10^(-m-1) + 10^(-m-1) * r * 2^e
//
assert(d <= 9);
JSON_ASSERT(d <= 9);
buffer[length++] = static_cast<char>('0' + d); // buffer := buffer * 10 + d
//
// M+ = buffer * 10^(-m-1) + 10^(-m-1) * r * 2^e
Expand Down Expand Up @@ -824,8 +823,8 @@ JSON_HEDLEY_NON_NULL(1)
inline void grisu2(char* buf, int& len, int& decimal_exponent,
diyfp m_minus, diyfp v, diyfp m_plus)
{
assert(m_plus.e == m_minus.e);
assert(m_plus.e == v.e);
JSON_ASSERT(m_plus.e == m_minus.e);
JSON_ASSERT(m_plus.e == v.e);

// --------(-----------------------+-----------------------)-------- (A)
// m- v m+
Expand Down Expand Up @@ -886,8 +885,8 @@ void grisu2(char* buf, int& len, int& decimal_exponent, FloatType value)
static_assert(diyfp::kPrecision >= std::numeric_limits<FloatType>::digits + 3,
"internal error: not enough precision");

assert(std::isfinite(value));
assert(value > 0);
JSON_ASSERT(std::isfinite(value));
JSON_ASSERT(value > 0);

// If the neighbors (and boundaries) of 'value' are always computed for double-precision
// numbers, all float's can be recovered using strtod (and strtof). However, the resulting
Expand Down Expand Up @@ -923,8 +922,8 @@ JSON_HEDLEY_NON_NULL(1)
JSON_HEDLEY_RETURNS_NON_NULL
inline char* append_exponent(char* buf, int e)
{
assert(e > -1000);
assert(e < 1000);
JSON_ASSERT(e > -1000);
JSON_ASSERT(e < 1000);

if (e < 0)
{
Expand Down Expand Up @@ -976,8 +975,8 @@ JSON_HEDLEY_RETURNS_NON_NULL
inline char* format_buffer(char* buf, int len, int decimal_exponent,
int min_exp, int max_exp)
{
assert(min_exp < 0);
assert(max_exp > 0);
JSON_ASSERT(min_exp < 0);
JSON_ASSERT(max_exp > 0);

const int k = len;
const int n = len + decimal_exponent;
Expand All @@ -1003,7 +1002,7 @@ inline char* format_buffer(char* buf, int len, int decimal_exponent,
// dig.its
// len <= max_digits10 + 1

assert(k > n);
JSON_ASSERT(k > n);

std::memmove(buf + (static_cast<size_t>(n) + 1), buf + n, static_cast<size_t>(k) - static_cast<size_t>(n));
buf[n] = '.';
Expand Down Expand Up @@ -1061,7 +1060,7 @@ JSON_HEDLEY_RETURNS_NON_NULL
char* to_chars(char* first, const char* last, FloatType value)
{
static_cast<void>(last); // maybe unused - fix warning
assert(std::isfinite(value));
JSON_ASSERT(std::isfinite(value));

// Use signbit(value) instead of (value < 0) since signbit works for -0.
if (std::signbit(value))
Expand All @@ -1079,7 +1078,7 @@ char* to_chars(char* first, const char* last, FloatType value)
return first;
}

assert(last - first >= std::numeric_limits<FloatType>::max_digits10);
JSON_ASSERT(last - first >= std::numeric_limits<FloatType>::max_digits10);

// Compute v = buffer * 10^decimal_exponent.
// The decimal digits are stored in the buffer, which needs to be interpreted
Expand All @@ -1089,16 +1088,16 @@ char* to_chars(char* first, const char* last, FloatType value)
int decimal_exponent = 0;
dtoa_impl::grisu2(first, len, decimal_exponent, value);

assert(len <= std::numeric_limits<FloatType>::max_digits10);
JSON_ASSERT(len <= std::numeric_limits<FloatType>::max_digits10);

// Format the buffer like printf("%.*g", prec, value)
constexpr int kMinExp = -4;
// Use digits10 here to increase compatibility with version 2.
constexpr int kMaxExp = std::numeric_limits<FloatType>::digits10;

assert(last - first >= kMaxExp + 2);
assert(last - first >= 2 + (-kMinExp - 1) + std::numeric_limits<FloatType>::max_digits10);
assert(last - first >= std::numeric_limits<FloatType>::max_digits10 + 6);
JSON_ASSERT(last - first >= kMaxExp + 2);
JSON_ASSERT(last - first >= 2 + (-kMinExp - 1) + std::numeric_limits<FloatType>::max_digits10);
JSON_ASSERT(last - first >= std::numeric_limits<FloatType>::max_digits10 + 6);

return dtoa_impl::format_buffer(first, len, decimal_exponent, kMinExp, kMaxExp);
}
Expand Down
9 changes: 4 additions & 5 deletions include/nlohmann/detail/input/binary_reader.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

#include <algorithm> // generate_n
#include <array> // array
#include <cassert> // assert
#include <cmath> // ldexp
#include <cstddef> // size_t
#include <cstdint> // uint8_t, uint16_t, uint32_t, uint64_t
Expand Down Expand Up @@ -109,7 +108,7 @@ class binary_reader
break;

default: // LCOV_EXCL_LINE
assert(false); // LCOV_EXCL_LINE
JSON_ASSERT(false); // LCOV_EXCL_LINE
}

// strict mode: next byte must be EOF
Expand Down Expand Up @@ -717,8 +716,8 @@ class binary_reader
{
const int exp = (half >> 10u) & 0x1Fu;
const unsigned int mant = half & 0x3FFu;
assert(0 <= exp and exp <= 32);
assert(mant <= 1024);
JSON_ASSERT(0 <= exp and exp <= 32);
JSON_ASSERT(mant <= 1024);
switch (exp)
{
case 0:
Expand Down Expand Up @@ -2295,7 +2294,7 @@ class binary_reader
break;

default: // LCOV_EXCL_LINE
assert(false); // LCOV_EXCL_LINE
JSON_ASSERT(false); // LCOV_EXCL_LINE
}

return error_msg + " " + context + ": " + detail;
Expand Down
9 changes: 4 additions & 5 deletions include/nlohmann/detail/input/input_adapters.hpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#pragma once

#include <array> // array
#include <cassert> // assert
#include <cstddef> // size_t
#include <cstdio> //FILE *
#include <cstring> // strlen
Expand Down Expand Up @@ -297,13 +296,13 @@ class wide_string_input_adapter
{
fill_buffer<sizeof(WideCharType)>();

assert(utf8_bytes_filled > 0);
assert(utf8_bytes_index == 0);
JSON_ASSERT(utf8_bytes_filled > 0);
JSON_ASSERT(utf8_bytes_index == 0);
}

// use buffer
assert(utf8_bytes_filled > 0);
assert(utf8_bytes_index < utf8_bytes_filled);
JSON_ASSERT(utf8_bytes_filled > 0);
JSON_ASSERT(utf8_bytes_index < utf8_bytes_filled);
return utf8_bytes[utf8_bytes_index++];
}

Expand Down
Loading

0 comments on commit 4c7bd01

Please sign in to comment.