no bounds checking in basic_string::operator[] despite compilation macro

Hello,

Despite setting the _RWSTD_BOUNDS_CHECKING macro with CC 5.6 (Patch 117549-06) on Solaris 8, we don't get any exception when doing array bounds write on strings. This is all the more confusing as setting this macro works for vector::operator[] and the source code in SUNWspro/prod/include/CC/Cstd/string seems simple :

template <class charT, class traits , class Allocator >

inline _TYPENAME basic_string<charT, traits,Allocator>::const_reference

basic_string<charT, traits, Allocator>::operator[] (size_type pos) const

{

#ifdef _RWSTD_BOUNDS_CHECKING

_RWSTD_THROW(pos > size(), out_of_range,

__RWSTD::except_msg_string(__RWSTD::__rwse_PosBeyondEndOfString,

"basic_string::operator[](size_t) const", pos,size()).msgstr());

#endif

return __data_.data()[pos];

}

However, on a test case like hereunder, we get no exception despite the -D_RWSTD_BOUNDS_CHECKING option :

#include <string>

using std::string;

int main()

{

string a = "0123";

a[4] = '4';

}

[1119 byte] By [Maleze] at [2007-11-26 7:50:43]
# 1

We do not support modifcation of any of the _RW* macros in the libraries that we ship.

The runtime libraries are compiled using specific macro settings. If you compile your code using different macro settings, your code will not match the runtime libraries, and the results are unpredictable. Typically, the code will not compile, will not link if it compiles, or will not run correctly if it links.

According to the C++ standard, the operator[] function has undefined behavior for out-of-range access. Throwing an exception is allowed, but not required. To improve efficiency of access, this implementation does not make any checks.

If you want bounds checking, use the "at" member function instead. It is required in all implementations to check for access and throw an exception for invalid access.

Reference: C++ Standard, section 21.3.4 "basic_string element access".

clamage45 at 2007-7-6 20:09:00 > top of Java-index,Development Tools,Solaris and Linux Development Tools...