acadia-newlib/newlib/libc
Keith Packard cf86601d98 svfwscanf: Simplify _sungetwc_r to eliminate apparent buffer overflow
svfwscanf replaces getwc and ungetwc_r. The comments in the code talk
about avoiding file operations, but they also need to bypass the
mbtowc calls as svfwscanf operates on wchar_t, not multibyte data,
which is a more important reason here; they would not work correctly
otherwise.

The ungetwc replacement has code which uses the 3 byte FILE _ubuf
field, but if wchar_t is 32-bits, this field is not large enough to
hold even one wchar_t value. Building in this mode generates warnings
about array overflow:

	In file included from ../../newlib/libc/stdio/svfiwscanf.c:35:
	../../newlib/libc/stdio/vfwscanf.c: In function '_sungetwc_r.isra':
	../../newlib/libc/stdio/vfwscanf.c:316:12: warning: array subscript 4294967295 is above array bounds of 'unsigned char[3]' [-Warray-bounds]
	  316 |   fp->_p = &fp->_ubuf[sizeof (fp->_ubuf) - sizeof (wchar_t)];
	      |            ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	In file included from ../../newlib/libc/stdio/stdio.h:46,
			 from ../../newlib/libc/stdio/vfwscanf.c:82,
			 from ../../newlib/libc/stdio/svfiwscanf.c:35:
	../../newlib/libc/include/sys/reent.h:216:17: note: while referencing '_ubuf'
	  216 |   unsigned char _ubuf[3]; /* guarantee an ungetc() buffer */
	      |                 ^~~~~

However, the vfwscanf code *never* ungets data before the start of the
scanning operation, and *always* ungets data which matches the input
at that point, so the code always hits the block which backs up over
the input data and never hits the block which uses the _ubuf field.

In addition, the svfwscanf code will always start with the unget
buffer empty, so the ungetwc replacement never needs to support an
unget buffer at all.

Simplify the code by removing support for everything other than
backing up over the input data, leaving the check to make sure it
doesn't get underflowed in case the vfscanf code has a bug in it.

Signed-off-by: Keith Packard <keithp@keithp.com>
2021-08-18 10:37:24 +02:00
..
argz Add build mechanism to share common header files between machines 2021-04-13 12:55:33 +02:00
ctype fix and amend scripts and makefile rules to generate Unicode data 2021-07-06 15:35:37 +02:00
errno Add build mechanism to share common header files between machines 2021-04-13 12:55:33 +02:00
iconv Add build mechanism to share common header files between machines 2021-04-13 12:55:33 +02:00
include libc: Added prototypes for new POSIX APIs 2021-08-09 10:29:55 +02:00
locale Add build mechanism to share common header files between machines 2021-04-13 12:55:33 +02:00
machine aarch64: support binary mode for opening files 2021-05-26 15:17:11 +01:00
misc Add build mechanism to share common header files between machines 2021-04-13 12:55:33 +02:00
posix Add build mechanism to share common header files between machines 2021-04-13 12:55:33 +02:00
reent Add build mechanism to share common header files between machines 2021-04-13 12:55:33 +02:00
search Add build mechanism to share common header files between machines 2021-04-13 12:55:33 +02:00
signal libc: Fix compilation for new sig2str/str2sig implementation 2021-08-02 16:42:03 +02:00
ssp Add build mechanism to share common header files between machines 2021-04-13 12:55:33 +02:00
stdio svfwscanf: Simplify _sungetwc_r to eliminate apparent buffer overflow 2021-08-18 10:37:24 +02:00
stdio64 Add build mechanism to share common header files between machines 2021-04-13 12:55:33 +02:00
stdlib stdlib: Make strtod/strtof set ERANGE consistently for underflow. 2021-07-07 13:22:02 -04:00
string fix and amend scripts and makefile rules to generate Unicode data 2021-07-06 15:35:37 +02:00
sys libc/sys/rtems/crt0.c: Fix two warnings. 2021-06-17 12:58:36 -05:00
syscalls Add build mechanism to share common header files between machines 2021-04-13 12:55:33 +02:00
time Add build mechanism to share common header files between machines 2021-04-13 12:55:33 +02:00
unix Add build mechanism to share common header files between machines 2021-04-13 12:55:33 +02:00
xdr Add build mechanism to share common header files between machines 2021-04-13 12:55:33 +02:00
Makefile.am ssp: add build infrastructure 2017-11-29 11:25:46 -06:00
Makefile.in Add build mechanism to share common header files between machines 2021-04-13 12:55:33 +02:00
Makefile.unidata fix and amend scripts and makefile rules to generate Unicode data 2021-07-06 15:35:37 +02:00
aclocal.m4 Fix up errors in regenerated files for 2.3.0 release. 2016-01-04 12:57:31 -05:00
configure Add build mechanism to share common header files between machines 2021-04-13 12:55:33 +02:00
configure.in Add build mechanism to share common header files between machines 2021-04-13 12:55:33 +02:00
libc.in.xml ssp: add documentation 2017-11-29 11:25:45 -06:00
libc.texinfo ssp: add documentation 2017-11-29 11:25:45 -06:00
saber Remove matherr, and SVID and X/Open math library configurations 2019-01-23 10:46:24 +01:00
sys.tex Don't include stdio64 functions in reentrant syscalls menu when not bulding stdio64. 2015-08-03 09:54:33 +01:00