gcc/libstdc++-v3
Jonathan Wakely 5ae74944af libstdc++: Check for EOF if extraction avoids buffer overflow [PR106248]
In r11-2581-g17abcc77341584 (for LWG 2499) I added overflow checks to
the pre-C++20 operator>>(istream&, char*) overload.  Those checks can
cause extraction to stop after filling the buffer, where previously it
would have tried to extract another character and stopped at EOF. When
that happens we no longer set eofbit in the stream state, which is
consistent with the behaviour of the new C++20 overload, but is an
observable and unexpected change in the C++17 behaviour. What makes it
worse is that the behaviour change is dependent on optimization, because
__builtin_object_size is used to detect the buffer size and that only
works when optimizing.

To avoid the unexpected and optimization-dependent change in behaviour,
set eofbit manually if we stopped extracting because of the buffer size
check, but had reached EOF anyway. If the stream's rdstate() != goodbit
or width() is non-zero and smaller than the buffer, there's nothing to
do. Otherwise, we filled the buffer and need to check for EOF, and maybe
set eofbit.

The new check is guarded by #ifdef __OPTIMIZE__ because otherwise
__builtin_object_size is useless. There's no point compiling and
emitting dead code that can't be eliminated because we're not
optimizing.

We could add extra checks that the next character in the buffer is not
whitespace, to detect the case where we stopped early and prevented a
buffer overflow that would have happened otherwise. That would allow us
to assert or set badbit in the stream state when undefined behaviour was
prevented. However, those extra checks would increase the size of the
function, potentially reducing the likelihood of it being inlined, and
so making the buffer size detection less reliable. It seems preferable
to prevent UB and silently truncate, rather than miss the UB and allow
the overflow to happen.

libstdc++-v3/ChangeLog:

	PR libstdc++/106248
	* include/std/istream [C++17] (operator>>(istream&, char*)):
	Set eofbit if we stopped extracting at EOF.
	* testsuite/27_io/basic_istream/extractors_character/char/pr106248.cc:
	New test.
	* testsuite/27_io/basic_istream/extractors_character/wchar_t/pr106248.cc:
	New test.
2022-07-12 23:35:28 +01:00
..
config libstdc++: Remove whitespace before preprocessor directives 2022-05-12 18:04:01 +01:00
doc libstdc++: Fix hyperlink in docs 2022-05-16 14:54:07 +01:00
include libstdc++: Check for EOF if extraction avoids buffer overflow [PR106248] 2022-07-12 23:35:28 +01:00
libsupc++ libstdc++: eh_globals: gthreads: reset _S_init before deleting key 2022-06-21 23:11:02 -03:00
po libstdc++: Define <stacktrace> header for C++23 2022-01-17 12:13:02 +00:00
python libstdc++: Fix atomic and error_code printers for versioned namespace 2022-05-26 22:29:04 +01:00
scripts libstdc++: use grep -E instead of egrep in scripts 2022-06-24 20:31:31 +08:00
src libstdc++: Improve exceptions thrown from fs::temp_directory_path 2022-06-30 14:16:27 +01:00
testsuite libstdc++: Check for EOF if extraction avoids buffer overflow [PR106248] 2022-07-12 23:35:28 +01:00
ChangeLog Daily bump. 2022-07-10 00:16:23 +00:00
ChangeLog-1998
ChangeLog-1999
ChangeLog-2000
ChangeLog-2001
ChangeLog-2002
ChangeLog-2003
ChangeLog-2004
ChangeLog-2005
ChangeLog-2006
ChangeLog-2007
ChangeLog-2008
ChangeLog-2009
ChangeLog-2010
ChangeLog-2011
ChangeLog-2012
ChangeLog-2013
ChangeLog-2014
ChangeLog-2015
ChangeLog-2016
ChangeLog-2017
ChangeLog-2018
ChangeLog-2019
ChangeLog-2020
ChangeLog-2021 Rotate ChangeLog files - part 1 - add ChangeLog-2021. 2022-01-03 10:18:16 +01:00
Makefile.am Update copyright years. 2022-01-03 10:42:10 +01:00
Makefile.in libstdc++: Define <stacktrace> header for C++23 2022-01-17 12:13:02 +00:00
README
acinclude.m4 libstdc++: check for openat 2022-06-23 23:28:14 -03:00
aclocal.m4
config.h.in libstdc++: check for openat 2022-06-23 23:28:14 -03:00
configure Enable some features for RTEMS in libstdc++ 2022-07-01 12:03:13 +02:00
configure.ac Enable some features for RTEMS in libstdc++ 2022-07-01 12:03:13 +02:00
configure.host
crossconfig.m4 Enable some features for RTEMS in libstdc++ 2022-07-01 12:03:13 +02:00
fragment.am
linkage.m4

README

file: libstdc++-v3/README

New users may wish to point their web browsers to the file
index.html in the 'doc/html' subdirectory.  It contains brief
building instructions and notes on how to configure the library in
interesting ways.