newlib-cygwin/newlib/libc/stdio
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
..
Makefile.am stdio: Fix make rule override 2017-07-28 15:05:54 +02:00
Makefile.in Add build mechanism to share common header files between machines 2021-04-13 12:55:33 +02:00
asiprintf.c Use remove-advertising-clause script to edit BSD licenses 2020-01-29 19:03:31 +01:00
asniprintf.c ansification: remove _DEFUN 2018-01-17 11:47:26 -06:00
asnprintf.c ansification: remove _EXFUN, _EXFUN_NOTHROW 2018-01-17 11:47:29 -06:00
asprintf.c Use remove-advertising-clause script to edit BSD licenses 2020-01-29 19:03:31 +01:00
clearerr.c Use remove-advertising-clause script to edit BSD licenses 2020-01-29 19:03:31 +01:00
clearerr_u.c ansification: remove _DEFUN 2018-01-17 11:47:26 -06:00
diprintf.c ansification: remove _DEFUN 2018-01-17 11:47:26 -06:00
dprintf.c ansification: remove _EXFUN, _EXFUN_NOTHROW 2018-01-17 11:47:29 -06:00
fclose.c Use remove-advertising-clause script to edit BSD licenses 2020-01-29 19:03:31 +01:00
fcloseall.c Use remove-advertising-clause script to edit BSD licenses 2020-01-29 19:03:31 +01:00
fdopen.c Use remove-advertising-clause script to edit BSD licenses 2020-01-29 19:03:31 +01:00
feof.c Use remove-advertising-clause script to edit BSD licenses 2020-01-29 19:03:31 +01:00
feof_u.c ansification: remove _DEFUN 2018-01-17 11:47:26 -06:00
ferror.c Use remove-advertising-clause script to edit BSD licenses 2020-01-29 19:03:31 +01:00
ferror_u.c ansification: remove _DEFUN 2018-01-17 11:47:26 -06:00
fflush.c Use remove-advertising-clause script to edit BSD licenses 2020-01-29 19:03:31 +01:00
fflush_u.c Implement BSD/GNU unlocked stdio extensions. 2014-12-18 14:55:21 +00:00
fgetc.c Use remove-advertising-clause script to edit BSD licenses 2020-01-29 19:03:31 +01:00
fgetc_u.c ansification: remove _DEFUN 2018-01-17 11:47:26 -06:00
fgetpos.c Use remove-advertising-clause script to edit BSD licenses 2020-01-29 19:03:31 +01:00
fgets.c Use remove-advertising-clause script to edit BSD licenses 2020-01-29 19:03:31 +01:00
fgets_u.c Implement BSD/GNU unlocked stdio extensions. 2014-12-18 14:55:21 +00:00
fgetwc.c ansification: remove _DEFUN 2018-01-17 11:47:26 -06:00
fgetwc_u.c ansification: remove _DEFUN 2018-01-17 11:47:26 -06:00
fgetws.c ansification: remove _DEFUN 2018-01-17 11:47:26 -06:00
fgetws_u.c Implement BSD/GNU unlocked stdio extensions. 2014-12-18 14:55:21 +00:00
fileno.c Use remove-advertising-clause script to edit BSD licenses 2020-01-29 19:03:31 +01:00
fileno_u.c ansification: remove _DEFUN 2018-01-17 11:47:26 -06:00
findfp.c Use remove-advertising-clause script to edit BSD licenses 2020-01-29 19:03:31 +01:00
fiprintf.c Use remove-advertising-clause script to edit BSD licenses 2020-01-29 19:03:31 +01:00
fiscanf.c Use remove-advertising-clause script to edit BSD licenses 2020-01-29 19:03:31 +01:00
flags.c Typo in license for newlib/libc/stdio/flags.c 2020-02-06 11:58:50 +01:00
floatio.h Use remove-advertising-clause script to edit BSD licenses 2020-01-29 19:03:31 +01:00
fmemopen.c ansification: remove _DEFUN 2018-01-17 11:47:26 -06:00
fopen.c Use remove-advertising-clause script to edit BSD licenses 2020-01-29 19:03:31 +01:00
fopencookie.c ansification: remove _DEFUN 2018-01-17 11:47:26 -06:00
fprintf.c Use remove-advertising-clause script to edit BSD licenses 2020-01-29 19:03:31 +01:00
fpurge.c ansification: remove _DEFUN 2018-01-17 11:47:26 -06:00
fputc.c Use remove-advertising-clause script to edit BSD licenses 2020-01-29 19:03:31 +01:00
fputc_u.c ansification: remove _DEFUN 2018-01-17 11:47:26 -06:00
fputs.c Use remove-advertising-clause script to edit BSD licenses 2020-01-29 19:03:31 +01:00
fputs_u.c Implement BSD/GNU unlocked stdio extensions. 2014-12-18 14:55:21 +00:00
fputwc.c ansification: remove _DEFUN 2018-01-17 11:47:26 -06:00
fputwc_u.c ansification: remove _DEFUN 2018-01-17 11:47:26 -06:00
fputws.c ansification: remove _DEFUN 2018-01-17 11:47:26 -06:00
fputws_u.c Implement BSD/GNU unlocked stdio extensions. 2014-12-18 14:55:21 +00:00
fread.c Use remove-advertising-clause script to edit BSD licenses 2020-01-29 19:03:31 +01:00
fread_u.c Implement BSD/GNU unlocked stdio extensions. 2014-12-18 14:55:21 +00:00
freopen.c Use remove-advertising-clause script to edit BSD licenses 2020-01-29 19:03:31 +01:00
fscanf.c Use remove-advertising-clause script to edit BSD licenses 2020-01-29 19:03:31 +01:00
fseek.c Use remove-advertising-clause script to edit BSD licenses 2020-01-29 19:03:31 +01:00
fseeko.c Use remove-advertising-clause script to edit BSD licenses 2020-01-29 19:03:31 +01:00
fsetlocking.c ansification: remove _DEFUN 2018-01-17 11:47:26 -06:00
fsetpos.c Use remove-advertising-clause script to edit BSD licenses 2020-01-29 19:03:31 +01:00
ftell.c Use remove-advertising-clause script to edit BSD licenses 2020-01-29 19:03:31 +01:00
ftello.c Use remove-advertising-clause script to edit BSD licenses 2020-01-29 19:03:31 +01:00
funopen.c ansification: remove _DEFUN 2018-01-17 11:47:26 -06:00
fvwrite.c Use remove-advertising-clause script to edit BSD licenses 2020-01-29 19:03:31 +01:00
fvwrite.h Use remove-advertising-clause script to edit BSD licenses 2020-01-29 19:03:31 +01:00
fwalk.c Use remove-advertising-clause script to edit BSD licenses 2020-01-29 19:03:31 +01:00
fwide.c ansification: remove _DEFUN 2018-01-17 11:47:26 -06:00
fwprintf.c Use remove-advertising-clause script to edit BSD licenses 2020-01-29 19:03:31 +01:00
fwrite.c Use remove-advertising-clause script to edit BSD licenses 2020-01-29 19:03:31 +01:00
fwrite_u.c Implement BSD/GNU unlocked stdio extensions. 2014-12-18 14:55:21 +00:00
fwscanf.c Use remove-advertising-clause script to edit BSD licenses 2020-01-29 19:03:31 +01:00
getc.c Use remove-advertising-clause script to edit BSD licenses 2020-01-29 19:03:31 +01:00
getc_u.c Use remove-advertising-clause script to edit BSD licenses 2020-01-29 19:03:31 +01:00
getchar.c Use remove-advertising-clause script to edit BSD licenses 2020-01-29 19:03:31 +01:00
getchar_u.c Use remove-advertising-clause script to edit BSD licenses 2020-01-29 19:03:31 +01:00
getdelim.c ansification: remove _DEFUN 2018-01-17 11:47:26 -06:00
getline.c ansification: remove _EXFUN, _EXFUN_NOTHROW 2018-01-17 11:47:29 -06:00
gets.c Use remove-advertising-clause script to edit BSD licenses 2020-01-29 19:03:31 +01:00
getw.c Use remove-advertising-clause script to edit BSD licenses 2020-01-29 19:03:31 +01:00
getwc.c ansification: remove _DEFUN 2018-01-17 11:47:26 -06:00
getwc_u.c ansification: remove _DEFUN 2018-01-17 11:47:26 -06:00
getwchar.c ansification: remove _DEFUN 2018-01-17 11:47:26 -06:00
getwchar_u.c ansification: remove _DEFUN 2018-01-17 11:47:26 -06:00
iprintf.c Use remove-advertising-clause script to edit BSD licenses 2020-01-29 19:03:31 +01:00
iscanf.c Use remove-advertising-clause script to edit BSD licenses 2020-01-29 19:03:31 +01:00
local.h Use remove-advertising-clause script to edit BSD licenses 2020-01-29 19:03:31 +01:00
makebuf.c Use remove-advertising-clause script to edit BSD licenses 2020-01-29 19:03:31 +01:00
mktemp.c Use remove-advertising-clause script to edit BSD licenses 2020-01-29 19:03:31 +01:00
nano-vfprintf.c Fix vprintf and vfscanf for GCC PR 14577 2018-02-01 13:28:28 +01:00
nano-vfprintf_float.c nano-vfprintf_float.c: Fix check if negative for nans. 2018-12-13 13:15:32 +01:00
nano-vfprintf_i.c ansification: remove _CONST 2018-01-17 11:47:08 -06:00
nano-vfprintf_local.h ansification: remove _PTR 2018-01-17 11:47:16 -06:00
nano-vfscanf.c Use remove-advertising-clause script to edit BSD licenses 2020-01-29 19:03:31 +01:00
nano-vfscanf_float.c Use remove-advertising-clause script to edit BSD licenses 2020-01-29 19:03:31 +01:00
nano-vfscanf_i.c ansification: remove _PARAMS 2018-01-17 11:47:13 -06:00
nano-vfscanf_local.h Use remove-advertising-clause script to edit BSD licenses 2020-01-29 19:03:31 +01:00
open_memstream.c Fix warnings when building for msp430-elf 2020-09-03 12:55:32 +02:00
perror.c Use remove-advertising-clause script to edit BSD licenses 2020-01-29 19:03:31 +01:00
printf.c Use remove-advertising-clause script to edit BSD licenses 2020-01-29 19:03:31 +01:00
putc.c Use remove-advertising-clause script to edit BSD licenses 2020-01-29 19:03:31 +01:00
putc_u.c Use remove-advertising-clause script to edit BSD licenses 2020-01-29 19:03:31 +01:00
putchar.c Use remove-advertising-clause script to edit BSD licenses 2020-01-29 19:03:31 +01:00
putchar_u.c Use remove-advertising-clause script to edit BSD licenses 2020-01-29 19:03:31 +01:00
puts.c Use remove-advertising-clause script to edit BSD licenses 2020-01-29 19:03:31 +01:00
putw.c Use remove-advertising-clause script to edit BSD licenses 2020-01-29 19:03:31 +01:00
putwc.c ansification: remove _DEFUN 2018-01-17 11:47:26 -06:00
putwc_u.c ansification: remove _DEFUN 2018-01-17 11:47:26 -06:00
putwchar.c ansification: remove _DEFUN 2018-01-17 11:47:26 -06:00
putwchar_u.c ansification: remove _DEFUN 2018-01-17 11:47:26 -06:00
refill.c Use remove-advertising-clause script to edit BSD licenses 2020-01-29 19:03:31 +01:00
remove.c Use remove-advertising-clause script to edit BSD licenses 2020-01-29 19:03:31 +01:00
rename.c Use remove-advertising-clause script to edit BSD licenses 2020-01-29 19:03:31 +01:00
rewind.c Use remove-advertising-clause script to edit BSD licenses 2020-01-29 19:03:31 +01:00
rget.c Use remove-advertising-clause script to edit BSD licenses 2020-01-29 19:03:31 +01:00
scanf.c Use remove-advertising-clause script to edit BSD licenses 2020-01-29 19:03:31 +01:00
sccl.c Use remove-advertising-clause script to edit BSD licenses 2020-01-29 19:03:31 +01:00
setbuf.c Use remove-advertising-clause script to edit BSD licenses 2020-01-29 19:03:31 +01:00
setbuffer.c Use remove-advertising-clause script to edit BSD licenses 2020-01-29 19:03:31 +01:00
setlinebuf.c Use remove-advertising-clause script to edit BSD licenses 2020-01-29 19:03:31 +01:00
setvbuf.c Use remove-advertising-clause script to edit BSD licenses 2020-01-29 19:03:31 +01:00
siprintf.c Use remove-advertising-clause script to edit BSD licenses 2020-01-29 19:03:31 +01:00
siscanf.c Use remove-advertising-clause script to edit BSD licenses 2020-01-29 19:03:31 +01:00
sniprintf.c Use remove-advertising-clause script to edit BSD licenses 2020-01-29 19:03:31 +01:00
snprintf.c Use remove-advertising-clause script to edit BSD licenses 2020-01-29 19:03:31 +01:00
sprintf.c Use remove-advertising-clause script to edit BSD licenses 2020-01-29 19:03:31 +01:00
sscanf.c Use remove-advertising-clause script to edit BSD licenses 2020-01-29 19:03:31 +01:00
stdio.c Use remove-advertising-clause script to edit BSD licenses 2020-01-29 19:03:31 +01:00
stdio.tex Fix makedoc markup used in texinfo source 2015-06-23 16:14:39 +01:00
stdio_ext.c ansification: remove _DEFUN 2018-01-17 11:47:26 -06:00
swprintf.c Use remove-advertising-clause script to edit BSD licenses 2020-01-29 19:03:31 +01:00
swscanf.c Use remove-advertising-clause script to edit BSD licenses 2020-01-29 19:03:31 +01:00
tmpfile.c ansification: remove _DEFUN 2018-01-17 11:47:26 -06:00
tmpnam.c ansification: remove _DEFUN 2018-01-17 11:47:26 -06:00
ungetc.c Use remove-advertising-clause script to edit BSD licenses 2020-01-29 19:03:31 +01:00
ungetwc.c ansification: remove _DEFUN 2018-01-17 11:47:26 -06:00
vasiprintf.c Use remove-advertising-clause script to edit BSD licenses 2020-01-29 19:03:31 +01:00
vasniprintf.c ansification: remove _DEFUN 2018-01-17 11:47:26 -06:00
vasnprintf.c ansification: remove _EXFUN, _EXFUN_NOTHROW 2018-01-17 11:47:29 -06:00
vasprintf.c Use remove-advertising-clause script to edit BSD licenses 2020-01-29 19:03:31 +01:00
vdiprintf.c ansification: remove _DEFUN 2018-01-17 11:47:26 -06:00
vdprintf.c ansification: remove _EXFUN, _EXFUN_NOTHROW 2018-01-17 11:47:29 -06:00
vfieeefp.h Add __packed to struct ldieee 2017-04-18 12:25:35 +02:00
vfprintf.c Use remove-advertising-clause script to edit BSD licenses 2020-01-29 19:03:31 +01:00
vfscanf.c stdio: Parse 0x0p+00 correctly in scanf 2021-06-18 17:15:37 -04:00
vfwprintf.c ansification: remove _EXFUN, _EXFUN_NOTHROW 2018-01-17 11:47:29 -06:00
vfwscanf.c svfwscanf: Simplify _sungetwc_r to eliminate apparent buffer overflow 2021-08-18 10:37:24 +02:00
viprintf.c Use remove-advertising-clause script to edit BSD licenses 2020-01-29 19:03:31 +01:00
viscanf.c Use remove-advertising-clause script to edit BSD licenses 2020-01-29 19:03:31 +01:00
vprintf.c Use remove-advertising-clause script to edit BSD licenses 2020-01-29 19:03:31 +01:00
vscanf.c Use remove-advertising-clause script to edit BSD licenses 2020-01-29 19:03:31 +01:00
vsiprintf.c Use remove-advertising-clause script to edit BSD licenses 2020-01-29 19:03:31 +01:00
vsiscanf.c Use remove-advertising-clause script to edit BSD licenses 2020-01-29 19:03:31 +01:00
vsniprintf.c Use remove-advertising-clause script to edit BSD licenses 2020-01-29 19:03:31 +01:00
vsnprintf.c Use remove-advertising-clause script to edit BSD licenses 2020-01-29 19:03:31 +01:00
vsprintf.c Use remove-advertising-clause script to edit BSD licenses 2020-01-29 19:03:31 +01:00
vsscanf.c Use remove-advertising-clause script to edit BSD licenses 2020-01-29 19:03:31 +01:00
vswprintf.c Use remove-advertising-clause script to edit BSD licenses 2020-01-29 19:03:31 +01:00
vswscanf.c Use remove-advertising-clause script to edit BSD licenses 2020-01-29 19:03:31 +01:00
vwprintf.c Use remove-advertising-clause script to edit BSD licenses 2020-01-29 19:03:31 +01:00
vwscanf.c Use remove-advertising-clause script to edit BSD licenses 2020-01-29 19:03:31 +01:00
wbuf.c Use remove-advertising-clause script to edit BSD licenses 2020-01-29 19:03:31 +01:00
wprintf.c Use remove-advertising-clause script to edit BSD licenses 2020-01-29 19:03:31 +01:00
wscanf.c Use remove-advertising-clause script to edit BSD licenses 2020-01-29 19:03:31 +01:00
wsetup.c Use remove-advertising-clause script to edit BSD licenses 2020-01-29 19:03:31 +01:00