Segmentation faults using setw() manipulator
Simon Pamment
simon528@home.com
Tue Jan 23 13:59:00 GMT 2001
I am using a snapshot taken today (23/1/2001) on an i686 linux
platform and am having a problem with setw() whereby for some
widths outputting a character string will result in a segmentation
fault.
The following sample program illustrates this for the case where
the output string is "ABC" and the format width is 27.
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
#include <iostream>
#include <iomanip>
int
main(int, char*[])
{
std::cout << "Result = [" << std::setw(27) << "ABC" << "]\n";
return 0;
}
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
Trying different combinations of strings and widths suggests that
there may be somewhat of a pattern. For a given string the above
program will correctly format it up to a certain width (call it W).
For widths greater than W the program exhibits one of two modes of
behaviour depending on whether the length of the string being
output is even or odd.
* If the length is even, the program will seg fault for all lengths
greater than W.
* If the length is odd, the program will output a corrupt string
(of the correct length) for width W+1 and will seg fault for all
widths greater than W+1.
The following table summarises the situation for successively
larger strings.
output | maximum | width for | minimum
string | width for | corrupt | width for
| success | output | seg fault
===========+============+============+============
"" | 24 | | 25
"A" | 24 | 25 | 26
"AB" | 25 | | 26
"ABC" | 25 | 26 | 27
"ABCD" | 26 | | 27
"ABCDE" | 26 | 27 | 28
"ABCDEF" | 27 | | 28
"ABCDEFG" | 27 | 28 | 29
I would be interested to know if anybody else can duplicate this
problem (at least on an Intel 686 platform).
In case it is useful the following is the output when compiling the
program with the verbose flag.
g++ -v -o sw sw.cc
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
Reading specs from /work/exp/local/lib/gcc-lib/i686-pc-linux-gnu/2.97/specs
Configured with: /work/exp/local/src/gcc.src/gcc/configure --enable-shared --with-gnu-as --with-gnu-ld --prefix=/work/exp/local
gcc version 2.97 20010123 (experimental)
/work/exp/local/lib/gcc-lib/i686-pc-linux-gnu/2.97/cc1plus -v -D__GNUC__=2 -D__GNUC_MINOR__=97 -D__GNUC_PATCHLEVEL__=0 -D__ELF__ -Dunix -Dlinux -D__ELF__ -D__unix__ -D__linux__ -D__unix -D__linux -Asystem=posix -D__STDC_HOSTED__=1 -Acpu=i386 -Amachine=i386 -Di386 -D__i386 -D__i386__ -D__tune_i686__ -D__tune_pentiumpro__ sw.cc -D__GNUG__=2 -D_GNU_SOURCE -D__GXX_ABI_VERSION=100 -quiet -dumpbase sw.cc -version -o /tmp/ccyiv5Pa.s
GNU CPP version 2.97 20010123 (experimental) (cpplib) (i386 Linux/ELF)
GNU CPP version 2.97 20010123 (experimental) (cpplib) (i386 Linux/ELF)
GNU C++ version 2.97 20010123 (experimental) (i686-pc-linux-gnu)
compiled by GNU C version 2.97 20010123 (experimental).
ignoring nonexistent directory "/usr/local/include"
#include "..." search starts here:
#include <...> search starts here:
/work/exp/local/include/g++-v3
/work/exp/local/i686-pc-linux-gnu/include/g++-v3
/work/exp/local/lib/gcc-lib/i686-pc-linux-gnu/2.97/include
/work/exp/local/i686-pc-linux-gnu/include
/usr/include
End of search list.
/work/exp/local/lib/gcc-lib/i686-pc-linux-gnu/2.97/../../../../i686-pc-linux-gnu/bin/as -V -Qy -o /tmp/ccg03AO9.o /tmp/ccyiv5Pa.s
GNU assembler version 2.10.1 (i686-pc-linux-gnu) using BFD version 2.10.1
/work/exp/local/lib/gcc-lib/i686-pc-linux-gnu/2.97/collect2 -m elf_i386 -dynamic-linker /lib/ld-linux.so.2 -o sw /usr/lib/crt1.o /usr/lib/crti.o /work/exp/local/lib/gcc-lib/i686-pc-linux-gnu/2.97/crtbegin.o -L/work/exp/local/lib/gcc-lib/i686-pc-linux-gnu/2.97 -L/work/exp/local/lib/gcc-lib/i686-pc-linux-gnu/2.97/../../../../i686-pc-linux-gnu/lib -L/work/exp/local/lib/gcc-lib/i686-pc-linux-gnu/2.97/../../.. /tmp/ccg03AO9.o -lstdc++ -lm -lgcc -lc -lgcc /work/exp/local/lib/gcc-lib/i686-pc-linux-gnu/2.97/crtend.o /usr/lib/crtn.o
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
More information about the Gcc-bugs
mailing list