Skip to content

Commit

Permalink
Don't access line[-1] for a zero-length "line" from fgets.
Browse files Browse the repository at this point in the history
A NUL byte at beginning of file, or just after a newline
would provoke an invalid buf[-1] access in a few places.

* builtin-grep.c (cmd_grep): Don't access buf[-1].
* builtin-pack-objects.c (get_object_list): Likewise.
* builtin-rev-list.c (read_revisions_from_stdin): Likewise.
* bundle.c (read_bundle_header): Likewise.
* server-info.c (read_pack_info_file): Likewise.
* transport.c (insert_packed_refs): Likewise.

Signed-off-by: Jim Meyering <meyering@redhat.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
  • Loading branch information
meyering authored and gitster committed Jan 4, 2008
1 parent 95bf4bd commit 872c930
Show file tree
Hide file tree
Showing 6 changed files with 6 additions and 6 deletions.
2 changes: 1 addition & 1 deletion builtin-grep.c
Expand Up @@ -644,7 +644,7 @@ int cmd_grep(int argc, const char **argv, const char *prefix)
die("'%s': %s", argv[1], strerror(errno));
while (fgets(buf, sizeof(buf), patterns)) {
int len = strlen(buf);
if (buf[len-1] == '\n')
if (len && buf[len-1] == '\n')
buf[len-1] = 0;
/* ignore empty line like grep does */
if (!buf[0])
Expand Down
2 changes: 1 addition & 1 deletion builtin-pack-objects.c
Expand Up @@ -2013,7 +2013,7 @@ static void get_object_list(int ac, const char **av)

while (fgets(line, sizeof(line), stdin) != NULL) {
int len = strlen(line);
if (line[len - 1] == '\n')
if (len && line[len - 1] == '\n')
line[--len] = 0;
if (!len)
break;
Expand Down
2 changes: 1 addition & 1 deletion builtin-rev-list.c
Expand Up @@ -520,7 +520,7 @@ static void read_revisions_from_stdin(struct rev_info *revs)

while (fgets(line, sizeof(line), stdin) != NULL) {
int len = strlen(line);
if (line[len - 1] == '\n')
if (len && line[len - 1] == '\n')
line[--len] = 0;
if (!len)
break;
Expand Down
2 changes: 1 addition & 1 deletion bundle.c
Expand Up @@ -48,7 +48,7 @@ int read_bundle_header(const char *path, struct bundle_header *header)
: &header->references;
char delim;

if (buffer[len - 1] == '\n')
if (len && buffer[len - 1] == '\n')
buffer[len - 1] = '\0';
if (get_sha1_hex(buffer + offset, sha1)) {
warning("unrecognized header: %s", buffer);
Expand Down
2 changes: 1 addition & 1 deletion server-info.c
Expand Up @@ -101,7 +101,7 @@ static int read_pack_info_file(const char *infofile)

while (fgets(line, sizeof(line), fp)) {
int len = strlen(line);
if (line[len-1] == '\n')
if (len && line[len-1] == '\n')
line[--len] = 0;

if (!len)
Expand Down
2 changes: 1 addition & 1 deletion transport.c
Expand Up @@ -118,7 +118,7 @@ static void insert_packed_refs(const char *packed_refs, struct ref **list)
if (hexval(buffer[0]) > 0xf)
continue;
len = strlen(buffer);
if (buffer[len - 1] == '\n')
if (len && buffer[len - 1] == '\n')
buffer[--len] = '\0';
if (len < 41)
continue;
Expand Down

0 comments on commit 872c930

Please sign in to comment.