libstdc++/8636: global ostringstreams lose their data

michael.pronath@gmx.de michael.pronath@gmx.de
Tue Nov 19 05:48:00 GMT 2002


>Number:         8636
>Category:       libstdc++
>Synopsis:       global ostringstreams lose their data
>Confidential:   no
>Severity:       serious
>Priority:       medium
>Responsible:    unassigned
>State:          open
>Class:          sw-bug
>Submitter-Id:   net
>Arrival-Date:   Tue Nov 19 05:46:05 PST 2002
>Closed-Date:
>Last-Modified:
>Originator:     michael.pronath@gmx.de
>Release:        unknown-1.0
>Organization:
>Environment:
Linux glibc2 g++ 3.2
both in debian package and when compiled from source
>Description:
global ostringstreams lose their data after calling
c_str on them when new memory is allocated.
See appended example.
Expected output: Hello World, where are you?
real output:     Hello Hello Hello Hello Hell

reason: when fillmystream() returns, the ostringstream content is forgotten, just like it was an automatic
variable.  The string operator+= then allocates new memory
for extension of the string and gets the address of x and
then appends itself.

Interestingly, this does not happen in valgrind.
>How-To-Repeat:
g++ -o teststream teststream.cc
./teststream
valgrind ./teststream
>Fix:

>Release-Note:
>Audit-Trail:
>Unformatted:
----gnatsweb-attachment----
Content-Type: application/octet-stream; name="teststream.cc"
Content-Transfer-Encoding: base64
Content-Disposition: attachment; filename="teststream.cc"

I2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8c3N0cmVhbT4KI2luY2x1ZGUgPHN0cmluZz4K
CnVzaW5nIG5hbWVzcGFjZSBzdGQ7Cgpvc3RyaW5nc3RyZWFtIHRlc3RzdHJlYW07CmNvbnN0IGNo
YXIgKng7Cgp2b2lkIGZpbGxteXN0cmVhbSgpCnsKICAgdGVzdHN0cmVhbSA8PCAiIFdvcmxkLCB3
aGVyZSBhcmUgeW91PyI7CiAgIHggPSB0ZXN0c3RyZWFtLnN0cigpLmNfc3RyKCk7CiAgIHJldHVy
bjsKfQogICAKaW50IG1haW4oKQp7CiAgIHN0cmluZyB0ZXN0c3RyaW5nPSIiOwogICBmaWxsbXlz
dHJlYW0oKTsKICAgdGVzdHN0cmluZyA9ICJIZWxsbyAiOwogICB0ZXN0c3RyaW5nICs9IHg7CiAg
IGNvdXQgPDwgInRlc3RzdHJpbmcgaXMgJyIgPDwgdGVzdHN0cmluZyA8PCAiJyIgPDwgZW5kbDsK
fQoK



More information about the Gcc-bugs mailing list