This is the mail archive of the
gcc-bugs@gcc.gnu.org
mailing list for the GCC project.
[Bug libstdc++/43634] New: std::string::replace with C string can replace too many characters
- From: "poftwaresatent at gmail dot com" <gcc-bugzilla at gcc dot gnu dot org>
- To: gcc-bugs at gcc dot gnu dot org
- Date: 2 Apr 2010 23:46:05 -0000
- Subject: [Bug libstdc++/43634] New: std::string::replace with C string can replace too many characters
- Reply-to: gcc-bugzilla at gcc dot gnu dot org
gcc version 4.4.3 20100108 (prerelease) (Debian 4.4.2-9)
basic_string::replace(size_type __pos, size_type __n1, const _CharT* __s)
replaces length(__s) characters even if __n1<length(__s), which is not the
behavior documented in the header, on
http://www.cplusplus.com/reference/string/string/replace/ , or in Josuttis "The
C++ Standard Library" (Addison Wesley).
Suggested fix: it would suffice to take the min() of __n1 and length(__s)
instead of just the latter. In my version, that's on line 1324 of
/usr/include/c++/4.4.3/bits/basic_string.h
Workaround: use the other replace() signature that allows to explicitly set a C
string length.
See also test_std_string.cpp:
// g++ -Wall -g -O0 -o test_std_string test_std_string.cpp
#include <string>
#include <iostream>
int main(int argc, char ** argv)
{
static char const * foo_cstr("fooXXX");
std::string foo("something else");
foo.resize(3);
foo.replace(0, 3, foo_cstr);
if (foo != "foo") {
std::cout << "foo.replace(0, 3, foo_cstr) failed: got \"" << foo << "\"
instead of \"foo\"\n";
}
foo.resize(3);
foo.replace(0, 3, foo_cstr, 3);
if (foo != "foo") {
std::cout << "foo.replace(0, 3, foo_cstr, 3) failed: got \"" << foo << "\"
instead of \"foo\"\n";
}
}
--
Summary: std::string::replace with C string can replace too many
characters
Product: gcc
Version: 4.4.2
Status: UNCONFIRMED
Severity: major
Priority: P3
Component: libstdc++
AssignedTo: unassigned at gcc dot gnu dot org
ReportedBy: poftwaresatent at gmail dot com
GCC target triplet: i486-linux-gnu
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=43634