C++ Standard Library
  1. C++ Standard Library
  2. STDCXX-214

[LWG #467] std::char_traits<char>::compare() broken for negative characters

    Details

    • Type: Bug Bug
    • Status: Resolved
    • Priority: Major Major
    • Resolution: Fixed
    • Affects Version/s: 4.1.2, 4.1.3
    • Fix Version/s: 4.2.0
    • Component/s: 21. Strings
    • Labels:
      None
    • Environment:

      all

      Description

      Moved from the Rogue Wave bug tracking database:

      ***Created By: sebor @ May 03, 2004 03:04:54 PM***
      According to Table 37 in [lib.char.traits.require]:

      X::compare(p,q,n) yields: 0 if for each i in [0,n), X::eq(p[i],q[i]) is true; else, a negative value if, for some j in [0,n), X::lt(p[j],q[j]) is true and for each i in [0,j) X::eq(p[i],q[i]) is true; else a positive value.

      The following fails with gcc 3.4 on SunOS 5.7 because char_traits<char>::compare() uses memcmp() which, on this platform, compares characters as if they were unsigned.

      $ cat t.cpp && ./t
      /* 1 */ #include <cassert>
      /* 2 */ #include <string>
      /* 3 */
      /* 4 */ int main ()
      /* 5 */

      { /* 6 */ typedef std::char_traits<char> Traits; /* 7 */ /* 8 */ const char s[] = "\0\377"; /* 9 */ /* 10 */ if (Traits::lt (s [0], s [1])) /* 11 */ assert (Traits::compare (s + 0, s + 1, 1) < 0); /* 12 */ else /* 13 */ assert (Traits::compare (s + 0, s + 1, 1) > 0); /* 14 */ }

      /* 15 */
      Assertion failed: Traits::compare (s + 0, s + 1, 1) > 0, file t.cpp, line 13
      Abort (core dumped)

        Activity

        Hide
        Martin Sebor added a comment -

        Fixed. Need to add a test to the test suite.

        Show
        Martin Sebor added a comment - Fixed. Need to add a test to the test suite.
        Hide
        Martin Sebor added a comment -

        This problem is the subject of Library Working Group issue 467:
        http://www.open-std.org/jtc1/sc22/wg21/docs/lwg-defects.html#467

        Show
        Martin Sebor added a comment - This problem is the subject of Library Working Group issue 467: http://www.open-std.org/jtc1/sc22/wg21/docs/lwg-defects.html#467

          People

          • Assignee:
            Martin Sebor
            Reporter:
            Martin Sebor
          • Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved:

              Development