c++/325: string::find("\0") gives wrong result

heiko.scheit@mpi-hd.mpg.de heiko.scheit@mpi-hd.mpg.de
Thu Jun 15 04:46:00 GMT 2000


>Number:         325
>Category:       c++
>Synopsis:       string::find("\0") gives wrong result
>Confidential:   no
>Severity:       serious
>Priority:       medium
>Responsible:    unassigned
>State:          open
>Class:          sw-bug
>Submitter-Id:   net
>Arrival-Date:   Thu Jun 15 04:46:00 PDT 2000
>Closed-Date:
>Last-Modified:
>Originator:     Heiko Scheit
>Release:        unknown-1.0
>Organization:
>Environment:

>Description:
Below the string test is initialized to "test" and then
it is checked at what position "\0" occurs in the string.
test.find("\0") returns a wrong result of 4, whereas 
string::npos should be returned.  A work around 
is to user find_last_of("\0").

~/junk> cat test.C
#include <iostream>
#include <string>

#define SHOW(A) #A " ="<<A
main(){
  string test("test");
  cout<<SHOW(test.size())<<endl;
  cout<<SHOW(test.find("\0"))<<endl;
  cout<<SHOW(test.find_last_of("\0"))<<endl;
  cout<<SHOW(string::npos)<<endl;
}
~/junk> g++ test.C
~/junk> g++ -v
Reading specs from /usr/lib/gcc-lib/i486-linux/egcs-2.91.66/specs
gcc version egcs-2.91.66 Debian GNU/Linux (egcs-1.1.2 release)

~/junk> uname
Linux
~/junk> gcc -v
Reading specs from /usr/lib/gcc-lib/i486-linux/2.7.2.3/specs
gcc version 2.7.2.3

~/junk> ./a.out
test.size() =4
test.find("\0") =4
test.find_last_of("\0") =4294967295
string::npos =4294967295
>How-To-Repeat:
#include <iostream>
#include <string>

#define SHOW(A) #A " ="<<A
main(){
  string test("test");
  cout<<SHOW(test.size())<<endl;
  cout<<SHOW(test.find("\0"))<<endl;
  cout<<SHOW(test.find_last_of("\0"))<<endl;
  cout<<SHOW(string::npos)<<endl;
}
>Fix:
use find_last_of("\0") or check that 
string::size() is larger than string::find("\0")
which it should always be
>Release-Note:
>Audit-Trail:
>Unformatted:


More information about the Gcc-prs mailing list