Fix long double inf test under Valgrind. Fixes #5175#5176
Merged
Conversation
When you use long double as a floating point type with the current version of this file and try to dump json it prints trash instead of actual number. This if-else fixes the problem. On using long double you just need to add an 'L' modifier before 'g' in format string. Signed-off-by: Kirill Lokotkov <klokotkov@ya.ru>
Signed-off-by: Kirill Lokotkov <klokotkov@ya.ru>
Signed-off-by: Kirill Lokotkov <klokotkov@ya.ru>
Signed-off-by: rusloker <klokotkov@ya.ru>
Signed-off-by: rusloker <klokotkov@ya.ru>
# Conflicts: # include/nlohmann/detail/output/serializer.hpp # single_include/nlohmann/json.hpp
Signed-off-by: rusloker <klokotkov@ya.ru>
…type safety Signed-off-by: rusloker <klokotkov@ya.ru>
Signed-off-by: rusloker <klokotkov@ya.ru>
Signed-off-by: rusloker <klokotkov@ya.ru>
Signed-off-by: rusloker <klokotkov@ya.ru>
Signed-off-by: rusloker <klokotkov@ya.ru>
nlohmann
requested changes
May 16, 2026
Owner
nlohmann
left a comment
There was a problem hiding this comment.
There are 2 Clang-Tidy warnings to be fixed:
/__w/json/json/tests/src/unit-serialization.cpp:353:9: error: missing username/bug in TODO [google-readability-todo,-warnings-as-errors]
353 | // TODO: remove this guard once Valgrind's 80-bit long double support
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
| // TODO(unknown): remove this guard once Valgrind's 80-bit long double support
/__w/json/json/tests/src/unit-serialization.cpp:357:9: error: variable 'inf_probe' of type 'volatile long double' can be declared 'const' [misc-const-correctness,-warnings-as-errors]
357 | volatile long double inf_probe = std::numeric_limits<long double>::infinity();
| ^
| const
431 warnings generated.
Closed
2 tasks
Signed-off-by: rusloker <klokotkov@ya.ru>
Owner
|
Thanks for the quick fix! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
ci_test_valgrindstarted failing after #3929 added long-double serialization coverage. The two failing assertions are:doctest reports:
This PR keeps the assertions but guards them with a runtime probe, so the section continues to validate the production behavior on every real platform while not failing under Valgrind:
Only
tests/src/unit-serialization.cppis touched (+15/−2). No library code is modified.Why
The library itself is correct —
dump_float()already callsif (!std::isfinite(x)) { write "null"; return; }. The failure is in Valgrind, not in the JSON code.Valgrind has had incomplete 80-bit x87 long-double support since 2009 (Valgrind bug #197915, status ASSIGNED — Valgrind tracks its bugs on bugs.kde.org). On every long-double operation Valgrind round-trips through 64-bit double, and
+inf/-infcome back out looking likeLDBL_MAX. Reproduced locally withg++ 13.3 / valgrind 3.22.0on Ubuntu 24.04 — the bit pattern ofstd::numeric_limits<long double>::infinity()is preserved in memory, butstd::isfinite()returnstrue,std::isinf()returnsfalse, andsnprintf("%Lg", …)prints theLDBL_MAXdecimal instead of"inf". That's exactly the symptom in the issue.Why a runtime probe (and why
volatile)std::isfinite()recognize long-double infinity?" — so it adapts to any toolchain that exhibits the same defect, with no dependency on Valgrind-specific headers or build flags. The test file stays standard C++.volatileis load-bearing. Without it,std::isfiniteof aconstexprinfinity is folded tofalseat compile time and the guard becomes dead code.volatileforces a runtime read through the same FP pathdump_float()exercises, so the probe truly reflects platform behavior.+/-infis mishandled.TODOnext to the guard links to the upstream Valgrind bug so the workaround can be removed once Valgrind ships proper 80-bit support and the project bumps its minimum Valgrind version.Verification
Tested against the matrix of compilers/standards the CI exercises (Ubuntu 24.04 / GCC 13.3 / Clang 18.1 / Valgrind 3.22.0):
-Wvolatile,-Werrorci_test_valgrinduses GCC)ctest -L valgrind -R test-serialization_cpp11_valgrind(CI target)PassedFull
test-serialization_cpp11under Valgrind: 7/7 cases pass, 105/105 assertions pass, no Memcheck errors.make amalgamate. — N/A; no headers touched.Read the Contribution Guidelines for detailed information.