Skip to content

Commit e4c67d3

Browse files
committed
Merge branch 'js/misc-defensive' into seen
Assorted changes that please CodeQL. * js/misc-defensive: shallow: handle missing shallow commits gracefully test-tool repository: check return value of `lookup_commit()` submodule: check return value of `submodule_from_path()` inherit_tracking(): defensive programming describe: defensive programming fetch: defensive programming push: defensive programming stash: defensive programming stash: defensive programming verify_commit_graph(): defensive programming unparse_commit(): defensive programming fetch-pack: defensive programming get_parent(): defensive programming revision: defensive programming
2 parents bc23a12 + ffde052 commit e4c67d3

File tree

13 files changed

+31
-7
lines changed

13 files changed

+31
-7
lines changed

branch.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,8 @@ static int inherit_tracking(struct tracking *tracking, const char *orig_ref)
224224
skip_prefix(orig_ref, "refs/heads/", &bare_ref);
225225

226226
branch = branch_get(bare_ref);
227+
if (!branch)
228+
BUG("could not get branch for '%s", bare_ref);
227229
if (!branch->remote_name) {
228230
warning(_("asked to inherit tracking from '%s', but no remote is set"),
229231
bare_ref);

builtin/describe.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,8 @@ static void describe_commit(struct object_id *oid, struct strbuf *dst)
324324
unsigned int unannotated_cnt = 0;
325325

326326
cmit = lookup_commit_reference(the_repository, oid);
327+
if (!cmit)
328+
die(_("could not look up commit '%s'"), oid_to_hex(oid));
327329

328330
n = find_commit_name(&cmit->object.oid);
329331
if (n && (tags || all || n->prio == 2)) {

builtin/fetch.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -552,7 +552,7 @@ static struct ref *get_ref_map(struct remote *remote,
552552
if (remote &&
553553
(remote->fetch.nr ||
554554
/* Note: has_merge implies non-NULL branch->remote_name */
555-
(has_merge && !strcmp(branch->remote_name, remote->name)))) {
555+
(has_merge && branch && !strcmp(branch->remote_name, remote->name)))) {
556556
for (i = 0; i < remote->fetch.nr; i++) {
557557
get_fetch_map(remote_refs, &remote->fetch.items[i], &tail, 0);
558558
if (remote->fetch.items[i].dst &&
@@ -570,6 +570,7 @@ static struct ref *get_ref_map(struct remote *remote,
570570
* Note: has_merge implies non-NULL branch->remote_name
571571
*/
572572
if (has_merge &&
573+
branch &&
573574
!strcmp(branch->remote_name, remote->name))
574575
add_merge_config(&ref_map, remote_refs, branch, &tail);
575576
} else if (!prefetch) {

builtin/push.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ static void refspec_append_mapped(struct refspec *refspec, const char *ref,
9090
if (push_default == PUSH_DEFAULT_UPSTREAM &&
9191
skip_prefix(matched->name, "refs/heads/", &branch_name)) {
9292
struct branch *branch = branch_get(branch_name);
93-
if (branch->merge_nr == 1 && branch->merge[0]->src) {
93+
if (branch && branch->merge_nr == 1 && branch->merge[0]->src) {
9494
refspec_appendf(refspec, "%s:%s",
9595
ref, branch->merge[0]->src);
9696
return;

builtin/stash.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -343,7 +343,7 @@ static int reset_tree(struct object_id *i_tree, int update, int reset)
343343
memset(&opts, 0, sizeof(opts));
344344

345345
tree = parse_tree_indirect(i_tree);
346-
if (parse_tree(tree))
346+
if (!tree || parse_tree(tree))
347347
return -1;
348348

349349
init_tree_desc(t, &tree->object.oid, tree->buffer, tree->size);
@@ -1456,6 +1456,11 @@ static int do_create_stash(const struct pathspec *ps, struct strbuf *stash_msg_b
14561456
goto done;
14571457
} else {
14581458
head_commit = lookup_commit(the_repository, &info->b_commit);
1459+
if (!head_commit) {
1460+
ret = error(_("could not look up commit '%s'"),
1461+
oid_to_hex (&info->b_commit));
1462+
goto done;
1463+
}
14591464
}
14601465

14611466
if (!check_changes(ps, include_untracked, &untracked_files)) {

builtin/submodule--helper.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1937,6 +1937,9 @@ static int determine_submodule_update_strategy(struct repository *r,
19371937
const char *val;
19381938
int ret;
19391939

1940+
if (!sub)
1941+
return error(_("could not retrieve submodule information for path '%s'"), path);
1942+
19401943
key = xstrfmt("submodule.%s.update", sub->name);
19411944

19421945
if (update) {

commit-graph.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2796,6 +2796,11 @@ static int verify_one_commit_graph(struct repository *r,
27962796
the_repository->hash_algo);
27972797

27982798
graph_commit = lookup_commit(r, &cur_oid);
2799+
if (!graph_commit) {
2800+
graph_report(_("failed to look up commit %s for commit-graph"),
2801+
oid_to_hex(&cur_oid));
2802+
continue;
2803+
}
27992804
odb_commit = (struct commit *)create_object(r, &cur_oid, alloc_commit_node(r));
28002805
if (repo_parse_commit_internal(r, odb_commit, 0, 0)) {
28012806
graph_report(_("failed to parse commit %s from object database for commit-graph"),

commit.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ void unparse_commit(struct repository *r, const struct object_id *oid)
188188
{
189189
struct commit *c = lookup_commit(r, oid);
190190

191-
if (!c->object.parsed)
191+
if (!c || !c->object.parsed)
192192
return;
193193
free_commit_list(c->parents);
194194
c->parents = NULL;

fetch-pack.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ static struct commit *deref_without_lazy_fetch(const struct object_id *oid,
156156
struct tag *tag = (struct tag *)
157157
parse_object(the_repository, oid);
158158

159-
if (!tag->tagged)
159+
if (!tag || !tag->tagged)
160160
return NULL;
161161
if (mark_tags_complete_and_check_obj_db)
162162
tag->object.flags |= COMPLETE;

object-name.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1112,7 +1112,7 @@ static enum get_oid_result get_parent(struct repository *r,
11121112
if (ret)
11131113
return ret;
11141114
commit = lookup_commit_reference(r, &oid);
1115-
if (repo_parse_commit(r, commit))
1115+
if (!commit || repo_parse_commit(r, commit))
11161116
return MISSING_OBJECT;
11171117
if (!idx) {
11181118
oidcpy(result, &commit->object.oid);

revision.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3354,6 +3354,9 @@ static int leave_one_treesame_to_parent(struct rev_info *revs, struct commit *co
33543354
struct commit_list *p;
33553355
unsigned n;
33563356

3357+
if (!ts)
3358+
return 0;
3359+
33573360
for (p = commit->parents, n = 0; p; p = p->next, n++) {
33583361
if (ts->treesame[n]) {
33593362
if (p->item->object.flags & TMP_MARK) {

shallow.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -705,7 +705,8 @@ void assign_shallow_commits_to_refs(struct shallow_info *info,
705705
for (i = 0; i < nr_shallow; i++) {
706706
struct commit *c = lookup_commit(the_repository,
707707
&oid[shallow[i]]);
708-
c->object.flags |= BOTTOM;
708+
if (c)
709+
c->object.flags |= BOTTOM;
709710
}
710711

711712
for (i = 0; i < ref->nr; i++)

t/helper/test-repository.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ static void test_parse_commit_in_graph(const char *gitdir, const char *worktree,
2727
repo_set_hash_algo(the_repository, hash_algo_by_ptr(r.hash_algo));
2828

2929
c = lookup_commit(&r, commit_oid);
30+
if (!c)
31+
die("Could not look up %s", oid_to_hex(commit_oid));
3032

3133
if (!parse_commit_in_graph(&r, c))
3234
die("Couldn't parse commit");

0 commit comments

Comments
 (0)