Suppress GCC's false -Warray-bounds error in set_parents() with JSON_DIAGNOSTICS

With JSON_DIAGNOSTICS, GCC 13 to 15 report a false -Warray-bounds error
at -O3 when set_parents() is inlined right after a non-container value
(e.g., a string) was created: the std::map access in the object branch
is checked against the string allocation although m_type rules that
branch out. GCC 15 fixed the pattern from #4819 but not this one.

Suppress -Warray-bounds around set_parents() only, and add a regression
test that is compiled with -O3 -Werror=array-bounds on GCC.

Fixes #5742

Signed-off-by: Niels Lohmann <mail@nlohmann.me>
This commit is contained in:
Niels Lohmann committed 2026-10-01 07:51:39 +02:00
1 parent bc1d9d11f4
commit 6b8d2f3d92
4 files changed
+105

No files matched your search

+12
View File
@@ -27441,6 +27441,15 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
static_cast<void>(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
@@ -27477,6 +27486,9 @@ 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)
{