Don't access line[-1] for a zero-length "line" from fgets.
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>
Jim Meyering committed
Jan 4, 2008 at 18:37 UTC
872c930dcb048c1a2b50e6ce881c521dcee15e23
6 files changed
+6
-6
builtin-grep.c
+1
-1
@@ -644,7 +644,7 @@ int cmd_grep(int argc, const char **argv, const char *prefix)
644
die("'%s': %s", argv[1], strerror(errno));
645
while (fgets(buf, sizeof(buf), patterns)) {
646
int len = strlen(buf);
647
- if (buf[len-1] == '\n')
647
+ if (len && buf[len-1] == '\n')
648
buf[len-1] = 0;
649
/* ignore empty line like grep does */
650
if (!buf[0])
builtin-pack-objects.c
+1
-1
@@ -2013,7 +2013,7 @@ static void get_object_list(int ac, const char **av)
2013
2014
while (fgets(line, sizeof(line), stdin) != NULL) {
2015
int len = strlen(line);
2016
- if (line[len - 1] == '\n')
2016
+ if (len && line[len - 1] == '\n')
2017
line[--len] = 0;
2018
if (!len)
2019
break;
builtin-rev-list.c
+1
-1
@@ -520,7 +520,7 @@ static void read_revisions_from_stdin(struct rev_info *revs)
520
521
while (fgets(line, sizeof(line), stdin) != NULL) {
522
int len = strlen(line);
523
- if (line[len - 1] == '\n')
523
+ if (len && line[len - 1] == '\n')
524
line[--len] = 0;
525
if (!len)
526
break;
bundle.c
+1
-1
@@ -48,7 +48,7 @@ int read_bundle_header(const char *path, struct bundle_header *header)
48
: &header->references;
49
char delim;
50
51
- if (buffer[len - 1] == '\n')
51
+ if (len && buffer[len - 1] == '\n')
52
buffer[len - 1] = '\0';
53
if (get_sha1_hex(buffer + offset, sha1)) {
54
warning("unrecognized header: %s", buffer);
server-info.c
+1
-1
@@ -101,7 +101,7 @@ static int read_pack_info_file(const char *infofile)
101
102
while (fgets(line, sizeof(line), fp)) {
103
int len = strlen(line);
104
- if (line[len-1] == '\n')
104
+ if (len && line[len-1] == '\n')
105
line[--len] = 0;
106
107
if (!len)
transport.c
+1
-1
@@ -118,7 +118,7 @@ static void insert_packed_refs(const char *packed_refs, struct ref **list)
118
if (hexval(buffer[0]) > 0xf)
119
continue;
120
len = strlen(buffer);
121
- if (buffer[len - 1] == '\n')
121
+ if (len && buffer[len - 1] == '\n')
122
buffer[--len] = '\0';
123
if (len < 41)
124
continue;