[Bug c++/12415] New: Bug in class std::string, method erase(pos)

wayne dot donovan at unitab dot com dot au gcc-bugzilla@gcc.gnu.org
Fri Sep 26 05:43:00 GMT 2003


PLEASE REPLY TO gcc-bugzilla@gcc.gnu.org ONLY, *NOT* gcc-bugs@gcc.gnu.org.

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=12415

           Summary: Bug in class std::string, method erase(pos)
           Product: gcc
           Version: 2.95.3
            Status: UNCONFIRMED
          Severity: normal
          Priority: P2
         Component: c++
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: wayne dot donovan at unitab dot com dot au
                CC: gcc-bugs at gcc dot gnu dot org

There is a bug in the erase(pos) method of the std::string class. This call 
should "delete the character which iterator pos refers and returns the 
position of the next character".

However, this call is deleting all characters to the end of the string 
(instead of a single character).

See example code below...
//-----------------------------------------------------------------------------
// GCC 2.95.3 STL bug in <string>
//
// Reference Source for Usage of std::string::erase()
//      The C++ Standard Library, Nicolai M. Josuttis, 4th Printing, April 2000
//      ISBN: 0201379260
//
// The following snippit removes all embedded commas in the passed in Line
//
// Line     The line of text to process and update
//
void RemoveCommas(std::string& rLine)
{
    std::string::size_type idx = rLine.length();

    while( --idx >= 0 )
    {
        if( rLine.at(idx) != ',' )      // erase this comma
        {
            rLine.erase(idx);           // BUG 1: erases until end of string
//          rLine.erase(idx, idx+1);    // works correctly, erases 1 character
//          rLine.replace(idx, 1, "");  // works correctly, erases 1 character
        }
    }
}



More information about the Gcc-bugs mailing list