diff --git a/include/nlohmann/json.hpp b/include/nlohmann/json.hpp index 71247c417..e70eb3cc3 100644 --- a/include/nlohmann/json.hpp +++ b/include/nlohmann/json.hpp @@ -763,15 +763,6 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec static_cast(check_parents); } - // GCC 13 to at least 15 report a false -Warray-bounds error when set_parents() - // is inlined at -O3 right after a non-container value was created: the analysis - // does not use m_type to rule out the object/array branches and checks - // the std::map access against the allocation of, e.g., a string. - // See https://github.com/nlohmann/json/issues/5742 and #4819. -#if defined(__GNUC__) && !defined(__clang__) -#pragma GCC diagnostic push -#pragma GCC diagnostic ignored "-Warray-bounds" -#endif void set_parents() { #if JSON_DIAGNOSTICS @@ -808,9 +799,6 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec } #endif } -#if defined(__GNUC__) && !defined(__clang__) -#pragma GCC diagnostic pop -#endif iterator set_parents(iterator it, std::ptrdiff_t count_set_parents) { diff --git a/single_include/nlohmann/json.hpp b/single_include/nlohmann/json.hpp index 6406c39c1..1a4f50b21 100644 --- a/single_include/nlohmann/json.hpp +++ b/single_include/nlohmann/json.hpp @@ -27461,15 +27461,6 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec static_cast(check_parents); } - // GCC 13 to at least 15 report a false -Warray-bounds error when set_parents() - // is inlined at -O3 right after a non-container value was created: the analysis - // does not use m_type to rule out the object/array branches and checks - // the std::map access against the allocation of, e.g., a string. - // See https://github.com/nlohmann/json/issues/5742 and #4819. -#if defined(__GNUC__) && !defined(__clang__) -#pragma GCC diagnostic push -#pragma GCC diagnostic ignored "-Warray-bounds" -#endif void set_parents() { #if JSON_DIAGNOSTICS @@ -27506,9 +27497,6 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec } #endif } -#if defined(__GNUC__) && !defined(__clang__) -#pragma GCC diagnostic pop -#endif iterator set_parents(iterator it, std::ptrdiff_t count_set_parents) { diff --git a/tests/src/unit-diagnostics-optimized.cpp b/tests/src/unit-diagnostics-optimized.cpp index 0d715c325..a020c2fe9 100644 --- a/tests/src/unit-diagnostics-optimized.cpp +++ b/tests/src/unit-diagnostics-optimized.cpp @@ -7,10 +7,14 @@ // SPDX-License-Identifier: MIT // Regression test for https://github.com/nlohmann/json/issues/5742: with -// JSON_DIAGNOSTICS, GCC (13 to at least 15) reports a false -Warray-bounds -// error in the inlined set_parents() at -O3. The warning depends on GCC's -// inlining decisions, so the sections cover patterns that trigger it on -// different GCC versions (#4819: GCC 13 and 14; #5742: GCC 14 and 15). +// JSON_DIAGNOSTICS, GCC (12 to at least 16) reported a false -Warray-bounds +// error in the inlined set_parents() at -O3. The type of a new string was set +// before the string was allocated, so GCC had to assume that operator new +// could change it again and checked the object branch of set_parents() +// against the string's allocation. Setting the type after creating the value +// avoids this. The warning depends on GCC's inlining decisions, so the +// sections cover two patterns that trigger it on different GCC versions +// (#4819 and #5742). // On GCC, this file is compiled with -O3 -Werror=array-bounds (see // tests/CMakeLists.txt), so the test fails to build if the warning returns.