public inbox for gcc-prs@sourceware.org
help / color / mirror / Atom feed
* Re: libstdc++/4354: string::assign behaving unexpectedly when destination string == source string
@ 2001-11-04 7:52 bkoz
0 siblings, 0 replies; 4+ messages in thread
From: bkoz @ 2001-11-04 7:52 UTC (permalink / raw)
To: bkoz; +Cc: gcc-prs
The following reply was made to PR libstdc++/4354; it has been noted by GNATS.
From: bkoz@gcc.gnu.org
To: bagnara@cs.unipr.it, bkoz@gcc.gnu.org, gcc-bugs@gcc.gnu.org,
gcc-gnats@gcc.gnu.org, gcc-prs@gcc.gnu.org, ncm@cantrip.org,
pcarlini@unitus.it
Cc:
Subject: Re: libstdc++/4354: string::assign behaving unexpectedly when destination string == source string
Date: 15 Nov 2001 20:40:13 -0000
Synopsis: string::assign behaving unexpectedly when destination string == source string
State-Changed-From-To: feedback->closed
State-Changed-By: bkoz
State-Changed-When: Thu Nov 15 12:40:12 2001
State-Changed-Why:
Fixed.
http://gcc.gnu.org/cgi-bin/gnatsweb.pl?cmd=view%20audit-trail&pr=4354&database=gcc
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: libstdc++/4354: string::assign behaving unexpectedly when destination string == source string
@ 2001-11-04 3:21 bkoz
0 siblings, 0 replies; 4+ messages in thread
From: bkoz @ 2001-11-04 3:21 UTC (permalink / raw)
To: bagnara, bkoz, gcc-bugs, gcc-gnats, gcc-prs, ncm, pcarlini
Synopsis: string::assign behaving unexpectedly when destination string == source string
State-Changed-From-To: feedback->closed
State-Changed-By: bkoz
State-Changed-When: Thu Nov 15 12:40:12 2001
State-Changed-Why:
Fixed.
http://gcc.gnu.org/cgi-bin/gnatsweb.pl?cmd=view%20audit-trail&pr=4354&database=gcc
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: libstdc++/4354: string::assign behaving unexpectedly when destination string == source string
@ 2001-10-31 0:40 bkoz
0 siblings, 0 replies; 4+ messages in thread
From: bkoz @ 2001-10-31 0:40 UTC (permalink / raw)
To: bagnara, bkoz, gcc-bugs, gcc-prs, ncm, nobody, pcarlini
Synopsis: string::assign behaving unexpectedly when destination string == source string
Responsible-Changed-From-To: unassigned->bkoz
Responsible-Changed-By: bkoz
Responsible-Changed-When: Wed Oct 31 00:40:45 2001
Responsible-Changed-Why:
Mine.
State-Changed-From-To: open->feedback
State-Changed-By: bkoz
State-Changed-When: Wed Oct 31 00:40:45 2001
State-Changed-Why:
Thanks Paolo!
This patch is in mainline, I've also adjusted the testsuites to test for these cases.
-benjamin
http://gcc.gnu.org/cgi-bin/gnatsweb.pl?cmd=view%20audit-trail&pr=4354&database=gcc
^ permalink raw reply [flat|nested] 4+ messages in thread
* libstdc++/4354: string::assign behaving unexpectedly when destination string == source string
@ 2001-09-18 16:46 pcarlini
0 siblings, 0 replies; 4+ messages in thread
From: pcarlini @ 2001-09-18 16:46 UTC (permalink / raw)
To: gcc-gnats; +Cc: ncm, bagnara
>Number: 4354
>Category: libstdc++
>Synopsis: string::assign behaving unexpectedly when destination string == source string
>Confidential: no
>Severity: serious
>Priority: medium
>Responsible: unassigned
>State: open
>Class: sw-bug
>Submitter-Id: net
>Arrival-Date: Tue Sep 18 16:46:00 PDT 2001
>Closed-Date:
>Last-Modified:
>Originator: Paolo Carlini / Roberto Bagnara
>Release: gcc-3.0.1 (current 3.1 and 3.0.2 snapshots too)
>Organization:
>Environment:
x86, glibc-2.2.4, linux-2.4.8, binutils-2.11.2
>Description:
The following simple program prints "merge" instead of
"mergesort_ap_variant.pl", as it (probably) should:
#include <iostream>
#include <string>
using namespace std;
int main() {
string aux = "../BenchCtiTr/apt/mergesort_ap_variant.pl";
string::size_type i = aux.rfind("/");
if (i != string::npos)
aux.assign(aux, i+1, string::npos);
cout << aux.c_str() << endl;
}
>How-To-Repeat:
The member function string::assign behaves unexpectedly when
dealing with the same string as source and destination.
>Fix:
I traced back the problem to the following function in
basic_string.tcc (lines 442-462):
--------------------------------------
template<typename _CharT, typename _Traits, typename _Alloc>
template<typename _ForwardIter>
basic_string<_CharT, _Traits, _Alloc>&
basic_string<_CharT, _Traits, _Alloc>::
_M_replace(iterator __i1, iterator __i2, _ForwardIter __k1,
_ForwardIter __k2, forward_iterator_tag)
{
size_type __dold = __i2 - __i1;
size_type __dmax = this->max_size();
size_type __dnew = static_cast<size_type>(distance(__k1, __k2));
if (__dmax <= __dnew)
__throw_length_error("basic_string::_M_replace");
size_type __off = __i1 - _M_ibegin();
_M_mutate(__off, __dold, __dnew);
// Invalidated __i1, __i2
if (__dnew)
_S_copy_chars(_M_data() + __off, __k1, __k2);
return *this;
}
---------------------------------------
Irrespective of the fact that we are dealing with the
very same string as source and destination, _M_mutate is
called (with __dold == 41 and __dnew == 23) **before** the
actual copy operation (_S_copy_chars) is carried out.
Therefore, the aux string, as source of the assign, is
clobbered, i.e. is truncated at the 23rd character
("../BenchCtiTr/apt/merge"), and the eventual copy
operation (_S_copy_chars) produces aux == "merge".
Indeed, tentatively exchanging the _M_mutate and
_S_copy_chars calls leads to the expected behaviour when
source string == destination string.
Therefore, it seems likely that this special case should be
detected and dealt with in that way (i.e., swapping the
aforementioned calls) modulo perhaps reference counting
issues which I do not fully understand at present :(
>Release-Note:
>Audit-Trail:
>Unformatted:
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2001-11-15 20:46 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2001-11-04 7:52 libstdc++/4354: string::assign behaving unexpectedly when destination string == source string bkoz
-- strict thread matches above, loose matches on Subject: below --
2001-11-04 3:21 bkoz
2001-10-31 0:40 bkoz
2001-09-18 16:46 pcarlini
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).