From c39725b398c14d9fcd466d359659b5337bd27df0 Mon Sep 17 00:00:00 2001 From: Gregory Nutt Date: Thu, 12 Mar 2015 07:51:23 -0600 Subject: [PATCH] libc: stdio: Fix NULL pointer dereference in ungetc(). If 'stream' was NULL, 'stream->fs_oflags' was evaluated. From Juha Niskanen --- libc/stdio/lib_ungetc.c | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/libc/stdio/lib_ungetc.c b/libc/stdio/lib_ungetc.c index 9d14499d7b7..bf8699a384d 100644 --- a/libc/stdio/lib_ungetc.c +++ b/libc/stdio/lib_ungetc.c @@ -94,10 +94,17 @@ int ungetc(int c, FAR FILE *stream) int nungotten; #endif + /* Verify that a non-NULL stream was provided */ + + if (!stream) + { + set_errno(EBADF); + return EOF; + } + /* Stream must be open for read access */ - if ((stream && stream->fs_fd < 0) || - ((stream->fs_oflags & O_RDOK) == 0)) + if ((stream->fs_fd < 0) || ((stream->fs_oflags & O_RDOK) == 0)) { set_errno(EBADF); return EOF;