From ec4a615f6cfaed50d20fc4176dc18254533f9f52 Mon Sep 17 00:00:00 2001 From: Petro Karashchenko Date: Fri, 14 Oct 2022 11:54:56 +0200 Subject: [PATCH] libs/libc/stdio: fix ungetc operation Do not modify errno in case of failure Return EOF if the value of c equals to EOF Signed-off-by: Petro Karashchenko --- libs/libc/stdio/lib_ungetc.c | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/libs/libc/stdio/lib_ungetc.c b/libs/libc/stdio/lib_ungetc.c index bf4a5833cd..f5ebea1e39 100644 --- a/libs/libc/stdio/lib_ungetc.c +++ b/libs/libc/stdio/lib_ungetc.c @@ -43,11 +43,10 @@ int ungetc(int c, FAR FILE *stream) int nungotten; #endif - /* Verify that a non-NULL stream was provided */ + /* Verify that a non-NULL stream was provided and c is not EOF */ - if (!stream) + if (!stream || c == EOF) { - set_errno(EBADF); return EOF; } @@ -55,7 +54,6 @@ int ungetc(int c, FAR FILE *stream) if ((stream->fs_fd < 0) || ((stream->fs_oflags & O_RDOK) == 0)) { - set_errno(EBADF); return EOF; } @@ -70,7 +68,6 @@ int ungetc(int c, FAR FILE *stream) else #endif { - set_errno(ENOMEM); return EOF; } }