mirror of
git://git.musl-libc.org/musl
synced 2025-01-25 08:03:04 +00:00
fix undefined behavior in scanf core
as reported/analyzed by Pascal Cuoq, the shlim and shcnt macros/functions are called by the scanf core (vfscanf) with f->rpos potentially null (if the FILE is not yet activated for reading at the time of the call). in this case, they compute differences between a null pointer (f->rpos) and a non-null one (f->buf), resulting in undefined behavior. it's unlikely that any observably wrong behavior occurred in practice, at least without LTO, due to limits on what's visible to the compiler from translation unit boundaries, but this has not been checked. fix is simply ensuring that the FILE is activated for read mode before entering the main scanf loop, and erroring out early if it can't be.
This commit is contained in:
parent
19f870c3a6
commit
b287cd745c
@ -76,6 +76,9 @@ int vfscanf(FILE *restrict f, const char *restrict fmt, va_list ap)
|
||||
|
||||
FLOCK(f);
|
||||
|
||||
if (!f->rpos) __toread(f);
|
||||
if (!f->rpos) goto input_fail;
|
||||
|
||||
for (p=(const unsigned char *)fmt; *p; p++) {
|
||||
|
||||
alloc = 0;
|
||||
|
Loading…
Reference in New Issue
Block a user