From b248bfe395b5755aa68e88078f99c7e08ee605e6 Mon Sep 17 00:00:00 2001 From: hniksic Date: Wed, 5 Dec 2001 15:58:12 -0800 Subject: [PATCH] [svn] Don't abort in read_whole_lines when a line begins with \0. Published in . --- src/ChangeLog | 4 ++++ src/utils.c | 17 +++++++++++++---- 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/src/ChangeLog b/src/ChangeLog index 7bd7a4f5..9620ba74 100644 --- a/src/ChangeLog +++ b/src/ChangeLog @@ -1,3 +1,7 @@ +2001-12-06 Hrvoje Niksic + + * utils.c (read_whole_line): Handle lines beginning with \0. + 2001-12-05 Hrvoje Niksic * recur.c (convert_all_links): Guard against duplicates in diff --git a/src/utils.c b/src/utils.c index 3c13e67f..0130bc2d 100644 --- a/src/utils.c +++ b/src/utils.c @@ -955,13 +955,17 @@ suffix (const char *str) /* Read a line from FP. The function reallocs the storage as needed to accomodate for any length of the line. Reallocs are done - storage exponentially, doubling the storage after each overflow to - minimize the number of calls to realloc() and fgets(). The newline + exponentially, doubling the storage after each overflow to minimize + the number of calls to realloc() and fgets(). The newline character at the end of line is retained. After end-of-file is encountered without anything being read, NULL is returned. NULL is also returned on error. To distinguish - between these two cases, use the stdio function ferror(). */ + between these two cases, use the stdio function ferror(). + + A future version of this function will be rewritten to use fread() + instead of fgets(), and to return the length of the line, which + will make the function usable on files with binary content. */ char * read_whole_line (FILE *fp) @@ -973,9 +977,14 @@ read_whole_line (FILE *fp) while (fgets (line + length, bufsize - length, fp)) { length += strlen (line + length); - assert (length > 0); + if (length == 0) + /* Possible for example when reading from a binary file where + a line begins with \0. */ + continue; + if (line[length - 1] == '\n') break; + /* fgets() guarantees to read the whole line, or to use up the space we've given it. We can double the buffer unconditionally. */