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

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

commit a73ae1c4723213f198b5e79b3332360bb5a7e4d9
Author: Marco Casaroli <[email protected]>
AuthorDate: Wed Oct 28 05:01:41 2026 +0100

    libs/libc/stdio: Return the last line without a delimiter in getdelim().
    
    getdelim() and getline() returned -1 at the end of the file also when
    they had read characters: the last line of a file that does not end with
    the delimiter was lost.  POSIX returns the characters read, and -1 only if
    no characters were read at the end of the file or on a read error, as
    glibc and musl do.
    
    Return the characters read before the end of the file, NUL-terminated.
    
    Assisted-by: Claude Code:claude-opus-5-5
    Signed-off-by: Marco Casaroli <[email protected]>
---
 libs/libc/stdio/lib_getdelim.c | 11 +++++++++--
 1 file changed, 9 insertions(+), 2 deletions(-)

diff --git a/libs/libc/stdio/lib_getdelim.c b/libs/libc/stdio/lib_getdelim.c
index 1df381c231c..578b8f44e1b 100644
--- a/libs/libc/stdio/lib_getdelim.c
+++ b/libs/libc/stdio/lib_getdelim.c
@@ -180,9 +180,16 @@ ssize_t getdelim(FAR char **lineptr, size_t *n, int 
delimiter,
       ch = fgetc(stream);
       if (ch == EOF)
         {
-          /* errno is not set in this case */
+          /* Return a last line without a delimiter.  Fail only if no
+           * characters were read, or on a read error.
+           */
+
+          if (ncopied == 0 || ferror(stream))
+            {
+              return -1;
+            }
 
-          return -1;
+          break;
         }
 
       /* Save the character in the user buffer and increment the number of

Reply via email to