NetBSD-Bugs archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

lib/58206: sync_with_stdio breaks reads from cin



>Number:         58206
>Category:       lib
>Synopsis:       sync_with_stdio breaks reads from cin
>Confidential:   no
>Severity:       serious
>Priority:       medium
>Responsible:    lib-bug-people
>State:          open
>Class:          sw-bug
>Submitter-Id:   net
>Arrival-Date:   Sat Apr 27 23:45:00 +0000 2024
>Originator:     Taylor R Campbell
>Release:        current, 10, 9
>Organization:
The std::NetBSD++ Foundation
>Environment:
>Description:
In C++, std::ios::sync_with_stdio(false) has the side effect that reading from std::cin sets the eof|fail bits and does nothing to attempt a read from file descriptor 0.
>How-To-Repeat:
$ cat cin.cc
#include <iostream>

using std::cin;
using std::cout;

int
main(void)
{
	char buf[1024];

	std::ios::sync_with_stdio(false);
	cin.read(buf, sizeof buf);
	cout << "read " << cin.gcount() << " bytes, "
	     << "state " << cin.rdstate()
	     << " (eof|fail = " << (cin.eofbit|cin.failbit) << ")"
	     << std::endl;
	return 0;
}
$ make cin CXXFLAGS=-std=c++11
c++ -std=c++11   -o cin cin.cc 
$ echo foo | ./cin
read 0 bytes, state 6 (eof|fail = 6)

If I remove sync_with_stdio:

$ echo foo | ./cin
read 4 bytes, state 6 (eof|fail = 6)
>Fix:
Yes, please!



Home | Main Index | Thread Index | Old Index