From 402611c3ba3be5b3b0486835d98e22ac7ced2722 Mon Sep 17 00:00:00 2001
From: Rich Felker <dalias@aerifal.cx>
Date: Thu, 4 Sep 2014 21:37:13 -0400
Subject: suppress null termination when fgets reads EOF with no data

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.
---
 src/stdio/fgets.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

(limited to 'src')

diff --git a/src/stdio/fgets.c b/src/stdio/fgets.c
index b01a4187..cf5b1039 100644
--- a/src/stdio/fgets.c
+++ b/src/stdio/fgets.c
@@ -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);
 
-- 
cgit v1.2.3-70-g09d2