From 1c030cccf17f4079c06ac5977ce8936a5776f5f4 Mon Sep 17 00:00:00 2001 From: Niels Lohmann Date: Thu, 29 Jul 2021 15:23:21 +0200 Subject: [PATCH 01/13] :white_check_mark: add regression test #2824 --- test/src/unit-regression2.cpp | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/test/src/unit-regression2.cpp b/test/src/unit-regression2.cpp index fa5de3179..96bf6c872 100644 --- a/test/src/unit-regression2.cpp +++ b/test/src/unit-regression2.cpp @@ -157,6 +157,26 @@ struct adl_serializer }; } // namespace nlohmann +///////////////////////////////////////////////////////////////////// +// for #2824 +///////////////////////////////////////////////////////////////////// + +class sax_no_exception : public nlohmann::detail::json_sax_dom_parser +{ + public: + explicit sax_no_exception(json& j) : nlohmann::detail::json_sax_dom_parser(j, false) {} + + static bool parse_error(std::size_t position, const std::string& last_token, const json::exception& ex) + { + error_string = ex.what(); + return false; + } + + static std::string error_string; +}; + +std::string sax_no_exception::error_string = ""; + TEST_CASE("regression tests 2") { @@ -622,6 +642,15 @@ TEST_CASE("regression tests 2") } } + SECTION("issue #2824 - encoding of json::exception::what()") + { + json j; + sax_no_exception sax(j); + + CHECK (!json::sax_parse("xyz", &sax)); + CHECK(sax_no_exception::error_string == "[json.exception.parse_error.101] parse error at line 1, column 1: syntax error while parsing value - invalid literal; last read: 'x'"); + } + SECTION("issue #2825 - Properly constrain the basic_json conversion operator") { static_assert(std::is_copy_assignable::value, ""); From 2c030bac2cf49cbc5201d39899a4e8f7953b73e0 Mon Sep 17 00:00:00 2001 From: Niels Lohmann Date: Fri, 30 Jul 2021 14:41:06 +0200 Subject: [PATCH 02/13] :white_check_mark: add regression test #2824 --- test/src/unit-regression2.cpp | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/test/src/unit-regression2.cpp b/test/src/unit-regression2.cpp index 96bf6c872..56bb3f398 100644 --- a/test/src/unit-regression2.cpp +++ b/test/src/unit-regression2.cpp @@ -166,16 +166,16 @@ class sax_no_exception : public nlohmann::detail::json_sax_dom_parser public: explicit sax_no_exception(json& j) : nlohmann::detail::json_sax_dom_parser(j, false) {} - static bool parse_error(std::size_t position, const std::string& last_token, const json::exception& ex) + static bool parse_error(std::size_t /*position*/, const std::string& /*last_token*/, const json::exception& ex) { - error_string = ex.what(); + error_string = new std::string(ex.what()); return false; } - static std::string error_string; + static std::string* error_string; }; -std::string sax_no_exception::error_string = ""; +std::string* sax_no_exception::error_string = nullptr; TEST_CASE("regression tests 2") @@ -648,7 +648,8 @@ TEST_CASE("regression tests 2") sax_no_exception sax(j); CHECK (!json::sax_parse("xyz", &sax)); - CHECK(sax_no_exception::error_string == "[json.exception.parse_error.101] parse error at line 1, column 1: syntax error while parsing value - invalid literal; last read: 'x'"); + CHECK(*sax_no_exception::error_string == "[json.exception.parse_error.101] parse error at line 1, column 1: syntax error while parsing value - invalid literal; last read: 'x'"); + delete sax_no_exception::error_string; } SECTION("issue #2825 - Properly constrain the basic_json conversion operator") From c7822840eb57d35a63ddcdec115bc3ea42266567 Mon Sep 17 00:00:00 2001 From: Niels Lohmann Date: Fri, 30 Jul 2021 15:00:54 +0200 Subject: [PATCH 03/13] :white_check_mark: add regression test #2824 --- test/src/unit-regression2.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/src/unit-regression2.cpp b/test/src/unit-regression2.cpp index 56bb3f398..c20d72e38 100644 --- a/test/src/unit-regression2.cpp +++ b/test/src/unit-regression2.cpp @@ -168,7 +168,7 @@ class sax_no_exception : public nlohmann::detail::json_sax_dom_parser static bool parse_error(std::size_t /*position*/, const std::string& /*last_token*/, const json::exception& ex) { - error_string = new std::string(ex.what()); + error_string = new std::string(ex.what()); // NOLINT(cppcoreguidelines-owning-memory) return false; } From 8676f6745e84e1f84b3444dd200d71b109b1a090 Mon Sep 17 00:00:00 2001 From: Niels Lohmann Date: Fri, 30 Jul 2021 21:18:25 +0200 Subject: [PATCH 04/13] :white_check_mark: add regression test #2824 --- test/src/unit-regression2.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/src/unit-regression2.cpp b/test/src/unit-regression2.cpp index c20d72e38..603963f50 100644 --- a/test/src/unit-regression2.cpp +++ b/test/src/unit-regression2.cpp @@ -649,7 +649,7 @@ TEST_CASE("regression tests 2") CHECK (!json::sax_parse("xyz", &sax)); CHECK(*sax_no_exception::error_string == "[json.exception.parse_error.101] parse error at line 1, column 1: syntax error while parsing value - invalid literal; last read: 'x'"); - delete sax_no_exception::error_string; + delete sax_no_exception::error_string; // NOLINT(cppcoreguidelines-owning-memory) } SECTION("issue #2825 - Properly constrain the basic_json conversion operator") From 5a1bc76f34a01d30eb3b4b0c54f6ec97de28cdb0 Mon Sep 17 00:00:00 2001 From: Niels Lohmann Date: Sun, 1 Aug 2021 13:34:55 +0200 Subject: [PATCH 05/13] :alembic: switch off exceptions --- test/CMakeLists.txt | 9 ++++ test/src/unit-disabled_exceptions.cpp | 66 +++++++++++++++++++++++++++ 2 files changed, 75 insertions(+) create mode 100644 test/src/unit-disabled_exceptions.cpp diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index cbbdcace3..271b8c77a 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -90,6 +90,15 @@ foreach(file ${files}) endif() endforeach() +# disable exceptions for test-disabled_exceptions +target_compile_definitions(test-disabled_exceptions PUBLIC JSON_NOEXCEPTION) +if (CMAKE_CXX_COMPILER_ID STREQUAL "Clang" OR CMAKE_CXX_COMPILER_ID STREQUAL "AppleClang" OR CMAKE_CXX_COMPILER_ID STREQUAL "GNU") + target_compile_options(test-disabled_exceptions PUBLIC -fno-exceptions) +elseif (CMAKE_CXX_COMPILER_ID STREQUAL "MSVC") + target_compile_options(test-disabled_exceptions PUBLIC /EH) + target_compile_definitions(test-disabled_exceptions PUBLIC _HAS_EXCEPTIONS=0) +endif() + add_executable(json_unit EXCLUDE_FROM_ALL $ ${files}) target_compile_definitions(json_unit PRIVATE DOCTEST_CONFIG_SUPER_FAST_ASSERTS) target_compile_options(json_unit PRIVATE diff --git a/test/src/unit-disabled_exceptions.cpp b/test/src/unit-disabled_exceptions.cpp new file mode 100644 index 000000000..6da9dcf1d --- /dev/null +++ b/test/src/unit-disabled_exceptions.cpp @@ -0,0 +1,66 @@ +/* + __ _____ _____ _____ + __| | __| | | | JSON for Modern C++ (test suite) +| | |__ | | | | | | version 3.9.1 +|_____|_____|_____|_|___| https://github.com/nlohmann/json + +Licensed under the MIT License . +SPDX-License-Identifier: MIT +Copyright (c) 2013-2019 Niels Lohmann . + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. +*/ + +#include "doctest_compatibility.h" + +#include +using json = nlohmann::json; + +///////////////////////////////////////////////////////////////////// +// for #2824 +///////////////////////////////////////////////////////////////////// + +class sax_no_exception : public nlohmann::detail::json_sax_dom_parser +{ + public: + explicit sax_no_exception(json& j) : nlohmann::detail::json_sax_dom_parser(j, false) {} + + static bool parse_error(std::size_t /*position*/, const std::string& /*last_token*/, const json::exception& ex) + { + error_string = new std::string(ex.what()); // NOLINT(cppcoreguidelines-owning-memory) + return false; + } + + static std::string* error_string; +}; + +std::string* sax_no_exception::error_string = nullptr; + +TEST_CASE("Tests with disabled exceptions") +{ + SECTION("issue #2824 - encoding of json::exception::what()") + { + json j; + sax_no_exception sax(j); + + CHECK (!json::sax_parse("xyz", &sax)); + CHECK(*sax_no_exception::error_string == "[json.exception.parse_error.101] parse error at line 1, column 1: syntax error while parsing value - invalid literal; last read: 'x'"); + delete sax_no_exception::error_string; // NOLINT(cppcoreguidelines-owning-memory) + } +} From c3df4ffd5aa4b0b80f2d19a6b7006f89f3efd09d Mon Sep 17 00:00:00 2001 From: Niels Lohmann Date: Sun, 1 Aug 2021 14:08:14 +0200 Subject: [PATCH 06/13] :rotating_light: fix warning --- include/nlohmann/detail/output/binary_writer.hpp | 1 + single_include/nlohmann/json.hpp | 1 + 2 files changed, 2 insertions(+) diff --git a/include/nlohmann/detail/output/binary_writer.hpp b/include/nlohmann/detail/output/binary_writer.hpp index 24e7c1094..7cd3d804b 100644 --- a/include/nlohmann/detail/output/binary_writer.hpp +++ b/include/nlohmann/detail/output/binary_writer.hpp @@ -908,6 +908,7 @@ class binary_writer if (JSON_HEDLEY_UNLIKELY(it != BasicJsonType::string_t::npos)) { JSON_THROW(out_of_range::create(409, "BSON key cannot contain code point U+0000 (at byte " + std::to_string(it) + ")", j)); + static_cast(j); } return /*id*/ 1ul + name.size() + /*zero-terminator*/1u; diff --git a/single_include/nlohmann/json.hpp b/single_include/nlohmann/json.hpp index 28ce55292..681f19495 100644 --- a/single_include/nlohmann/json.hpp +++ b/single_include/nlohmann/json.hpp @@ -14209,6 +14209,7 @@ class binary_writer if (JSON_HEDLEY_UNLIKELY(it != BasicJsonType::string_t::npos)) { JSON_THROW(out_of_range::create(409, "BSON key cannot contain code point U+0000 (at byte " + std::to_string(it) + ")", j)); + static_cast(j); } return /*id*/ 1ul + name.size() + /*zero-terminator*/1u; From 83c005f881e6be8095de954030318dad1ea37371 Mon Sep 17 00:00:00 2001 From: Niels Lohmann Date: Mon, 9 Aug 2021 21:43:03 +0200 Subject: [PATCH 07/13] :alembic: try minimal example --- test/src/unit-disabled_exceptions.cpp | 88 ++++++++++++++++++++++++++- 1 file changed, 87 insertions(+), 1 deletion(-) diff --git a/test/src/unit-disabled_exceptions.cpp b/test/src/unit-disabled_exceptions.cpp index 6da9dcf1d..b0d772d89 100644 --- a/test/src/unit-disabled_exceptions.cpp +++ b/test/src/unit-disabled_exceptions.cpp @@ -52,6 +52,81 @@ class sax_no_exception : public nlohmann::detail::json_sax_dom_parser std::string* sax_no_exception::error_string = nullptr; +// +#include // size_t +#include // exception +#include // runtime_error +#include // to_string +#include // vector + +// +namespace nlohmann +{ +namespace detail2 +{ + +struct position_t +{ + std::size_t chars_read_total = 0; + std::size_t chars_read_current_line = 0; + std::size_t lines_read = 0; + + constexpr operator size_t() const + { + return chars_read_total; + } +}; + + +class exception : public std::exception +{ + public: + const char* what() const noexcept override + { + return m.what(); + } + + const int id; + + protected: + exception(int id_, const char* what_arg) : id(id_), m(what_arg) {} + + static std::string name(const std::string& ename, int id_) + { + return "[json.exception." + ename + "." + std::to_string(id_) + "] "; + } + + private: + std::runtime_error m; +}; + +class parse_error : public exception +{ + public: + static parse_error create(int id_, const position_t& pos, const std::string& what_arg) + { + std::string w = exception::name("parse_error", id_) + "parse error" + + position_string(pos) + ": " + what_arg; + return parse_error(id_, pos.chars_read_total, w.c_str()); + } + + const std::size_t byte; + + private: + parse_error(int id_, std::size_t byte_, const char* what_arg) + : exception(id_, what_arg), byte(byte_) {} + + static std::string position_string(const position_t& pos) + { + return " at line " + std::to_string(pos.lines_read + 1) + + ", column " + std::to_string(pos.chars_read_current_line); + } +}; + +} // namespace detail +} // namespace nlohmann +// + TEST_CASE("Tests with disabled exceptions") { SECTION("issue #2824 - encoding of json::exception::what()") @@ -60,7 +135,18 @@ TEST_CASE("Tests with disabled exceptions") sax_no_exception sax(j); CHECK (!json::sax_parse("xyz", &sax)); - CHECK(*sax_no_exception::error_string == "[json.exception.parse_error.101] parse error at line 1, column 1: syntax error while parsing value - invalid literal; last read: 'x'"); + //CHECK(*sax_no_exception::error_string == "[json.exception.parse_error.101] parse error at line 1, column 1: syntax error while parsing value - invalid literal; last read: 'x'"); delete sax_no_exception::error_string; // NOLINT(cppcoreguidelines-owning-memory) } + + SECTION("test") + { + nlohmann::detail2::position_t pos; + pos.chars_read_total = 100; + pos.chars_read_current_line = 50; + pos.lines_read = 1; + + auto error = nlohmann::detail2::parse_error::create(100, pos, "foo"); + CHECK(std::string(error.what()) == "[json.exception.parse_error.100] parse error at line 2, column 50: foo"); + } } From 36394cc262e50b0e2a5a53e11655564ffc2d669b Mon Sep 17 00:00:00 2001 From: Niels Lohmann Date: Mon, 9 Aug 2021 21:58:05 +0200 Subject: [PATCH 08/13] :alembic: try minimal example --- test/src/unit-disabled_exceptions.cpp | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/test/src/unit-disabled_exceptions.cpp b/test/src/unit-disabled_exceptions.cpp index b0d772d89..22a85b2c8 100644 --- a/test/src/unit-disabled_exceptions.cpp +++ b/test/src/unit-disabled_exceptions.cpp @@ -77,7 +77,6 @@ struct position_t } }; - class exception : public std::exception { public: @@ -86,7 +85,7 @@ class exception : public std::exception return m.what(); } - const int id; + const int id; // NOLINT(cppcoreguidelines-non-private-member-variables-in-classes) protected: exception(int id_, const char* what_arg) : id(id_), m(what_arg) {} @@ -123,7 +122,7 @@ class parse_error : public exception } }; -} // namespace detail +} // namespace detail2 } // namespace nlohmann // From 0d530c9de83229ced6d25a6a2e8dbd305c07c7ff Mon Sep 17 00:00:00 2001 From: Niels Lohmann Date: Tue, 10 Aug 2021 08:22:21 +0200 Subject: [PATCH 09/13] :alembic: try minimal example --- test/src/unit-disabled_exceptions.cpp | 49 ++++++--------------------- 1 file changed, 11 insertions(+), 38 deletions(-) diff --git a/test/src/unit-disabled_exceptions.cpp b/test/src/unit-disabled_exceptions.cpp index 22a85b2c8..248a2134a 100644 --- a/test/src/unit-disabled_exceptions.cpp +++ b/test/src/unit-disabled_exceptions.cpp @@ -65,18 +65,6 @@ namespace nlohmann namespace detail2 { -struct position_t -{ - std::size_t chars_read_total = 0; - std::size_t chars_read_current_line = 0; - std::size_t lines_read = 0; - - constexpr operator size_t() const - { - return chars_read_total; - } -}; - class exception : public std::exception { public: @@ -85,14 +73,12 @@ class exception : public std::exception return m.what(); } - const int id; // NOLINT(cppcoreguidelines-non-private-member-variables-in-classes) - protected: - exception(int id_, const char* what_arg) : id(id_), m(what_arg) {} + exception(const char* what_arg) : m(what_arg) {} - static std::string name(const std::string& ename, int id_) + static std::string name(const std::string& ename) { - return "[json.exception." + ename + "." + std::to_string(id_) + "] "; + return "[json.exception." + ename + "] "; } private: @@ -102,24 +88,16 @@ class exception : public std::exception class parse_error : public exception { public: - static parse_error create(int id_, const position_t& pos, const std::string& what_arg) + static parse_error create(const std::string& what_arg) { - std::string w = exception::name("parse_error", id_) + "parse error" + - position_string(pos) + ": " + what_arg; - return parse_error(id_, pos.chars_read_total, w.c_str()); + std::string w = exception::name("parse_error") + what_arg; + return parse_error(w.c_str()); } - const std::size_t byte; - private: - parse_error(int id_, std::size_t byte_, const char* what_arg) - : exception(id_, what_arg), byte(byte_) {} - - static std::string position_string(const position_t& pos) - { - return " at line " + std::to_string(pos.lines_read + 1) + - ", column " + std::to_string(pos.chars_read_current_line); - } + parse_error(const char* what_arg) + : exception(what_arg) + {} }; } // namespace detail2 @@ -140,12 +118,7 @@ TEST_CASE("Tests with disabled exceptions") SECTION("test") { - nlohmann::detail2::position_t pos; - pos.chars_read_total = 100; - pos.chars_read_current_line = 50; - pos.lines_read = 1; - - auto error = nlohmann::detail2::parse_error::create(100, pos, "foo"); - CHECK(std::string(error.what()) == "[json.exception.parse_error.100] parse error at line 2, column 50: foo"); + auto error = nlohmann::detail2::parse_error::create("foo"); + CHECK(std::string(error.what()) == "[json.exception.parse_error] foo"); } } From edc203a837c79d9fec672134993dfa625b80de4b Mon Sep 17 00:00:00 2001 From: Niels Lohmann Date: Tue, 10 Aug 2021 08:35:14 +0200 Subject: [PATCH 10/13] :alembic: try minimal example --- test/src/unit-disabled_exceptions.cpp | 11 ++--------- 1 file changed, 2 insertions(+), 9 deletions(-) diff --git a/test/src/unit-disabled_exceptions.cpp b/test/src/unit-disabled_exceptions.cpp index 248a2134a..ed8800f81 100644 --- a/test/src/unit-disabled_exceptions.cpp +++ b/test/src/unit-disabled_exceptions.cpp @@ -76,11 +76,6 @@ class exception : public std::exception protected: exception(const char* what_arg) : m(what_arg) {} - static std::string name(const std::string& ename) - { - return "[json.exception." + ename + "] "; - } - private: std::runtime_error m; }; @@ -90,14 +85,12 @@ class parse_error : public exception public: static parse_error create(const std::string& what_arg) { - std::string w = exception::name("parse_error") + what_arg; + std::string w = "[json.exception.parse_error] " + what_arg; return parse_error(w.c_str()); } private: - parse_error(const char* what_arg) - : exception(what_arg) - {} + parse_error(const char* what_arg) : exception(what_arg) {} }; } // namespace detail2 From 26569f8eccf0611938103c0549f1c0465c83f153 Mon Sep 17 00:00:00 2001 From: Niels Lohmann Date: Tue, 10 Aug 2021 10:09:31 +0200 Subject: [PATCH 11/13] :alembic: try minimal example --- test/src/unit-disabled_exceptions.cpp | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/test/src/unit-disabled_exceptions.cpp b/test/src/unit-disabled_exceptions.cpp index ed8800f81..31a9873cf 100644 --- a/test/src/unit-disabled_exceptions.cpp +++ b/test/src/unit-disabled_exceptions.cpp @@ -83,13 +83,6 @@ class exception : public std::exception class parse_error : public exception { public: - static parse_error create(const std::string& what_arg) - { - std::string w = "[json.exception.parse_error] " + what_arg; - return parse_error(w.c_str()); - } - - private: parse_error(const char* what_arg) : exception(what_arg) {} }; @@ -111,7 +104,8 @@ TEST_CASE("Tests with disabled exceptions") SECTION("test") { - auto error = nlohmann::detail2::parse_error::create("foo"); + std::string w = "[json.exception.parse_error] foo"; + auto error = nlohmann::detail2::parse_error(w.c_str()); CHECK(std::string(error.what()) == "[json.exception.parse_error] foo"); } } From b792bf56601b33737b2abf2d10945d984075643f Mon Sep 17 00:00:00 2001 From: Niels Lohmann Date: Tue, 10 Aug 2021 10:29:13 +0200 Subject: [PATCH 12/13] :alembic: try minimal example --- test/src/unit-disabled_exceptions.cpp | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/test/src/unit-disabled_exceptions.cpp b/test/src/unit-disabled_exceptions.cpp index 31a9873cf..5a29958c9 100644 --- a/test/src/unit-disabled_exceptions.cpp +++ b/test/src/unit-disabled_exceptions.cpp @@ -53,11 +53,8 @@ class sax_no_exception : public nlohmann::detail::json_sax_dom_parser std::string* sax_no_exception::error_string = nullptr; // -#include // size_t #include // exception #include // runtime_error -#include // to_string -#include // vector // namespace nlohmann @@ -83,6 +80,13 @@ class exception : public std::exception class parse_error : public exception { public: + static parse_error create(const std::string& what_arg) + { + std::string w = "[json.exception.parse_error] " + what_arg; + return parse_error(w.c_str()); + } + + private: parse_error(const char* what_arg) : exception(what_arg) {} }; @@ -104,8 +108,7 @@ TEST_CASE("Tests with disabled exceptions") SECTION("test") { - std::string w = "[json.exception.parse_error] foo"; - auto error = nlohmann::detail2::parse_error(w.c_str()); + auto error = nlohmann::detail2::parse_error::create("foo"); CHECK(std::string(error.what()) == "[json.exception.parse_error] foo"); } } From 68d8661f1b41c078a3871efdae748d313c793b79 Mon Sep 17 00:00:00 2001 From: Niels Lohmann Date: Wed, 11 Aug 2021 08:06:25 +0200 Subject: [PATCH 13/13] :memo: update documentation --- README.md | 2 +- doc/mkdocs/docs/features/macros.md | 2 ++ doc/mkdocs/docs/home/exceptions.md | 2 ++ test/CMakeLists.txt | 5 +-- test/src/unit-disabled_exceptions.cpp | 50 +-------------------------- 5 files changed, 9 insertions(+), 52 deletions(-) diff --git a/README.md b/README.md index 9f972c211..e34afaf4d 100644 --- a/README.md +++ b/README.md @@ -1648,7 +1648,7 @@ Here is a related issue [#1924](https://github.com/nlohmann/json/issues/1924). - 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/api/basic_json/operator%5B%5D/) 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/api/basic_json/at/). 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. +- **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. Note the explanatory [`what()`](https://en.cppreference.com/w/cpp/error/exception/what) string of exceptions is not available for MSVC if exceptions are disabled, see [#2824](https://github.com/nlohmann/json/discussions/2824). ## Execute unit tests diff --git a/doc/mkdocs/docs/features/macros.md b/doc/mkdocs/docs/features/macros.md index d008393f7..8b10ef1f3 100644 --- a/doc/mkdocs/docs/features/macros.md +++ b/doc/mkdocs/docs/features/macros.md @@ -32,6 +32,8 @@ When defining `JSON_NOEXCEPTION`, `#!cpp try` is replaced by `#!cpp if (true)`, The same effect is achieved by setting the compiler flag `-fno-exceptions`. +Note the explanatory [`what()`](https://en.cppreference.com/w/cpp/error/exception/what) string of exceptions is not available for MSVC if exceptions are disabled, see [#2824](https://github.com/nlohmann/json/discussions/2824). + ## `JSON_NO_IO` When defined, headers ``, ``, ``, ``, and `` are not included and parse functions relying on these headers are excluded. This is relevant for environment where these I/O functions are disallowed for security reasons (e.g., Intel Software Guard Extensions (SGX)). diff --git a/doc/mkdocs/docs/home/exceptions.md b/doc/mkdocs/docs/home/exceptions.md index d5d07362f..a04b60f86 100644 --- a/doc/mkdocs/docs/home/exceptions.md +++ b/doc/mkdocs/docs/home/exceptions.md @@ -50,6 +50,8 @@ Note that `JSON_THROW_USER` should leave the current scope (e.g., by throwing or #include ``` +Note the explanatory [`what()`](https://en.cppreference.com/w/cpp/error/exception/what) string of exceptions is not available for MSVC if exceptions are disabled, see [#2824](https://github.com/nlohmann/json/discussions/2824). + ### Extended diagnostic messages Exceptions in the library are thrown in the local context of the JSON value they are detected. This makes detailed diagnostics messages, and hence debugging, difficult. diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 271b8c77a..d0ae29438 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -95,8 +95,9 @@ target_compile_definitions(test-disabled_exceptions PUBLIC JSON_NOEXCEPTION) if (CMAKE_CXX_COMPILER_ID STREQUAL "Clang" OR CMAKE_CXX_COMPILER_ID STREQUAL "AppleClang" OR CMAKE_CXX_COMPILER_ID STREQUAL "GNU") target_compile_options(test-disabled_exceptions PUBLIC -fno-exceptions) elseif (CMAKE_CXX_COMPILER_ID STREQUAL "MSVC") - target_compile_options(test-disabled_exceptions PUBLIC /EH) - target_compile_definitions(test-disabled_exceptions PUBLIC _HAS_EXCEPTIONS=0) + # disabled due to https://github.com/nlohmann/json/discussions/2824 + #target_compile_options(test-disabled_exceptions PUBLIC /EH) + #target_compile_definitions(test-disabled_exceptions PUBLIC _HAS_EXCEPTIONS=0) endif() add_executable(json_unit EXCLUDE_FROM_ALL $ ${files}) diff --git a/test/src/unit-disabled_exceptions.cpp b/test/src/unit-disabled_exceptions.cpp index 5a29958c9..6da9dcf1d 100644 --- a/test/src/unit-disabled_exceptions.cpp +++ b/test/src/unit-disabled_exceptions.cpp @@ -52,48 +52,6 @@ class sax_no_exception : public nlohmann::detail::json_sax_dom_parser std::string* sax_no_exception::error_string = nullptr; -// -#include // exception -#include // runtime_error - -// -namespace nlohmann -{ -namespace detail2 -{ - -class exception : public std::exception -{ - public: - const char* what() const noexcept override - { - return m.what(); - } - - protected: - exception(const char* what_arg) : m(what_arg) {} - - private: - std::runtime_error m; -}; - -class parse_error : public exception -{ - public: - static parse_error create(const std::string& what_arg) - { - std::string w = "[json.exception.parse_error] " + what_arg; - return parse_error(w.c_str()); - } - - private: - parse_error(const char* what_arg) : exception(what_arg) {} -}; - -} // namespace detail2 -} // namespace nlohmann -// - TEST_CASE("Tests with disabled exceptions") { SECTION("issue #2824 - encoding of json::exception::what()") @@ -102,13 +60,7 @@ TEST_CASE("Tests with disabled exceptions") sax_no_exception sax(j); CHECK (!json::sax_parse("xyz", &sax)); - //CHECK(*sax_no_exception::error_string == "[json.exception.parse_error.101] parse error at line 1, column 1: syntax error while parsing value - invalid literal; last read: 'x'"); + CHECK(*sax_no_exception::error_string == "[json.exception.parse_error.101] parse error at line 1, column 1: syntax error while parsing value - invalid literal; last read: 'x'"); delete sax_no_exception::error_string; // NOLINT(cppcoreguidelines-owning-memory) } - - SECTION("test") - { - auto error = nlohmann::detail2::parse_error::create("foo"); - CHECK(std::string(error.what()) == "[json.exception.parse_error] foo"); - } }