Bug in std::string::compare(const char*,size_t, size_t)

Dominik Strasser dominik.strasser@mchp.siemens.de
Mon Oct 25 03:28:00 GMT 1999


There is a bug in the above function in gcc-2.95.1 (C++ lib)
It can be easily reproduced by the code below:

#include <string>
#include <iostream>

using namespace std;

int main()
{
        string a = "Kuckuck";

        if(a.compare("Kuck", 0, 4)==0)
                cout << "OK";
        else
                cout << "NOK";
        cout << endl;
        if(a.compare(string("Kuck"), 0, 4)==0)
                cout << "OK";
        else
                cout << "NOK";
        cout << endl;
        return 0;
}
The output is:
NOK
OK.

The following fix corrects the behavior:
*** /usr/local/include/g++-3/std/bastring.cc    Mon Oct 25 12:23:04 1999
--- ./bastring.cc       Mon Oct 25 12:20:54 1999
***************
*** 423,428 ****
--- 423,430 ----
    int r = traits::compare (data () + pos, s, rlen);
    if (r != 0)
      return r;
+   if (rlen == n)
+     return 0;
    return (length () - pos) - n;
  }

Regards

Dominik

-- 
Dominik Strasser	| Phone:  +49 89 636-43691
SIEMENS AG		| Fax:    +49 89 636-42284
ZT SE 4			| E-Mail:Dominik.Strasser@mchp.siemens.de
Otto-Hahn-Ring 6	|
D-81739 Muenchen	|


More information about the Gcc-bugs mailing list