Skip to content

Commit

Permalink
Always check the return value of repo_read_object_file()
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
dscho committed Feb 5, 2024
1 parent 2a540e4 commit 33e6b8d
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 4 deletions.
3 changes: 3 additions & 0 deletions bisect.c
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,9 @@ static void show_list(const char *debug, int counted, int nr,
const char *subject_start;
int subject_len;

if (!buf)
die(_("unable to read %s"), oid_to_hex(&commit->object.oid));

fprintf(stderr, "%c%c%c ",
(commit_flags & TREESAME) ? ' ' : 'T',
(commit_flags & UNINTERESTING) ? 'U' : ' ',
Expand Down
10 changes: 8 additions & 2 deletions builtin/cat-file.c
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,10 @@ static int cat_one_file(int opt, const char *exp_type, const char *obj_name,
&type,
&size);
const char *target;

if (!buffer)
die(_("unable to read %s"), oid_to_hex(&oid));

if (!skip_prefix(buffer, "object ", &target) ||
get_oid_hex(target, &blob_oid))
die("%s not a valid tag", oid_to_hex(&oid));
Expand Down Expand Up @@ -416,15 +420,15 @@ static void print_object_or_die(struct batch_options *opt, struct expand_data *d

contents = repo_read_object_file(the_repository, oid, &type,
&size);
if (!contents)
die("object %s disappeared", oid_to_hex(oid));

if (use_mailmap) {
size_t s = size;
contents = replace_idents_using_mailmap(contents, &s);
size = cast_size_t_to_ulong(s);
}

if (!contents)
die("object %s disappeared", oid_to_hex(oid));
if (type != data->type)
die("object %s changed type!?", oid_to_hex(oid));
if (data->info.sizep && size != data->size && !use_mailmap)
Expand Down Expand Up @@ -481,6 +485,8 @@ static void batch_object_write(const char *obj_name,

buf = repo_read_object_file(the_repository, &data->oid, &data->type,
&data->size);
if (!buf)
die(_("unable to read %s"), oid_to_hex(&data->oid));
buf = replace_idents_using_mailmap(buf, &s);
data->size = cast_size_t_to_ulong(s);

Expand Down
2 changes: 2 additions & 0 deletions builtin/grep.c
Original file line number Diff line number Diff line change
Expand Up @@ -571,6 +571,8 @@ static int grep_cache(struct grep_opt *opt,

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

hit |= grep_tree(opt, pathspec, &tree, &name, 0, 0);
Expand Down
6 changes: 4 additions & 2 deletions builtin/notes.c
Original file line number Diff line number Diff line change
Expand Up @@ -716,9 +716,11 @@ static int append_edit(int argc, const char **argv, const char *prefix)
struct strbuf buf = STRBUF_INIT;
char *prev_buf = repo_read_object_file(the_repository, note, &type, &size);

if (prev_buf && size)
if (!prev_buf)
die(_("unable to read %s"), oid_to_hex(note));
if (size)
strbuf_add(&buf, prev_buf, size);
if (d.buf.len && prev_buf && size)
if (d.buf.len && size)
append_separator(&buf);
strbuf_insert(&d.buf, 0, buf.buf, buf.len);

Expand Down
2 changes: 2 additions & 0 deletions combine-diff.c
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,8 @@ static char *grab_blob(struct repository *r,
free_filespec(df);
} else {
blob = repo_read_object_file(r, oid, &type, size);
if (!blob)
die(_("unable to read %s"), oid_to_hex(oid));
if (type != OBJ_BLOB)
die("object '%s' is not a blob!", oid_to_hex(oid));
}
Expand Down
3 changes: 3 additions & 0 deletions rerere.c
Original file line number Diff line number Diff line change
Expand Up @@ -973,6 +973,9 @@ static int handle_cache(struct index_state *istate,
mmfile[i].ptr = repo_read_object_file(the_repository,
&ce->oid, &type,
&size);
if (!mmfile[i].ptr)
die(_("unable to read %s"),
oid_to_hex(&ce->oid));
mmfile[i].size = size;
}
}
Expand Down

0 comments on commit 33e6b8d

Please sign in to comment.