Open StephanTLavavej opened 1 month ago
For the g
specifier (which corresponds to chars_format::general
, which corresponds to printf
's %g
), a precision of 0 should behave as if it were 1. It seems that we are missing this adjustment.
Ah, great catch! We correctly handle that in <charconv>
, so it seems that <format>
is missing the same logic:
Reported to me by Attila Feher. https://godbolt.org/z/djh5KxjW6
libstdc++ and libc++ behave as expected, but MSVC prints:
:detective: Analysis
I debugged into this, and although I'm not absolutely certain of the root cause yet, this line appears to be involved:
https://github.com/microsoft/STL/blob/926d458f82ae1711d4e92c0341f541a520ef6198/stl/inc/format#L2278
_Zeroes_to_append
is computed as-1
because_Extra_precision
and_Precision
are both0
, and_Digits
is1
.We've correctly computed
_Width
to be6
, but the negative_Zeroes_to_append
damages it, which appears to be the proximate cause of the bug:https://github.com/microsoft/STL/blob/926d458f82ae1711d4e92c0341f541a520ef6198/stl/inc/format#L2297
Later, we use
_Zeroes_to_append
in a clamped manner (i.e. negative values are treated as zero):https://github.com/microsoft/STL/blob/926d458f82ae1711d4e92c0341f541a520ef6198/stl/inc/format#L2328-L2330
However, we need to completely understand the problem and all possible codepaths before developing a fix (i.e. we can't just clamp it to zero and call it a day). Note that the
chars_format::general
codepath further adjusts_Zeroes_to_append
:https://github.com/microsoft/STL/blob/926d458f82ae1711d4e92c0341f541a520ef6198/stl/inc/format#L2278-L2289