[PATCH v2] libstdc++: Fix _Padding_sink in case when predicted is between padwidht and maxwidth [PR109162]
Tomasz Kamiński
tkaminsk@redhat.com
Sat Apr 26 04:56:13 GMT 2025
The _Padding_sink was behaving incorrectly, when the predicated width (based on
code units count) was higher than _M_maxwidth, but lower than _M_padwidth.
In this case _M_update() returned without calling _M_force_update() and computing
field width for Unicode encoding, because _M_buffering() returned 'true'.
As a consequence we switched to _M_ignoring() mode, while storing a sequence
with more code units but smaller field width than _M_maxwidth.
We now call _M_force_update() if predicted width is greater or equal to either
_M_padwidth or _M_maxwidth.
This happened for existing test case on 32bit architecture.
libstdc++-v3/ChangeLog:
* include/std/format (_Padding_sink::_M_update): Fixed condition for
calling _M_force_update.
* testsuite/std/format/debug.cc: Add test that reproducers this issue
on 64bit architecture.
* testsuite/std/format/ranges/sequence.cc: Another edge value test.
---
Fixed some types in message and replaced > with >= in check.
Tested on x86_64-linux with unix{,-std=c++98,-std=gnu++11,-std=gnu++20,
-D_GLIBCXX_USE_CXX11_ABI=0/-D_GLIBCXX_DEBUG,-D_GLIBCXX_ASSERTIONS/-m32,-std=gnu++23}.
OK for trunk?
libstdc++-v3/include/std/format | 7 ++++---
libstdc++-v3/testsuite/std/format/debug.cc | 9 +++++++++
libstdc++-v3/testsuite/std/format/ranges/sequence.cc | 9 +++++++++
3 files changed, 22 insertions(+), 3 deletions(-)
diff --git a/libstdc++-v3/include/std/format b/libstdc++-v3/include/std/format
index 69d8d189db6..b3794b64b59 100644
--- a/libstdc++-v3/include/std/format
+++ b/libstdc++-v3/include/std/format
@@ -3697,9 +3697,10 @@ namespace __format
_M_update(size_t __new)
{
_M_printwidth += __new;
- if (_M_buffering())
- return true;
- return _M_force_update();
+ // Compute estimated witdth, to see if is not reduced.
+ if (_M_printwidth >= _M_padwidth || _M_printwidth >= _M_maxwidth)
+ return _M_force_update();
+ return true;
}
void
diff --git a/libstdc++-v3/testsuite/std/format/debug.cc b/libstdc++-v3/testsuite/std/format/debug.cc
index d3402f80f4b..6165a295496 100644
--- a/libstdc++-v3/testsuite/std/format/debug.cc
+++ b/libstdc++-v3/testsuite/std/format/debug.cc
@@ -596,6 +596,10 @@ void test_padding()
VERIFY( strip_prefix(resv, 48, '*') );
VERIFY( resv == inv );
+ resv = res = std::format("{:*>300.200s}", in);
+ VERIFY( strip_prefix(resv, 108, '*') );
+ VERIFY( resv == inv );
+
resv = res = std::format("{:*>240.200s}", in);
VERIFY( strip_prefix(resv, 48, '*') );
VERIFY( resv == inv );
@@ -678,6 +682,11 @@ void test_padding()
VERIFY( strip_quotes(resv) );
VERIFY( resv == inv );
+ resv = res = std::format("{:*>300.200?}", in);
+ VERIFY( strip_prefix(resv, 106, '*') );
+ VERIFY( strip_quotes(resv) );
+ VERIFY( resv == inv );
+
resv = res = std::format("{:*>240.200?}", in);
VERIFY( strip_prefix(resv, 46, '*') );
VERIFY( strip_quotes(resv) );
diff --git a/libstdc++-v3/testsuite/std/format/ranges/sequence.cc b/libstdc++-v3/testsuite/std/format/ranges/sequence.cc
index 75fe4c19a52..32242860f10 100644
--- a/libstdc++-v3/testsuite/std/format/ranges/sequence.cc
+++ b/libstdc++-v3/testsuite/std/format/ranges/sequence.cc
@@ -295,6 +295,15 @@ void test_padding()
resv = res = std::format("{:*>10n:}", vs);
VERIFY( check_elems(resv, false) );
+ resv = res = std::format("{:*>256}", vs);
+ VERIFY( strip_prefix(resv, 48, '*') );
+ VERIFY( strip_squares(resv) );
+ VERIFY( check_elems(resv, true) );
+
+ resv = res = std::format("{:*>256n}", vs);
+ VERIFY( strip_prefix(resv, 50, '*') );
+ VERIFY( check_elems(resv, true) );
+
resv = res = std::format("{:*>240}", vs);
VERIFY( strip_prefix(resv, 32, '*') );
VERIFY( strip_squares(resv) );
--
2.49.0
More information about the Libstdc++
mailing list