Home Dashboard Directory Help
Search

debug assertion "string subscript out of range" is incorrect for [size()] by hhoppe


Status: 

Closed
 as Fixed Help for as Fixed


1
0
Sign in
to vote
Type: Bug
ID: 733729
Opened: 3/27/2012 2:47:36 PM
Access Restriction: Public
Moderator Decision: Sent to Engineering Team for consideration
0
Workaround(s)
view
0
User(s) can reproduce this bug

Description

Given an empty string, operator[](0) shows a debug assertion error "string subscript out of range".

The C++ standard (both before and after C++11) specify that operator[](size()) should return a reference to the value CharT() (the null character):

See http://en.cppreference.com/w/cpp/string/basic_string/operator_at :

If pos==size(),
    The const version returns a reference to the character with value CharT() (the null character). (until C++11)
    Both versions returns a reference to the character with value CharT() (the null character). Modifying the null character through non-const reference results in undefined behavior. (since C++11)

In other words, no assertion error should be reported when pos==size() .
Details
Sign in to post a comment.
Posted by hhoppe on 3/29/2012 at 9:47 AM
Thanks for the clear, detailed discussion.
The C++03 "undefined" behavior was not so useful in my opinion.
I look forward to the VC11 release.
Posted by Microsoft on 3/28/2012 at 9:30 PM
Hi,

Thanks for reporting this bug. We've already fixed it, and you can observe the fix in the VC11 Beta.

This actually changed between C++03 and C++11. C++03 21.3.4 [lib.string.access]/1 said:

"const_reference operator[](size_type pos) const;
reference operator[](size_type pos);
Returns: If pos < size(), returns data()[pos]. Otherwise, if pos == size(), the const version returns charT(). Otherwise, the behavior is undefined."

VC10's debug assertions strictly enforced these rules. In particular, they allowed s[s.size()] for const strings, but prohibited it for modifiable strings.

Now, C++11 21.4.5 [string.access]/1-2 says:

"const_reference operator[](size_type pos) const;
reference operator[](size_type pos);
Requires: pos <= size().
Returns: *(begin() + pos) if pos < size(), otherwise a reference to an object of type T with value charT(); the referenced value shall not be modified."

Therefore, we changed VC11 to allow s[s.size()] for both const and modifiable strings.

If you need to work around this in VC10, you can say: static_cast<const string&>(s)[s.size()]

If you have any further questions, feel free to E-mail me at stl@microsoft.com .

Stephan T. Lavavej
Visual C++ Libraries Developer
Posted by MS-Moderator08 [Feedback Moderator] on 3/28/2012 at 12:41 AM
Thank you for submitting feedback on Visual Studio 11 and .NET Framework. Your issue has been routed to the appropriate VS development team for review. We will contact you if we require any additional information.

Posted by MS-Moderator01 on 3/27/2012 at 3:50 PM
Thank you for your feedback, we are currently reviewing the issue you have submitted. If this issue is urgent, please contact support directly(http://support.microsoft.com)
Sign in to post a workaround.