public inbox for gcc-prs@sourceware.org
help / color / mirror / Atom feed
* libstdc++/5737: ostringstream::str() appends \0 character
@ 2002-02-20 10:06 wolfgang.bangerth
  0 siblings, 0 replies; 3+ messages in thread
From: wolfgang.bangerth @ 2002-02-20 10:06 UTC (permalink / raw)
  To: gcc-gnats


>Number:         5737
>Category:       libstdc++
>Synopsis:       ostringstream::str() appends \0 character
>Confidential:   no
>Severity:       serious
>Priority:       medium
>Responsible:    unassigned
>State:          open
>Class:          sw-bug
>Submitter-Id:   net
>Arrival-Date:   Wed Feb 20 10:06:04 PST 2002
>Closed-Date:
>Last-Modified:
>Originator:     Wolfgang Bangerth
>Release:        unknown-1.0
>Organization:
>Environment:
Linux 2.4.14, gcc version 3.1 20020215 (experimental)
I believe that gcc3.0.x has the same problem
>Description:
When extracting a string from an ostringstream object using
the str() function, the std::string also has stored a final
\0 character. This is not usually visible in output since
such characters are not printed, but of course is very much
confusing when your programs tells you that the string "x"
has characters other than 'x'!

I think that originally the \0 slips in when you "close" the
ostringstream object by std::ends, but it is not stripped
when the internal string is copied to the result of str().

Regards
  Wolfgang
>How-To-Repeat:
Use this program to see what happens:
-------------------------------------
#include <sstream>
#include <iostream>

using namespace std;

int main () {
				   // create a string using an ostringstream
  ostringstream s;  s << "x" << ends;

				   // output its content and size
  cout << '\"' << s.str() << '\"' << endl;
  cout << s.str().size() << endl;

				   // output each of its characters
  for (unsigned int i=0; i<s.str().size(); ++i)
    cout << "Character " << i << ": \'" << s.str()[i]
	 << "\' (value=" << static_cast<int>(s.str()[i]) << ")" << endl;
};
-----------------------------------------------

This is its output:
  "x" 
  2
  Character 0: 'x' (value=120)
  Character 1: '' (value=0)
Clearly the second character should not be there.
>Fix:

>Release-Note:
>Audit-Trail:
>Unformatted:


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: libstdc++/5737: ostringstream::str() appends \0 character
@ 2002-02-20 12:37 paolo
  0 siblings, 0 replies; 3+ messages in thread
From: paolo @ 2002-02-20 12:37 UTC (permalink / raw)
  To: gcc-bugs, gcc-prs, nobody, paolo, wolfgang.bangerth

Synopsis: ostringstream::str() appends \0 character

Responsible-Changed-From-To: unassigned->paolo
Responsible-Changed-By: paolo
Responsible-Changed-When: Wed Feb 20 11:00:22 2002
Responsible-Changed-Why:
    Analyzed
State-Changed-From-To: open->closed
State-Changed-By: paolo
State-Changed-When: Wed Feb 20 11:00:22 2002
State-Changed-Why:
    Not a bug.
    If you don't want '\0' simply don't output it with << ends!
    The semantics of the ends manipulator is exactly that of
    outputting a '\0', not that of "closing" in any sensible
    meaning (see, f.i., Josuttis, pp. 587, 612, 650).
    More generally, an user of the C++ string class is *never*
    required to deal with termination chars of any sort.
    The ends manipulator is really there only to deal with char*
    (i.e., "C") strings (see Josuttis, p. 649-650).
    Thanks for your report, Paolo.

http://gcc.gnu.org/cgi-bin/gnatsweb.pl?cmd=view%20audit-trail&database=gcc&pr=5737


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: libstdc++/5737: ostringstream::str() appends \0 character
@ 2002-02-20 10:36 Carlo Wood
  0 siblings, 0 replies; 3+ messages in thread
From: Carlo Wood @ 2002-02-20 10:36 UTC (permalink / raw)
  To: nobody; +Cc: gcc-prs

The following reply was made to PR libstdc++/5737; it has been noted by GNATS.

From: Carlo Wood <carlo@alinoe.com>
To: wolfgang.bangerth@iwr.uni-heidelberg.de
Cc: gcc-gnats@gcc.gnu.org
Subject: Re: libstdc++/5737: ostringstream::str() appends \0 character
Date: Wed, 20 Feb 2002 19:18:56 +0100

 >   ostringstream s;  s << "x" << ends;
 
 The 'ends' writes the '\0' to the stream.
 This is not a bug.
 
 -- 
 Carlo Wood <carlo@alinoe.com>


^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2002-02-20 19:00 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2002-02-20 10:06 libstdc++/5737: ostringstream::str() appends \0 character wolfgang.bangerth
2002-02-20 10:36 Carlo Wood
2002-02-20 12:37 paolo

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).