This is the mail archive of the
gcc-bugs@gcc.gnu.org
mailing list for the GCC project.
Bug in std::string::compare(const char*,size_t, size_t)
- To: gcc-bugs at gcc dot gnu dot org
- Subject: Bug in std::string::compare(const char*,size_t, size_t)
- From: Dominik Strasser <dominik dot strasser at mchp dot siemens dot de>
- Date: Mon, 25 Oct 1999 12:27:57 +0200
- CC: drepper at cygnus dot com
- Organization: Siemens AG, ZT SE 4
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 |