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

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

commit f04659a4e833b8242a6b64aeddd85c92302c9f66
Author: Marco Casaroli <[email protected]>
AuthorDate: Mon Oct 12 18:31:25 2026 +0200

    libs/libc/stdio: Flush a line buffered stream at a newline in fwrite().
    
    A line buffered stream was flushed at a newline only by fputc(),
    fputs() and puts().  printf() writes through fwrite(), so a line that
    printf() wrote to stdout or stderr stayed in the buffer until the
    buffer was full or the program exited.  The output of stderr came
    after later lines of stdout, and was lost when the program ended with
    _exit() or abort().
    
    Flush the stream in fwrite() when it is line buffered and the data
    holds a newline.
    
    Assisted-by: Claude Code:claude-opus-5-5
    Signed-off-by: Marco Casaroli <[email protected]>
---
 libs/libc/stdio/lib_libfwrite.c | 9 +++++++++
 1 file changed, 9 insertions(+)

diff --git a/libs/libc/stdio/lib_libfwrite.c b/libs/libc/stdio/lib_libfwrite.c
index 1b09653fe0c..4416b39c073 100644
--- a/libs/libc/stdio/lib_libfwrite.c
+++ b/libs/libc/stdio/lib_libfwrite.c
@@ -168,6 +168,15 @@ ssize_t lib_fwrite_unlocked(FAR const void *ptr, size_t 
count,
       src += count;
     }
 
+  /* A line buffered stream is flushed when a newline is written */
+
+  if ((stream->fs_flags & __FS_FLAG_LBF) != 0 &&
+      memchr(start, '\n', src - start) != NULL &&
+      lib_fflush_unlocked(stream) < 0)
+    {
+      goto errout;
+    }
+
   /* Return the number of bytes written */
 
   ret = (uintptr_t)src - (uintptr_t)start;

Reply via email to