This is the mail archive of the
gcc-bugs@gcc.gnu.org
mailing list for the GCC project.
[Bug libstdc++/48257] std::string::assign() corrupts std::string static data when called on emptyString1 using emptyString2.data()
- From: "mohsinrzaidi at gmail dot com" <gcc-bugzilla at gcc dot gnu dot org>
- To: gcc-bugs at gcc dot gnu dot org
- Date: 24 Mar 2011 06:19:56 -0000
- Subject: [Bug libstdc++/48257] std::string::assign() corrupts std::string static data when called on emptyString1 using emptyString2.data()
- Auto-submitted: auto-generated
- References: <bug-48257-4@http.gcc.gnu.org/bugzilla/>
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=48257
Mohsin <mohsinrzaidi at gmail dot com> changed:
What |Removed |Added
----------------------------------------------------------------------------
Status|RESOLVED |UNCONFIRMED
Resolution|INVALID |
--- Comment #4 from Mohsin <mohsinrzaidi at gmail dot com> 2011-03-24 06:19:52 UTC ---
(In reply to comment #3)
> In C++03 see 21.3.5.3 [lib.string.assign] paragraph 6 where
> string::assign(const char* s, size_t n) is defined to return
> assign( string(s, n) ) and that constructor:
>
> Constructs an object of class basic_string and determines its initial string
> value from the array of charT of length n whose first element is designated by
> s,
>
> In C++0x the requirement is explicit, [string::assign] paragraph 8 says
>
> Requires: s points to an array of at least n elements of charT.
I believe this is what you were referring to (from N3242=11-0012):
basic_string& assign(const charT* s, size_type n);
8 Requires: s points to an array of at least n elements of charT.
9 Throws: length_error if n > max_size().
10 Effects: Replaces the string controlled by *this with a string of length n
whose elements are a copy
of those pointed to by s.
11 Returns: *this.
Thanks for pointing this out. However, does libstdc++ throw and error if "n >
max_size()"? I don't think it does.
>From the latest libstdc++ documentation:
<snip>
template<typename _CharT, typename _Traits , typename _Alloc >
basic_string< _CharT, _Traits, _Alloc > & std::basic_string< _CharT, _Traits,
_Alloc >::assign ( const _CharT * __s,
size_type __n
)
Set value to a C substring.
Parameters:
s The C string to use.
n Number of characters to use.
Returns:
Reference to this string.
This function sets the value of this string to the first n characters of s. If
n is is larger than the number of available characters in s, the remainder of s
is used.
Definition at line 261 of file basic_string.tcc.
References std::basic_string< _CharT, _Traits, _Alloc >::size().
</snip>
I don't see this as saying that the string has to have at least n elements OR
that an error will be thrown if it isn't.
>From the latest basic_string code:
<snip>
template<typename _CharT, typename _Traits, typename _Alloc>
00259 basic_string<_CharT, _Traits, _Alloc>&
00260 basic_string<_CharT, _Traits, _Alloc>::
00261 assign(const _CharT* __s, size_type __n)
00262 {
00263 __glibcxx_requires_string_len(__s, __n);
00264 _M_check_length(this->size(), __n, "basic_string::assign");
00265 if (_M_disjunct(__s) || _M_rep()->_M_is_shared())
00266 return _M_replace_safe(size_type(0), this->size(), __s, __n);
00267 else
00268 {
00269 // Work in-place.
00270 const size_type __pos = __s - _M_data();
00271 if (__pos >= __n)
00272 _M_copy(_M_data(), __s, __n);
00273 else if (__pos)
00274 _M_move(_M_data(), __s, __n);
00275 _M_rep()->_M_set_length_and_sharable(__n);
00276 return *this;
00277 }
00278 }
</snip>
I don't see any errors being thrown. I am not trying to nit-pick but I've
raised this issue because I was affected by it and had to spend several days
isolating it. It would have made my life a whole lot easier if, as you claim,
libstdc++ had thrown an error.
Just a request, but please let the discussion complete before you go ahead and
mark the bug as Resolved-Invalid. I don't mind it being marked invalid, but I
consider it disrespectful for you to not even wait for my response and rushing
to mark it as invalid. Once we're through discussing, I will mark it invalid
myself if necessary.