]> git.ipfire.org Git - thirdparty/gcc.git/commit
libstdc++: Fix std::regex_replace for strings with embedded null [PR103664]
authorJonathan Wakely <jwakely@redhat.com>
Sun, 12 Dec 2021 21:15:17 +0000 (21:15 +0000)
committerJonathan Wakely <jwakely@redhat.com>
Fri, 23 Jun 2023 12:36:31 +0000 (13:36 +0100)
commit364cb498c472790e14561f7672dc5ab4a9222287
treec75276ae1774beb0a56def789598855cd94b8ce1
parentd877bf3bdf46b5c996505fc247d170e79fbfa4bf
libstdc++: Fix std::regex_replace for strings with embedded null [PR103664]

The overload of std::regex_replace that takes a std::basic_string as the
fmt argument (for the replacement string) is implemented in terms of the
one taking a const C*, which uses std::char_traits to find the length.
That means it stops at a null character, even though the basic_string
might have additional characters beyond that.

Rather than duplicate the implementation of the const C* one for the
std::basic_string case, this moves that implementation to a new
__regex_replace function which takes a const C* and a length. Then both
the std::basic_string and const C* overloads can call that (with the
latter using char_traits to find the length to pass to the new
function).

libstdc++-v3/ChangeLog:

PR libstdc++/103664
* include/bits/regex.h (__regex_replace): Declare.
(regex_replace): Use it.
* include/bits/regex.tcc (__regex_replace): Replace regex_replace
definition with __regex_replace.
* testsuite/28_regex/algorithms/regex_replace/char/103664.cc: New test.

(cherry picked from commit ef5d671cd80a4afa4f74c3dfe2904c63f51fcfde)
libstdc++-v3/include/bits/regex.h
libstdc++-v3/include/bits/regex.tcc
libstdc++-v3/testsuite/28_regex/algorithms/regex_replace/char/103664.cc [new file with mode: 0644]