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

[LWG #625] std::string::find_first_of() returns 0 for the empty string

    Details

    • Type: Bug Bug
    • Status: Closed
    • 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

      The program below is expected to run succeffully to completion (since the returned value must be less than the size of the string) but instead it aborts.

      $ cat t.cpp && nice gmake t && ./t
      #include <cassert>
      #include <string>

      int main ()
      {
      std::string::size_type xpos =
      std::string ("").find_first_of ("cba", 0, std::string::npos);

      assert (std::string::npos == xpos);
      }

      aCC -c -I/amd/devco/sebor/dev/stdlib/include/ansi -I/usr/include -D_RWSTDDEBUG -D_RWSTD_USE_CONFIG -I/amd/devco/sebor/dev/stdlib/include -I/build/sebor/aCC-3.70-11d/include -I/amd/devco/sebor/dev/stdlib/examples/include -Aa +nostl -g +d +w +W392 +W655 +W684 +W818 +W819 +W849 t.cpp
      aCC t.o -o t -Aa +nostl -Wl,+s -Wl,+vnocompatwarnings -L/build/sebor/aCC-3.70-11d/lib -L/build/sebor/aCC-3.70-11d/lib -lstd11d -lm
      ABORT instruction (core dumped)

        Issue Links

          Activity

          Hide
          Farid Zaripov added a comment -

          I can't repeat the issue.

          The test aborts within find_first_of() method due to RWSTD_REQUIRES (_n <= max_size (),) macro.

          Here __n is std::string::npos, but max_size() returns value less than std::string::npos.

          Another point: is that std::string::npos can be used in find_first_of() call?

          The standard says that n should be less than npos:

          21.3.7.3
          size_type
          find_first_of(const charT* s , size_type pos , size_type n ) const;
          4 Returns: find_first_of(basic_string<charT,traits,Allocator>(s ,n ),pos ).

          21.3.2
          basic_string(size_type n , charT c , const Allocator& a = Allocator());
          11 Requires: n < npos

          Show
          Farid Zaripov added a comment - I can't repeat the issue. The test aborts within find_first_of() method due to RWSTD_REQUIRES ( _n <= max_size (),) macro. Here __n is std::string::npos, but max_size() returns value less than std::string::npos. Another point: is that std::string::npos can be used in find_first_of() call? The standard says that n should be less than npos: 21.3.7.3 size_type find_first_of(const charT* s , size_type pos , size_type n ) const; 4 Returns: find_first_of(basic_string<charT,traits,Allocator>(s ,n ),pos ). 21.3.2 basic_string(size_type n , charT c , const Allocator& a = Allocator()); 11 Requires: n < npos
          Hide
          Martin Sebor added a comment -

          You just did (reproduce it): it aborted for you as well

          To answer your question: max_size() is a valid argument to find_first_of because there is no text that says is it isn't.

          The requirement you cite (that n should be less than npos) applies to to the string ctor in 21.3.2 but not to find_first_of. Returns clauses impose only one requirement: on the value returned by the function (see [structure.specifications]). Requirements on the observable side-effects of function calls (such as calls to other, for instance virtual, functions), are imposed by Effects clauses. The requirements on exceptions to be thrown are typically imposed by Throws clauses (although an Effects clause may impose the same requirement if the called function throws).

          I filed a new issue for this some time ago. See:
          http://home.twcny.rr.com/hinnant/cpp_extensions/issues_preview/lwg-active.html#625
          It's not on the official LWG issues list yet but it will be in the next mailing (in about two weeks).

          Show
          Martin Sebor added a comment - You just did (reproduce it): it aborted for you as well To answer your question: max_size() is a valid argument to find_first_of because there is no text that says is it isn't. The requirement you cite (that n should be less than npos) applies to to the string ctor in 21.3.2 but not to find_first_of. Returns clauses impose only one requirement: on the value returned by the function (see [structure.specifications] ). Requirements on the observable side-effects of function calls (such as calls to other, for instance virtual, functions), are imposed by Effects clauses. The requirements on exceptions to be thrown are typically imposed by Throws clauses (although an Effects clause may impose the same requirement if the called function throws). I filed a new issue for this some time ago. See: http://home.twcny.rr.com/hinnant/cpp_extensions/issues_preview/lwg-active.html#625 It's not on the official LWG issues list yet but it will be in the next mailing (in about two weeks).
          Hide
          Martin Sebor added a comment -

          Added an LWG #625 tag to indicate that this depends on Library Working Group issue 625:
          http://www.open-std.org/jtc1/sc22/wg21/docs/lwg-active.html#625

          Show
          Martin Sebor added a comment - Added an LWG #625 tag to indicate that this depends on Library Working Group issue 625: http://www.open-std.org/jtc1/sc22/wg21/docs/lwg-active.html#625
          Hide
          Martin Sebor added a comment -

          The test case doesn't fail anymore. I wonder if the change below fixed it. Farid, can you please confirm and close this?

          http://svn.apache.org/viewvc?view=rev&revision=559037

          Show
          Martin Sebor added a comment - The test case doesn't fail anymore. I wonder if the change below fixed it. Farid, can you please confirm and close this? http://svn.apache.org/viewvc?view=rev&revision=559037
          Hide
          Farid Zaripov added a comment -

          Yes. This issue is a part of the STDCXX-466 issue and fixed by patch for STDCXX-466.

          Show
          Farid Zaripov added a comment - Yes. This issue is a part of the STDCXX-466 issue and fixed by patch for STDCXX-466 .

            People

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

              Dates

              • Created:
                Updated:
                Resolved:

                Development