suppress null termination when fgets reads EOF with no data
authorRich Felker <dalias@aerifal.cx>
Fri, 5 Sep 2014 01:37:13 +0000 (21:37 -0400)
committerRich Felker <dalias@aerifal.cx>
Mon, 30 Mar 2015 05:15:44 +0000 (01:15 -0400)
the C standard requires that "the contents of the array remain
unchanged" in this case.

this patch also changes the behavior on read errors, but in that case
"the array contents are indeterminate", so the application cannot
inspect them anyway.

(cherry picked from commit 402611c3ba3be5b3b0486835d98e22ac7ced2722)

src/stdio/fgets.c

index b01a4187037d81e6457e473c4e682e24787c9021..cf5b1039b50e7c6eafb6d792f605f91836d3b118 100644 (file)
@@ -34,7 +34,7 @@ char *fgets(char *restrict s, int n, FILE *restrict f)
                n--;
                if ((*p++ = c) == '\n') break;
        }
-       *p = 0;
+       if (s) *p = 0;
 
        FUNLOCK(f);