std::basic_string::resize_and_overwrite & oversize passed to lambda

Ryszard Kabatek rumcajs@gmx.net
Thu Jan 11 20:40:14 GMT 2024


Hi!

I'm not sure if this was the intention, but in some cases the expected
size passed to resize_and_overwrite does not propagate to the lambda
being called. The value passed to lambda can be greater then the initial
value.


The code below produces the output:

16 != 30
17 != 30
18 != 30
19 != 30
20 != 30
21 != 30
22 != 30
23 != 30
24 != 30
25 != 30
26 != 30
27 != 30
28 != 30
29 != 30


#include<iostream>
#include<string>

int main()
{
   for (std::size_t i = 0; i < 142; ++i)
   {
       auto op = [](char*, std::size_t len)
       {
          return len;
       };

       std::string str;
       str.resize_and_overwrite(i, op);

       if (i != str.size())
          std::cout << i << " != " << str.size() << std::endl;
   }

   return 0;
}


Using built-in specs.
COLLECT_GCC=g++-13
COLLECT_LTO_WRAPPER=/usr/lib64/gcc/x86_64-suse-linux/13/lto-wrapper
OFFLOAD_TARGET_NAMES=nvptx-none
OFFLOAD_TARGET_DEFAULT=1
Target: x86_64-suse-linux
Configured with: ../configure CFLAGS=' -fmessage-length=0
-grecord-gcc-switches -O2 -funwind-tables -fasynchronous-unwind-tables
-fstack-clash-protection -g' CXXFLAGS=' -fmessage-length=0
-grecord-gcc-switches -O2 -funwind-tables -fasynchronous-unwind-tables
-fstack-clash-protection -g' XCFLAGS=' -fmessage-length=0
-grecord-gcc-switches -O2 -funwind-tables -fasynchronous-unwind-tables
-fstack-clash-protection -g' TCFLAGS=' -fmessage-length=0
-grecord-gcc-switches -O2 -funwind-tables -fasynchronous-unwind-tables
-fstack-clash-protection -g' GDCFLAGS=' -fmessage-length=0
-grecord-gcc-switches -O2 -funwind-tables -fasynchronous-unwind-tables
-fstack-clash-protection -g' --prefix=/usr --infodir=/usr/share/info
--mandir=/usr/share/man --libdir=/usr/lib64 --libexecdir=/usr/lib64
--enable-languages=c,c++,objc,fortran,obj-c++,ada,go,d,m2
--enable-offload-targets=nvptx-none, --enable-offload-defaulted
--without-cuda-driver --enable-checking=release --disable-werror
--with-gxx-include-dir=/usr/include/c++/13
--with-libstdcxx-zoneinfo=/usr/share/zoneinfo --enable-ssp
--disable-libssp --disable-libvtv --enable-cet=auto --disable-libcc1
--disable-plugin --with-bugurl=https://bugs.opensuse.org/
--with-pkgversion='SUSE Linux' --with-slibdir=/lib64 --with-system-zlib
--enable-libstdcxx-allocator=new --disable-libstdcxx-pch
--enable-libphobos --enable-version-specific-runtime-libs
--with-gcc-major-version-only --enable-linker-build-id
--enable-linux-futex --enable-gnu-indirect-function --program-suffix=-13
--without-system-libunwind --enable-multilib --with-arch-32=x86-64
--with-tune=generic --enable-link-serialization
--build=x86_64-suse-linux --host=x86_64-suse-linux
Thread model: posix
Supported LTO compression algorithms: zlib
gcc version 13.2.1 20230912 [revision
b96e66fd4ef3e36983969fb8cdd1956f551a074b] (SUSE Linux)




More information about the Libstdc++ mailing list