Bug 50733 - ostrstream is broken in function calls
ostrstream is broken in function calls
Status: CLOSED NOTABUG
Product: Red Hat Linux
Classification: Retired
Component: gcc (Show other bugs)
7.0
i686 Linux
medium Severity high
: ---
: ---
Assigned To: Jakub Jelinek
David Lawrence
:
Depends On:
Blocks:
  Show dependency treegraph
 
Reported: 2001-08-02 14:29 EDT by Dan Mergens
Modified: 2007-04-18 12:35 EDT (History)
0 users

See Also:
Fixed In Version:
Doc Type: Bug Fix
Doc Text:
Story Points: ---
Clone Of:
Environment:
Last Closed: 2001-08-08 05:47:55 EDT
Type: ---
Regression: ---
Mount Type: ---
Documentation: ---
CRM:
Verified Versions:
Category: ---
oVirt Team: ---
RHEL 7.3 requirements from Atomic Host:
Cloudforms Team: ---


Attachments (Terms of Use)

  None (edit)
Description Dan Mergens 2001-08-02 14:29:12 EDT
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 14:51:09 EDT
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 05:47:50 EDT
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 06:21:54 EDT
Yeah, either that or os << "hello" << endl << ends;
Comment 4 Dan Mergens 2001-08-08 13:03:10 EDT
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 13:18:49 EDT
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.