diff --git a/README.md b/README.md index ab44c969..b49942d6 100644 --- a/README.md +++ b/README.md @@ -823,7 +823,7 @@ I deeply appreciate the help of the following people. - [Martin Hořeňovský](https://github.com/horenmar) found a way for a 2x speedup for the compilation time of the test suite. - [ukhegg](https://github.com/ukhegg) found proposed an improvement for the examples section. - [rswanson-ihi](https://github.com/rswanson-ihi) noted a type in the README. -- +- [Mihai Stan](https://github.com/stanmihai4) fixed a bug in the comparison with `nullptr`s. Thanks a lot for helping out! Please [let me know](mailto:mail@nlohmann.me) if I forgot someone. @@ -857,7 +857,7 @@ $ make json_unit -Ctest $ ./test/json_unit "*"" =============================================================================== -All tests passed (11202562 assertions in 47 test cases) +All tests passed (11202588 assertions in 47 test cases) ``` Alternatively, you can use [CMake](https://cmake.org) and run diff --git a/src/json.hpp b/src/json.hpp index f823d435..6e49b4d9 100644 --- a/src/json.hpp +++ b/src/json.hpp @@ -5963,34 +5963,24 @@ class basic_json /*! @brief comparison: equal - - The functions compares the given JSON value against a null pointer. As the - null pointer can be used to initialize a JSON value to null, a comparison - of JSON value @a v with a null pointer should be equivalent to call - `v.is_null()`. - - @param[in] v JSON value to consider - @return whether @a v is null - - @complexity Constant. - - @liveexample{The example compares several JSON types to the null pointer. - ,operator__equal__nullptr_t} - - @since version 1.0.0 + @copydoc operator==(const_reference, const_reference) */ - friend bool operator==(const_reference v, std::nullptr_t) noexcept + template::value, int>::type = 0> + friend bool operator==(const_reference lhs, const ScalarType rhs) noexcept { - return v.is_null(); + return (lhs == basic_json(rhs)); } /*! @brief comparison: equal - @copydoc operator==(const_reference, std::nullptr_t) + @copydoc operator==(const_reference, const_reference) */ - friend bool operator==(std::nullptr_t, const_reference v) noexcept + template::value, int>::type = 0> + friend bool operator==(const ScalarType lhs, const_reference rhs) noexcept { - return v.is_null(); + return (basic_json(lhs) == rhs); } /*! @@ -6016,34 +6006,24 @@ class basic_json /*! @brief comparison: not equal - - The functions compares the given JSON value against a null pointer. As the - null pointer can be used to initialize a JSON value to null, a comparison - of JSON value @a v with a null pointer should be equivalent to call - `not v.is_null()`. - - @param[in] v JSON value to consider - @return whether @a v is not null - - @complexity Constant. - - @liveexample{The example compares several JSON types to the null pointer. - ,operator__notequal__nullptr_t} - - @since version 1.0.0 + @copydoc operator!=(const_reference, const_reference) */ - friend bool operator!=(const_reference v, std::nullptr_t) noexcept + template::value, int>::type = 0> + friend bool operator!=(const_reference lhs, const ScalarType rhs) noexcept { - return not v.is_null(); + return (lhs != basic_json(rhs)); } /*! @brief comparison: not equal - @copydoc operator!=(const_reference, std::nullptr_t) + @copydoc operator!=(const_reference, const_reference) */ - friend bool operator!=(std::nullptr_t, const_reference v) noexcept + template::value, int>::type = 0> + friend bool operator!=(const ScalarType lhs, const_reference rhs) noexcept { - return not v.is_null(); + return (basic_json(lhs) != rhs); } /*! diff --git a/src/json.hpp.re2c b/src/json.hpp.re2c index d58e80fc..dae2e958 100644 --- a/src/json.hpp.re2c +++ b/src/json.hpp.re2c @@ -5963,34 +5963,24 @@ class basic_json /*! @brief comparison: equal - - The functions compares the given JSON value against a null pointer. As the - null pointer can be used to initialize a JSON value to null, a comparison - of JSON value @a v with a null pointer should be equivalent to call - `v.is_null()`. - - @param[in] v JSON value to consider - @return whether @a v is null - - @complexity Constant. - - @liveexample{The example compares several JSON types to the null pointer. - ,operator__equal__nullptr_t} - - @since version 1.0.0 + @copydoc operator==(const_reference, const_reference) */ - friend bool operator==(const_reference v, std::nullptr_t) noexcept + template::value, int>::type = 0> + friend bool operator==(const_reference lhs, const ScalarType rhs) noexcept { - return v.is_null(); + return (lhs == basic_json(rhs)); } /*! @brief comparison: equal - @copydoc operator==(const_reference, std::nullptr_t) + @copydoc operator==(const_reference, const_reference) */ - friend bool operator==(std::nullptr_t, const_reference v) noexcept + template::value, int>::type = 0> + friend bool operator==(const ScalarType lhs, const_reference rhs) noexcept { - return v.is_null(); + return (basic_json(lhs) == rhs); } /*! @@ -6016,34 +6006,24 @@ class basic_json /*! @brief comparison: not equal - - The functions compares the given JSON value against a null pointer. As the - null pointer can be used to initialize a JSON value to null, a comparison - of JSON value @a v with a null pointer should be equivalent to call - `not v.is_null()`. - - @param[in] v JSON value to consider - @return whether @a v is not null - - @complexity Constant. - - @liveexample{The example compares several JSON types to the null pointer. - ,operator__notequal__nullptr_t} - - @since version 1.0.0 + @copydoc operator!=(const_reference, const_reference) */ - friend bool operator!=(const_reference v, std::nullptr_t) noexcept + template::value, int>::type = 0> + friend bool operator!=(const_reference lhs, const ScalarType rhs) noexcept { - return not v.is_null(); + return (lhs != basic_json(rhs)); } /*! @brief comparison: not equal - @copydoc operator!=(const_reference, std::nullptr_t) + @copydoc operator!=(const_reference, const_reference) */ - friend bool operator!=(std::nullptr_t, const_reference v) noexcept + template::value, int>::type = 0> + friend bool operator!=(const ScalarType lhs, const_reference rhs) noexcept { - return not v.is_null(); + return (basic_json(lhs) != rhs); } /*! diff --git a/test/src/unit-regression.cpp b/test/src/unit-regression.cpp index 3bd50747..9feb07e0 100644 --- a/test/src/unit-regression.cpp +++ b/test/src/unit-regression.cpp @@ -698,6 +698,24 @@ TEST_CASE("regression tests") CHECK_THROWS_AS(json::from_cbor(vec3), std::out_of_range); } + SECTION("issue #414 - compare with literal 0)") + { +#define CHECK_TYPE(v) \ + CHECK((json(v) == v));\ + CHECK((v == json(v)));\ + CHECK_FALSE((json(v) != v));\ + CHECK_FALSE((v != json(v))); + + CHECK_TYPE(nullptr); + CHECK_TYPE(0); + CHECK_TYPE(0u); + CHECK_TYPE(0L); + CHECK_TYPE(0.0); + CHECK_TYPE(""); + +#undef CHECK_TYPE + } + SECTION("issue #416 - Use-of-uninitialized-value (OSS-Fuzz issue 377)") { // original test case