From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 48) id A7CA23858D33; Thu, 4 May 2023 12:01:21 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org A7CA23858D33 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1683201681; bh=/kyqyvJHlY8z1E3MbYb9O5vQrA+7RoZWMjsXsk8WHWI=; h=From:To:Subject:Date:From; b=E6N06hD9mU1D/rd79mpG2MIsV3U575UniTiwOAKIviZyYkN+dkQD3J4H02FeABp0L Mocyb69NnBiiJUGd1XFmzEkEuq3bu2FfwHcPBBQwxgzlDj9Sdj/KIufoTGMlvFIDIB uGkZETic/Ia5NaAbUpykA88z9REET+QA8k6cbfYs= From: "slyfox at gcc dot gnu.org" To: gcc-bugs@gcc.gnu.org Subject: [Bug c++/109732] New: [14 regression] gcc miscompiles iterator comparison on nlohmann_json Date: Thu, 04 May 2023 12:01:21 +0000 X-Bugzilla-Reason: CC X-Bugzilla-Type: new X-Bugzilla-Watch-Reason: None X-Bugzilla-Product: gcc X-Bugzilla-Component: c++ X-Bugzilla-Version: 14.0 X-Bugzilla-Keywords: X-Bugzilla-Severity: normal X-Bugzilla-Who: slyfox at gcc dot gnu.org X-Bugzilla-Status: UNCONFIRMED X-Bugzilla-Resolution: X-Bugzilla-Priority: P3 X-Bugzilla-Assigned-To: unassigned at gcc dot gnu.org X-Bugzilla-Target-Milestone: --- X-Bugzilla-Flags: X-Bugzilla-Changed-Fields: bug_id short_desc product version bug_status bug_severity priority component assigned_to reporter target_milestone Message-ID: Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Bugzilla-URL: http://gcc.gnu.org/bugzilla/ Auto-Submitted: auto-generated MIME-Version: 1.0 List-Id: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=3D109732 Bug ID: 109732 Summary: [14 regression] gcc miscompiles iterator comparison on nlohmann_json Product: gcc Version: 14.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: c++ Assignee: unassigned at gcc dot gnu.org Reporter: slyfox at gcc dot gnu.org Target Milestone: --- Initially I observed the failure as a test failure on nlohmann_json-3.11.2 against gcc-14 master (r14-395-g1adb1a653d6739): 33 - test-items_cpp11 (Failed) 34 - test-items_cpp17 (Failed) I extracted smaller but not yet self-contained example that seems to illust= rate the problem: // $ cat unit-t.cpp #include #include namespace { int seen_failures =3D 0; __attribute__((noinline)) static void sne(nlohmann::json::const_reverse_iterator lhs, nlohmann::json::const_reverse_iterator rhs) { bool res =3D !(lhs =3D=3D rhs); if (!res) seen_failures++; } struct TestCase { void (*m_test)(); TestCase(void (*test)()) { // not used anywhere, but triggers the failure m_test =3D test; } }; static void _DOCTEST_ANON_FUNC_8() { const nlohmann::json js =3D "hello world"; const nlohmann::json js_const(js); nlohmann::json::const_reverse_iterator sit =3D js_const.crbegin(); sne(sit, js_const.crend()); } } int main() { // below 3 lines look like a no-op, but afects the result: TestCase ltc(&_DOCTEST_ANON_FUNC_8); std::vector testArray; testArray.push_back(<c); _DOCTEST_ANON_FUNC_8(); puts((seen_failures > 0) ? "FAILURE!" : "SUCCESS!"); return EXIT_SUCCESS; } To trigger it we will need json headers-only library: $ git clone --depth 1 https://github.com/nlohmann/json.git # commit 6af826d0bdb55e4b69e3ad817576745335f243ca $ g++-14 unit-t.cpp -O2 -Ijson/include -o a && ./a FAILURE! For comparison unoptimized and older gcc does work as expcted: $ g++-14 unit-t.cpp -O0 -Ijson/include -o a && ./a SUCCESS! $ g++ unit-t.cpp -O2 -Ijson/include -o a && ./a SUCCESS! I'm not sure if `nlohmann::json::const_reverse_iterator` is implemented correctly according to c++ requirements. The inheritance looks fishy. At le= ast I would expect consistent behaviour for -O0/O2. -fsanitize=3D{address,undefined} does not uncover anything obvious. Can you= help me understand if it's a gcc or json deficiency? Thank you!=