https://gcc.gnu.org/bugzilla/show_bug.cgi?id=65022

            Bug ID: 65022
           Summary: basic_string operator
           Product: gcc
           Version: 5.0
            Status: UNCONFIRMED
          Severity: critical
          Priority: P3
         Component: libstdc++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: wbrana at gmail dot com

operators don't return reference if out of range

http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2014/n4296.pdf

21.4.5 basic_string element access [string.access]

const_reference operator[](size_type pos) const;
reference operator[](size_type pos);
1 Requires: pos <= size().
2 Returns: *(begin() + pos) if pos < size(). Otherwise, returns a reference to
an object of type
charT with value charT(), where modifying the object leads to undefined
behavior.
3 Throws: Nothing.
4 Complexity: Constant time.

https://gcc.gnu.org/git/?p=gcc.git;a=blob_plain;f=libstdc%2B%2B-v3/include/bits/basic_string.h;hb=HEAD

      // Element access:
      /**
       *  @brief  Subscript access to the data contained in the %string.
       *  @param  __pos  The index of the character to access.
       *  @return  Read-only (constant) reference to the character.
       *
       *  This operator allows for easy, array-style, data access.
       *  Note that data access with this operator is unchecked and
       *  out_of_range lookups are not defined. (For checked lookups
       *  see at().)
       */
      const_reference
      operator[] (size_type __pos) const
      {
        _GLIBCXX_DEBUG_ASSERT(__pos <= size());
        return _M_data()[__pos];
      }

      /**
       *  @brief  Subscript access to the data contained in the %string.
       *  @param  __pos  The index of the character to access.
       *  @return  Read/write reference to the character.
       *
       *  This operator allows for easy, array-style, data access.
       *  Note that data access with this operator is unchecked and
       *  out_of_range lookups are not defined. (For checked lookups
       *  see at().)  Unshares the string.
       */
      reference
      operator[](size_type __pos)
      {
        // allow pos == size() as v3 extension:
        _GLIBCXX_DEBUG_ASSERT(__pos <= size());
        // but be strict in pedantic mode:
        _GLIBCXX_DEBUG_PEDASSERT(__pos < size());
        _M_leak();
        return _M_data()[__pos];
      }

Reply via email to