diff options
author | Alan Coopersmith <alan.coopersmith@oracle.com> | 2013-10-18 23:23:44 -0700 |
---|---|---|
committer | Alan Coopersmith <alan.coopersmith@oracle.com> | 2013-10-18 23:27:10 -0700 |
commit | a2976b6403ce3a98e8bb4f8db8c1703588800ee8 (patch) | |
tree | 9e2504d41f938ff36b7f4481eb8db28ab8bd855a /man.c | |
parent | 617209a03a219abfd0fc626be1a038d20443178b (diff) |
Ensure fgets read at least one byte before modifying string
If a file has a \0 byte (binary file, strange encoding, corruption),
fgets() can return a string starting with a \0 byte - check for that
before checking to see if the byte before the \0 is a \n, so we don't
reach back before the start of our memory buffer.
Also check that the penultimate byte is a \n before we chop it off,
in case we're reading from a file missing a newline, or a line longer
than fit in the buffer provided to fgets().
Signed-off-by: Alan Coopersmith <alan.coopersmith@oracle.com>
Diffstat (limited to 'man.c')
-rw-r--r-- | man.c | 7 |
1 files changed, 6 insertions, 1 deletions
@@ -290,7 +290,12 @@ ReadMandescFile(SectionList ** section_list, char *path) snprintf(mandesc_file, sizeof(mandesc_file), "%s/%s", path, MANDESC); if ((descfile = fopen(mandesc_file, "r")) != NULL) { while (fgets(string, BUFSIZ, descfile) != NULL) { - string[strlen(string) - 1] = '\0'; /* Strip off the CR. */ + size_t len = strlen(string); + + if (len == 0) + continue; + if (string[len - 1] == '\n') + string[len - 1] = '\0'; /* Strip off the CR. */ if (streq(string, NO_SECTION_DEFAULTS)) { use_defaults = FALSE; |