Skip to content

Commit 568459b

Browse files
dschogitster
authored andcommitted
Always check the return value of repo_read_object_file()
There are a couple of places in Git's source code where the return value is not checked. As a consequence, they are susceptible to segmentation faults. Signed-off-by: Johannes Schindelin <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 564d025 commit 568459b

File tree

6 files changed

+22
-4
lines changed

6 files changed

+22
-4
lines changed

bisect.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,9 @@ static void show_list(const char *debug, int counted, int nr,
159159
const char *subject_start;
160160
int subject_len;
161161

162+
if (!buf)
163+
die(_("unable to read %s"), oid_to_hex(&commit->object.oid));
164+
162165
fprintf(stderr, "%c%c%c ",
163166
(commit_flags & TREESAME) ? ' ' : 'T',
164167
(commit_flags & UNINTERESTING) ? 'U' : ' ',

builtin/cat-file.c

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,10 @@ static int cat_one_file(int opt, const char *exp_type, const char *obj_name,
222222
&type,
223223
&size);
224224
const char *target;
225+
226+
if (!buffer)
227+
die(_("unable to read %s"), oid_to_hex(&oid));
228+
225229
if (!skip_prefix(buffer, "object ", &target) ||
226230
get_oid_hex(target, &blob_oid))
227231
die("%s not a valid tag", oid_to_hex(&oid));
@@ -417,15 +421,15 @@ static void print_object_or_die(struct batch_options *opt, struct expand_data *d
417421

418422
contents = repo_read_object_file(the_repository, oid, &type,
419423
&size);
424+
if (!contents)
425+
die("object %s disappeared", oid_to_hex(oid));
420426

421427
if (use_mailmap) {
422428
size_t s = size;
423429
contents = replace_idents_using_mailmap(contents, &s);
424430
size = cast_size_t_to_ulong(s);
425431
}
426432

427-
if (!contents)
428-
die("object %s disappeared", oid_to_hex(oid));
429433
if (type != data->type)
430434
die("object %s changed type!?", oid_to_hex(oid));
431435
if (data->info.sizep && size != data->size && !use_mailmap)
@@ -482,6 +486,8 @@ static void batch_object_write(const char *obj_name,
482486

483487
buf = repo_read_object_file(the_repository, &data->oid, &data->type,
484488
&data->size);
489+
if (!buf)
490+
die(_("unable to read %s"), oid_to_hex(&data->oid));
485491
buf = replace_idents_using_mailmap(buf, &s);
486492
data->size = cast_size_t_to_ulong(s);
487493

builtin/grep.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -575,6 +575,8 @@ static int grep_cache(struct grep_opt *opt,
575575

576576
data = repo_read_object_file(the_repository, &ce->oid,
577577
&type, &size);
578+
if (!data)
579+
die(_("unable to read tree %s"), oid_to_hex(&ce->oid));
578580
init_tree_desc(&tree, data, size);
579581

580582
hit |= grep_tree(opt, pathspec, &tree, &name, 0, 0);

builtin/notes.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -718,9 +718,11 @@ static int append_edit(int argc, const char **argv, const char *prefix)
718718
struct strbuf buf = STRBUF_INIT;
719719
char *prev_buf = repo_read_object_file(the_repository, note, &type, &size);
720720

721-
if (prev_buf && size)
721+
if (!prev_buf)
722+
die(_("unable to read %s"), oid_to_hex(note));
723+
if (size)
722724
strbuf_add(&buf, prev_buf, size);
723-
if (d.buf.len && prev_buf && size)
725+
if (d.buf.len && size)
724726
append_separator(&buf);
725727
strbuf_insert(&d.buf, 0, buf.buf, buf.len);
726728

combine-diff.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -338,6 +338,8 @@ static char *grab_blob(struct repository *r,
338338
free_filespec(df);
339339
} else {
340340
blob = repo_read_object_file(r, oid, &type, size);
341+
if (!blob)
342+
die(_("unable to read %s"), oid_to_hex(oid));
341343
if (type != OBJ_BLOB)
342344
die("object '%s' is not a blob!", oid_to_hex(oid));
343345
}

rerere.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -975,6 +975,9 @@ static int handle_cache(struct index_state *istate,
975975
mmfile[i].ptr = repo_read_object_file(the_repository,
976976
&ce->oid, &type,
977977
&size);
978+
if (!mmfile[i].ptr)
979+
die(_("unable to read %s"),
980+
oid_to_hex(&ce->oid));
978981
mmfile[i].size = size;
979982
}
980983
}

0 commit comments

Comments
 (0)