[committed 3/4] libstdc++: Fix undefined behaviour in std::string

Jonathan Wakely jwakely@redhat.com
Tue May 4 22:19:48 GMT 2021


This fixes a ubsan error when constructing a string with a null pointer:

bits/basic_string.h:534:21: runtime error: applying non-zero offset 18446744073709551615 to null pointer

The _M_construct function only cares whether the second pointer is
non-null, so create a non-null value without undefined arithmetic.

We can also pass the random_access_iterator_tag directly to the
_M_construct function, to avoid going via the tag dispatching
_M_construct_aux, because we know we have pointers not integers here.

libstdc++-v3/ChangeLog:

         * include/bits/basic_string.h (basic_string(const CharT*, const A&)):
         Do not do arithmetic on null pointer.

Tested x86_64-linux. Committed to trunk.



-------------- next part --------------
A non-text attachment was scrubbed...
Name: patch.txt
Type: text/x-patch
Size: 1722 bytes
Desc: not available
URL: <https://gcc.gnu.org/pipermail/libstdc++/attachments/20210504/36b93c6c/attachment.bin>


More information about the Libstdc++ mailing list