This is the mail archive of the gcc-bugs@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

libstdc++/2672: std::string.compare(char*,0,n) fails to match partial strings where string::compare(string&,0,n) does



>Number:         2672
>Category:       libstdc++
>Synopsis:       std::string.compare(char*,0,n) fails to match partial strings where string::compare(string&,0,n) does
>Confidential:   no
>Severity:       serious
>Priority:       medium
>Responsible:    unassigned
>State:          open
>Class:          sw-bug
>Submitter-Id:   net
>Arrival-Date:   Fri Apr 27 18:46:01 PDT 2001
>Closed-Date:
>Last-Modified:
>Originator:     Logan Bruns
>Release:        gcc 2.95.3
>Organization:
>Environment:
sun sparc solaris 2.8
>Description:
A partial compare against a char* only matches if the
strings are the same length. The same compare against
string& version of the previous char* argument does match
even when the strings are of different lengths. Which is 
correct behavior.

For example: (test program attached.)

  string a = "prefix::name";
  cout << "compare against char* returns "
       << a.compare("prefix::", 0, 8) << endl;

  string b = "prefix::";
  cout << "compare against string& returns "
       << a.compare(b, 0, 8) << endl;

The results of both comparisons should be the same and
zero. They are the same underlying string and the first
8 characters of a is "prefix::".

However, the first case returns 4 and the second 0.

If believe that the bug is a result of two lines that are
in compare(string&...) and not in compare(char*...).

  if (rlen == n)
    return 0;


>How-To-Repeat:
g++ -o bug bug.cc
./bug

I get the following output:

compare against char* returns 4
compare against string& returns 0
>Fix:
If you look at the two functions in lines 395-427 of
g++-3/std/bastring.cc. You'll see that the two function
bodies are very similar except that compare(char* is
missing the following two lines:

  if (rlen == n)
    return 0;

Adding these two lines at line 426 of bastring.cc would
fix the problem.
>Release-Note:
>Audit-Trail:
>Unformatted:
----gnatsweb-attachment----
Content-Type: application/octet-stream; name="bug.cc"
Content-Transfer-Encoding: base64
Content-Disposition: attachment; filename="bug.cc"

I2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8c3RyaW5nPgoKdXNpbmcgc3RkOjpzdHJpbmc7
Cgp2b2lkCm1haW4oKSB7CiAgc3RyaW5nIGEgPSAicHJlZml4OjpuYW1lIjsKICBjb3V0IDw8ICJj
b21wYXJlIGFnYWluc3QgY2hhciogcmV0dXJucyAiCiAgICAgICA8PCBhLmNvbXBhcmUoInByZWZp
eDo6IiwgMCwgOCkgPDwgZW5kbDsKCiAgc3RyaW5nIGIgPSAicHJlZml4OjoiOwogIGNvdXQgPDwg
ImNvbXBhcmUgYWdhaW5zdCBzdHJpbmcmIHJldHVybnMgIgogICAgICAgPDwgYS5jb21wYXJlKGIs
IDAsIDgpIDw8IGVuZGw7Cn0K


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]