libstdc++/9425: filebuf::pbackfail broken
Thu Jan 23 19:44:00 GMT 2003

>Number:         9425
>Category:       libstdc++
>Synopsis:       filebuf::pbackfail broken
>Confidential:   no
>Severity:       serious
>Priority:       medium
>Responsible:    unassigned
>State:          open
>Class:          sw-bug
>Submitter-Id:   net
>Arrival-Date:   Thu Jan 23 19:06:01 UTC 2003
>Release:        gcc-3.2.1
Red Hat Linux 8.0
basic_filebuf<>::pbackfail calls basic_filebuf<>::seekoff, but fails to check the return value. If seekoff fails for some reason (including, but not limited to codecvt<>::encoding() <= 0), strange things happen.
See attachment.

Content-Type: text/plain; name=""
Content-Disposition: inline; filename=""

#include <fstream>
#include <locale>
#include <cstdio>
#include <cwchar>

#undef NDEBUG
#include <cassert>

using namespace std;

class Variable : public codecvt<char, char, mbstate_t>
	int do_encoding() const throw() { return 0; }

int main()
	char buf[BUFSIZ];
	filebuf fb01;"filebuf_virtuals-1.txt", ios_base::in);
	streamsize sz1 = fb01.sgetn(buf, BUFSIZ);
	assert(sz1 == BUFSIZ);

	int n1 = fb01.sputbackc('a');
	if (n1 == EOF)
		return 0;

	streamsize sz2 = fb01.sgetn(buf, BUFSIZ);
	assert(buf[0] == 'a');

	filebuf fb02;
	locale loc;
	loc = locale(loc, new Variable);
	fb02.pubimbue(loc);"filebuf_virtuals-1.txt", ios_base::in);
	streamsize sz3 = fb02.sgetn(buf, BUFSIZ);
	assert(sz3 == BUFSIZ);

	int n2 = fb02.sputbackc('a');
	if (n2 == EOF)
		return 0;

	streamsize sz4 = fb02.sgetn(buf, BUFSIZ);
	assert(buf[0] == 'a');
	assert(sz4 == sz2);

	return 0;

More information about the Gcc-bugs mailing list