Bug 50733 - ostrstream is broken in function calls
Summary: ostrstream is broken in function calls
Alias: None
Product: Red Hat Linux
Classification: Retired
Component: gcc
Version: 7.0
Hardware: i686
OS: Linux
Target Milestone: ---
Assignee: Jakub Jelinek
QA Contact: David Lawrence
Depends On:
TreeView+ depends on / blocked
Reported: 2001-08-02 18:29 UTC by Dan Mergens
Modified: 2007-04-18 16:35 UTC (History)
0 users

Clone Of:
Last Closed: 2001-08-08 09:47:55 UTC

Attachments (Terms of Use)

Description Dan Mergens 2001-08-02 18:29:12 UTC
From Bugzilla Helper:
User-Agent: Mozilla/4.76 [en] (X11; U; Linux 2.2.16-22 i686)

Description of problem:
An error occurs where garbage is added to the end of ostrstream strings.
This occurs when a function is called from a catch block when the function
allocates the ostrstream locally.

How reproducible:

Steps to Reproduce:
Compile and run the following code:
#include <strstream.h>
#include <stdio.h>
#include <stdlib.h>

void func()
	ostrstream os;

	os << "hello" << endl;
	cout << os.str() << endl;

int main( int argc, char* const* argv )
	try {
			throw( "error" );
	catch( const char* err )


Actual Results:  hello
@*@,*@T*@t*@*@<*@\*@|*@+@@+@`+@+@,+@T+@t+@?,@8,@X,@?,@ ,@@,@h,-@a

Expected Results:  hello

Additional info:

This problem does not occur when the ostrstream is local to the main
routine or if called from outside the catch block.

I would classify this with loss of data as printing of information is
trashed. (loss of data)

Comment 1 Dan Mergens 2001-08-02 18:51:09 UTC
I had another problem that wasn't so easily isolated, but is similar. When I
print out "" it is sometimes replaced with "(null)". This is a consistent bug in
my code, but I can't seem to get a simple test case to show it.

Comment 2 Jeremy Sanders 2001-08-08 09:47:50 UTC

The char* the str() function returns isn't null terminated!!

You need to do

ostrstream o;
o << "Hi there" << endl << '\0';

Comment 3 Jakub Jelinek 2001-08-08 10:21:54 UTC
Yeah, either that or os << "hello" << endl << ends;

Comment 4 Dan Mergens 2001-08-08 17:03:10 UTC
Yes, that is the obvious workaround. However, str() is supposed to add the string terminator. The programmer is NOT supposed to do it manually. Notice 
that this only happens in the above test case which suggests a subtle problem with the try/catch mechanism. Since I can't PROVE that str() is supposed 
to add the null terminator (although after years of programming C++ I've never had a problem with it), I'll concede that it is not an official bug.

Comment 5 Jakub Jelinek 2001-08-08 17:18:49 UTC
The above URL clearly states that you HAVE TO do it manually and str()
is not supposed to do it for you.
Checking libstdc++-v3 from GCC 3.0.1, it has exactly the same behaviour.
Reading [depr.strstreambuf] section in ISO C++ does not say anything that
str() should do it, it should just freeze() and then return the beginning.

Note You need to log in before you can comment on or make changes to this bug.