Bug 50733

Summary: ostrstream is broken in function calls
Product: [Retired] Red Hat Linux Reporter: Dan Mergens <dan>
Component: gccAssignee: Jakub Jelinek <jakub>
Status: CLOSED NOTABUG QA Contact: David Lawrence <dkl>
Severity: high Docs Contact:
Priority: medium    
Version: 7.0   
Target Milestone: ---   
Target Release: ---   
Hardware: i686   
OS: Linux   
Whiteboard:
Fixed In Version: Doc Type: Bug Fix
Doc Text:
Story Points: ---
Clone Of: Environment:
Last Closed: 2001-08-08 09:47:55 UTC Type: ---
Regression: --- Mount Type: ---
Documentation: --- CRM:
Verified Versions: Category: ---
oVirt Team: --- RHEL 7.3 requirements from Atomic Host:
Cloudforms Team: --- Target Upstream Version:
Embargoed:

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:
Always

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 )
	{
		func();
	}
}

	

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
From:

http://www.cygnus.com/pubs/gnupro-97r1/4_GNUPro_Libraries/c_The_GNU_C++_Iostream_Library/libioReading_and_writing_in_memory.html
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.