Browse Source

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.

(cherry picked from commit 402611c3ba3be5b3b0486835d98e22ac7ced2722)
Rich Felker 10 năm trước cách đây
mục cha
commit
6d14779eab
1 tập tin đã thay đổi với 1 bổ sung1 xóa
  1. 1 1
      src/stdio/fgets.c

+ 1 - 1
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);