This is an automated email from the ASF dual-hosted git repository.

GUIDINGLI pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx.git

commit a9eb47fda98cf0ffb8c84b00bd91240f0b5cfa20
Author: anpeiyun <[email protected]>
AuthorDate: Mon Oct 13 16:24:30 2025 +0800

    libc/stdio: Clear the pushback buffer of new streams.
    
    Guarantee that all new streams start with empty pushback buffers
    by explicitly clearing any ungetc characters during stream
    construction.
    
    Signed-off-by: anpeiyun <[email protected]>
    Signed-off-by: ganjing <[email protected]>
---
 libs/libc/stdio/lib_fopen.c   | 4 ++++
 libs/libc/stdio/lib_freopen.c | 5 +++++
 2 files changed, 9 insertions(+)

diff --git a/libs/libc/stdio/lib_fopen.c b/libs/libc/stdio/lib_fopen.c
index c313ce87619..d510253bd5c 100644
--- a/libs/libc/stdio/lib_fopen.c
+++ b/libs/libc/stdio/lib_fopen.c
@@ -127,7 +127,11 @@ FAR FILE *fdopen(int fd, FAR const char *mode)
   filep->fs_bufpos   = filep->fs_bufstart;
   filep->fs_bufread  = filep->fs_bufstart;
   filep->fs_flags    = __FS_FLAG_UBF; /* Fake setvbuf and fclose */
+#if CONFIG_NUNGET_CHARS > 0
+  /* clear any characters pushed back onto the stream */
 
+  filep->fs_nungotten = 0;
+#endif
 #  ifdef CONFIG_STDIO_LINEBUFFER
   /* Setup buffer flags */
 
diff --git a/libs/libc/stdio/lib_freopen.c b/libs/libc/stdio/lib_freopen.c
index 7a92fc84195..a186aa49045 100644
--- a/libs/libc/stdio/lib_freopen.c
+++ b/libs/libc/stdio/lib_freopen.c
@@ -113,6 +113,11 @@ FAR FILE *freopen(FAR const char *path, FAR const char 
*mode,
 #ifndef CONFIG_STDIO_DISABLE_BUFFERING
       lib_rdflush_unlocked(stream);
 #endif
+#if CONFIG_NUNGET_CHARS > 0
+      /* Clear any characters pushed back onto the stream */
+
+      stream->fs_nungotten = 0;
+#endif
 
       funlockfile(stream);
 

Reply via email to