Skip to content

Commit 872c930

Browse files
meyeringgitster
authored andcommitted
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>
1 parent 95bf4bd commit 872c930

File tree

6 files changed

+6
-6
lines changed

6 files changed

+6
-6
lines changed

builtin-grep.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -644,7 +644,7 @@ int cmd_grep(int argc, const char **argv, const char *prefix)
644644
die("'%s': %s", argv[1], strerror(errno));
645645
while (fgets(buf, sizeof(buf), patterns)) {
646646
int len = strlen(buf);
647-
if (buf[len-1] == '\n')
647+
if (len && buf[len-1] == '\n')
648648
buf[len-1] = 0;
649649
/* ignore empty line like grep does */
650650
if (!buf[0])

builtin-pack-objects.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2013,7 +2013,7 @@ static void get_object_list(int ac, const char **av)
20132013

20142014
while (fgets(line, sizeof(line), stdin) != NULL) {
20152015
int len = strlen(line);
2016-
if (line[len - 1] == '\n')
2016+
if (len && line[len - 1] == '\n')
20172017
line[--len] = 0;
20182018
if (!len)
20192019
break;

builtin-rev-list.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -520,7 +520,7 @@ static void read_revisions_from_stdin(struct rev_info *revs)
520520

521521
while (fgets(line, sizeof(line), stdin) != NULL) {
522522
int len = strlen(line);
523-
if (line[len - 1] == '\n')
523+
if (len && line[len - 1] == '\n')
524524
line[--len] = 0;
525525
if (!len)
526526
break;

bundle.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ int read_bundle_header(const char *path, struct bundle_header *header)
4848
: &header->references;
4949
char delim;
5050

51-
if (buffer[len - 1] == '\n')
51+
if (len && buffer[len - 1] == '\n')
5252
buffer[len - 1] = '\0';
5353
if (get_sha1_hex(buffer + offset, sha1)) {
5454
warning("unrecognized header: %s", buffer);

server-info.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ static int read_pack_info_file(const char *infofile)
101101

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

107107
if (!len)

transport.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ static void insert_packed_refs(const char *packed_refs, struct ref **list)
118118
if (hexval(buffer[0]) > 0xf)
119119
continue;
120120
len = strlen(buffer);
121-
if (buffer[len - 1] == '\n')
121+
if (len && buffer[len - 1] == '\n')
122122
buffer[--len] = '\0';
123123
if (len < 41)
124124
continue;

0 commit comments

Comments
 (0)