The following code demonstrates the bug.
#include <iostream>
using namespace std;
int main() {
    ios::sync_with_stdio(false);
    streambuf *s=cin.rdbuf();

    int c=s->sgetc();
    s->pubseekoff(0,ios::cur,ios::in);
    cout << s;
}

When the resulting executable is run with standard input coming
(for example) a pipe, the first 8191 bytes of input are not
replicated to standard out.

Expected behaviour is for pubseekoff to fail, but not to throw
away buffered data in the process.

If sync_with_stdio(false) is not called, the program behaves
as expected.

If sgetc() is not called on the streambuf, the program behaves
as expected.

If standard input is redirected from a file, then pubseekoff
does not fail, and the output is as expected.

Compiler output and a demonstration:

$ g++ -v --save-temps -o bug bug.cc
Using built-in specs.
Target: x86_64-pc-linux-gnu
Configured with: /var/tmp/portage/gcc-4.0.2-r3/work/gcc-4.0.2/configure
--prefix=/usr --bindir=/usr/x86_64-pc-linux-gnu/gcc-bin/4.0.2
--includedir=/usr/lib/gcc/x86_64-pc-linux-gnu/4.0.2/include
--datadir=/usr/share/gcc-data/x86_64-pc-linux-gnu/4.0.2
--mandir=/usr/share/gcc-data/x86_64-pc-linux-gnu/4.0.2/man
--infodir=/usr/share/gcc-data/x86_64-pc-linux-gnu/4.0.2/info
--with-gxx-include-dir=/usr/lib/gcc/x86_64-pc-linux-gnu/4.0.2/include/g++-v4
--host=x86_64-pc-linux-gnu --build=x86_64-pc-linux-gnu --disable-altivec
--enable-nls --without-included-gettext --with-system-zlib --disable-checking
--disable-werror --disable-libunwind-exceptions --enable-multilib
--disable-libmudflap --disable-libgcj --enable-languages=c,c++,f95
--enable-shared --enable-threads=posix --enable-__cxa_atexit
--enable-clocale=gnu
Thread model: posix
gcc version 4.0.2 (Gentoo 4.0.2-r3, pie-8.7.8)
 /usr/libexec/gcc/x86_64-pc-linux-gnu/4.0.2/cc1plus -E -quiet -v -D_GNU_SOURCE
bug.cc -mtune=k8 -fpch-preprocess -o bug.ii
ignoring nonexistent directory "/usr/local/include"
ignoring nonexistent directory
"/usr/lib/gcc/x86_64-pc-linux-gnu/4.0.2/../../../../x86_64-pc-linux-gnu/include"
#include "..." search starts here:
#include <...> search starts here:
 /usr/lib/gcc/x86_64-pc-linux-gnu/4.0.2/include/g++-v4
 /usr/lib/gcc/x86_64-pc-linux-gnu/4.0.2/include/g++-v4/x86_64-pc-linux-gnu
 /usr/lib/gcc/x86_64-pc-linux-gnu/4.0.2/include/g++-v4/backward
 /usr/lib/gcc/x86_64-pc-linux-gnu/4.0.2/include
 /usr/include
End of search list.
 /usr/libexec/gcc/x86_64-pc-linux-gnu/4.0.2/cc1plus -fpreprocessed bug.ii
-quiet -dumpbase bug.cc -mtune=k8 -auxbase bug -version -o bug.s
GNU C++ version 4.0.2 (Gentoo 4.0.2-r3, pie-8.7.8) (x86_64-pc-linux-gnu)
        compiled by GNU C version 4.0.2 (Gentoo 4.0.2-r3, pie-8.7.8).
GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
 /usr/lib/gcc/x86_64-pc-linux-gnu/4.0.2/../../../../x86_64-pc-linux-gnu/bin/as
-V -Qy -o bug.o bug.s
GNU assembler version 2.16.1 (x86_64-pc-linux-gnu) using BFD version 2.16.1
 /usr/libexec/gcc/x86_64-pc-linux-gnu/4.0.2/collect2 --eh-frame-hdr -m
elf_x86_64 -dynamic-linker /lib64/ld-linux-x86-64.so.2 -o bug
/usr/lib/gcc/x86_64-pc-linux-gnu/4.0.2/../../../../lib64/crt1.o
/usr/lib/gcc/x86_64-pc-linux-gnu/4.0.2/../../../../lib64/crti.o
/usr/lib/gcc/x86_64-pc-linux-gnu/4.0.2/crtbegin.o
-L/usr/lib/gcc/x86_64-pc-linux-gnu/4.0.2
-L/usr/lib/gcc/x86_64-pc-linux-gnu/4.0.2
-L/usr/lib/gcc/x86_64-pc-linux-gnu/4.0.2/../../../../x86_64-pc-linux-gnu/lib
-L/usr/lib/gcc/x86_64-pc-linux-gnu/4.0.2/../../../../lib64
-L/usr/lib/gcc/x86_64-pc-linux-gnu/4.0.2/../../.. -L/lib/../lib64
-L/usr/lib/../lib64 bug.o -lstdc++ -lm -lgcc_s -lgcc -lc -lgcc_s -lgcc
/usr/lib/gcc/x86_64-pc-linux-gnu/4.0.2/crtend.o
/usr/lib/gcc/x86_64-pc-linux-gnu/4.0.2/../../../../lib64/crtn.o

$ dd if=/dev/urandom bs=16k count=1 of=test-in
1+0 records in
1+0 records out
$ dd if=test-in bs=1 skip=8191 of=test-trunc
8193+0 records in
8193+0 records out
$ cat test-in | ./bug > test-out
$ cmp test-out test-trunc
$ wc -c test-in test-out test-trunc
16384 test-in
 8193 test-out
 8193 test-trunc
32770 total
$


-- 
           Summary: sync_with_stdio(false) triggers bug with sgetc and
                    pubseekoff
           Product: gcc
           Version: 4.0.2
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: libstdc++
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: sam at quux dot dropbear dot id dot au
 GCC build triplet: x86_64-pc-linux-gnu
  GCC host triplet: x86_64-pc-linux-gnu
GCC target triplet: x86_64-pc-linux-gnu


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=26777

Reply via email to