Thread View: gnu.g++.lib.bug
2 messages
2 total messages
Started by Doug Moore
Tue, 26 Jan 1993 01:49
2.3: ostrstream and freeze
Author: Doug Moore
Date: Tue, 26 Jan 1993 01:49
Date: Tue, 26 Jan 1993 01:49
73 lines
2817 bytes
2817 bytes
Unfreezing a previously frozen (with str) ostrstream, when that
ostrstream has a fixed, user-supplied buffer, causes the ostrstream
destructor to inappropriately delete the user-supplied buffer.
The program that follows should print, in an endless sequence,
"Hello!" and a memory address. And with the Sun C++ implementation of
strstreams, it does. With libg++, it crashes after the first
iteration.
Sun sparc. Sunos 4.1.2. g++ 2.3.3, with patches. libg++ 2.3, with
patches. Verbose compiler output:
======================================================================
g++ jnk.cc -o a.out -g -v -Wall
Reading specs from /blossom/lib/gcc-lib/sun4/2.3.3/specs
gcc version 2.3.3
/blossom/lib/gcc-lib/sun4/2.3.3/cpp -lang-c++ -v -undef -D__GNUC__=2 -D__GNUG__=2 -D__cplusplus -Dsparc -Dsun -Dunix -D__sparc__ -D__sun__ -D__unix__ -D__sparc -D__sun -D__unix -g -Wall jnk.cc /usr/tmp/cca07996.i
GNU CPP version 2.3.3 (sparc)
/blossom/lib/gcc-lib/sun4/2.3.3/cc1plus /usr/tmp/cca07996.i -quiet -dumpbase jnk.cc -g -Wall -version -o /usr/tmp/cca07996.s
GNU C++ version 2.3.3 (sparc) compiled by GNU C version 2.3.3.
as -o /usr/tmp/cca079961.o /usr/tmp/cca07996.s
/blossom/lib/gcc-lib/sun4/2.3.3/ld -e start -dc -dp -o a.out /lib/crt0.o -L/blossom/lib/gcc-lib/sun4/2.3.3 -L/blossom/lib /usr/tmp/cca079961.o -lg++ -lgcc -lc -lg -lgcc
======================================================================
libg++ program output
======================================================================
0xf7fff6c8
Hello!
Segmentation fault
======================================================================
sun c++ program output (abbreviated)
======================================================================
Hello!
0xf7fff6dc
Hello!
0xf7fff6dc
Hello!
0xf7fff6dc
======================================================================
Program source:
======================================================================
#include <iostream.h>
#include <strstream.h>
int main(int, char*[])
{
for(;;)
{
char buf[100];
ostrstream strut(buf, sizeof(buf));
strut << "Hello!" << ends;
char *s = strut.str();
cout << (void*)s << endl;
cout << s << endl;
strut.rdbuf()->freeze(0);
}
return 0;
}
======================================================================
Doug Moore
(dougm@cs.rice.edu)
Re: 2.3: ostrstream and freeze
Author: bothner
Date: Wed, 27 Jan 1993 02:47
Date: Wed, 27 Jan 1993 02:47
66 lines
2977 bytes
2977 bytes
> Unfreezing a previously frozen (with str) ostrstream, when that
> ostrstream has a fixed, user-supplied buffer,
Well, you can't unfreeze an ostrstream with a fixed user-supplied
buffer. In this case the freeze(0) should be ignored.
Patches to do so are appended below.
--Per Bothner
Cygnus Support bothner@cygnus.com
===================================================================
RCS file: /rel/cvsfiles/devo/libg++/iostream/strstream.h,v
retrieving revision 1.10
diff -c -r1.10 strstream.h
*** 1.10 1992/11/03 06:30:01
--- strstream.h 1993/01/26 03:01:10
***************
*** 33,38 ****
--- 33,39 ----
void init_const() { xsetflags(_S_NO_WRITES); }
void init_static(char *ptr, int size, char *pstart);
protected:
+ int is_static() const { return _allocate_buffer == (_alloc_type)0; }
virtual int overflow(int = EOF);
virtual int underflow();
virtual int pbackfail(int c);
***************
*** 56,64 ****
strstreambuf(const signed char *ptr, int size)
{ init_static((char*)ptr, size, NULL); init_const(); }
#endif
int frozen() { return _flags & _S_USER_BUF ? 1 : 0; }
void freeze(int n=1)
! { if (n) _flags |= _S_USER_BUF; else _flags &= ~_S_USER_BUF; }
_G_size_t pcount();
char *str();
virtual streampos seekoff(streamoff, _seek_dir, int mode=ios::in|ios::out);
--- 57,67 ----
strstreambuf(const signed char *ptr, int size)
{ init_static((char*)ptr, size, NULL); init_const(); }
#endif
+ // Note: frozen() is always true if is_static().
int frozen() { return _flags & _S_USER_BUF ? 1 : 0; }
void freeze(int n=1)
! { if (!is_static())
! { if (n) _flags |= _S_USER_BUF; else _flags &= ~_S_USER_BUF; } }
_G_size_t pcount();
char *str();
virtual streampos seekoff(streamoff, _seek_dir, int mode=ios::in|ios::out);
===================================================================
RCS file: /rel/cvsfiles/devo/libg++/iostream/strstream.C,v
retrieving revision 1.15
diff -c -r1.15 strstream.C
*** 1.15 1992/10/17 00:26:47
--- strstream.C 1993/01/26 02:59:57
***************
*** 182,189 ****
streampos strstreambuf::seekoff(streamoff off, _seek_dir dir,
int mode /*=ios::in|ios::out*/)
{
- _allocate_buffer = default_alloc;
- _free_buffer = default_free;
size_t cur_size = pcount();
streampos new_pos = EOF;
--- 182,187 ----
Thread Navigation
This is a paginated view of messages in the thread with full content displayed inline.
Messages are displayed in chronological order, with the original post highlighted in green.
Use pagination controls to navigate through all messages in large threads.
Back to All Threads