diff options
author | Jonathan Wakely <jwakely@redhat.com> | 2023-08-15 13:48:23 +0100 |
---|---|---|
committer | Jonathan Wakely <jwakely@redhat.com> | 2023-08-16 18:36:37 +0100 |
commit | 4a2b262597e4a6bc5732d4564673c1e19381dcfa (patch) | |
tree | 329c32e9e5c3e430ef91a3cad281da688bbb9bec /libgomp/allocator.c | |
parent | 0c316669b092fb65a1da2b22ea5efb348da12017 (diff) | |
download | gcc-4a2b262597e4a6bc5732d4564673c1e19381dcfa.zip gcc-4a2b262597e4a6bc5732d4564673c1e19381dcfa.tar.gz gcc-4a2b262597e4a6bc5732d4564673c1e19381dcfa.tar.bz2 |
libstdc++: Fix std::basic_string::resize_and_overwrite
The callable used for resize_and_overwrite was being passed the string's
expanded capacity, which might be greater than the new size being
requested. This is not conforming, as the standard requires the same n
to be passed to the callable that the user passed to
resize_and_overwrite.
The existing tests didn't catch this because they all used a value which
was more than twice the existing capacity, so the _M_create call
allocated exactly what was requested, and the value passed to the
callable was correct. But when the requested size is greater than the
current capacity but smaller than twice the current capacity, _M_create
will allocate twice the current capacity and then that value was being
passed to the callable.
I noticed this because std::format(L"{}", 0.25) was producing L"0.25XX"
where the XX characters were whatever happened to be on the stack before
the call. When std::format used resize_and_overwrite to widen a string
it was copying too many characters into the destination and setting the
result's length too long. I've added a test for this case, and a new
test that doesn't hardcode -std=gnu++20 so can be used to test
std::format in C++23 and C++26 modes.
libstdc++-v3/ChangeLog:
* include/bits/basic_string.tcc (resize_and_overwrite): Invoke
the callable with the same size as resize_and_overwrite was
called with.
* testsuite/21_strings/basic_string/capacity/char/resize_and_overwrite.cc:
Check with small values for the new size.
* testsuite/std/format/functions/format.cc: Check wide
formatting of double values that produce small strings.
* testsuite/std/format/functions/format_c++23.cc: New test.
Diffstat (limited to 'libgomp/allocator.c')
0 files changed, 0 insertions, 0 deletions