Index: gcc/gcov.c =================================================================== --- gcc/gcov.c (revision 206281) +++ gcc/gcov.c (working copy) @@ -2382,12 +2382,25 @@ read_line (FILE *file) { size_t len = strlen (string + pos); + /* If fgets returns non-zero but the string has zero length, + we have a line that starts with a NUL character. + Return NULL in this case. */ + if (len == 0) + return NULL; if (string[pos + len - 1] == '\n') { string[pos + len - 1] = 0; return string; } pos += len; + /* If fgets returns a short line without NL at the end, and the + file pointer is at EOF, we might have an incomplete last line, + or we have a NUL character on the last line. + Return the string as usual in this case. + However if the file is not at EOF we have a line with an + embedded NUL character. Return NULL in this case. */ + if (pos < string_len - 1) + return feof (file) ? string : NULL; string = XRESIZEVEC (char, string, string_len * 2); string_len *= 2; } Index: gcc/input.c =================================================================== --- gcc/input.c (revision 206281) +++ gcc/input.c (working copy) @@ -106,12 +106,25 @@ read_line (FILE *file) { size_t len = strlen (string + pos); + /* If fgets returns non-zero but the string has zero length, + we have a line that starts with a NUL character. + Return NULL in this case. */ + if (len == 0) + return NULL; if (string[pos + len - 1] == '\n') { string[pos + len - 1] = 0; return string; } pos += len; + /* If fgets returns a short line without NL at the end, and the + file pointer is at EOF, we might have an incomplete last line, + or we have a NUL character on the last line. + Return the string as usual in this case. + However if the file is not at EOF we have a line with an + embedded NUL character. Return NULL in this case. */ + if (pos < string_len - 1) + return feof (file) ? string : NULL; string = XRESIZEVEC (char, string, string_len * 2); string_len *= 2; }