Skip to content

Commit c5449c0

Browse files
committed
Merge branch 'defensive-programming'
These patches implement some defensive programming to address complaints some static analyzers might have. Signed-off-by: Johannes Schindelin <[email protected]>
2 parents 66039f8 + e6987d3 commit c5449c0

File tree

10 files changed

+24
-6
lines changed

10 files changed

+24
-6
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)) {

commit-graph.c

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

27992799
graph_commit = lookup_commit(r, &cur_oid);
2800+
if (!graph_commit) {
2801+
graph_report(_("failed to look up commit %s for commit-graph"),
2802+
oid_to_hex(&cur_oid));
2803+
continue;
2804+
}
28002805
odb_commit = (struct commit *)create_object(r, &cur_oid, alloc_commit_node(r));
28012806
if (repo_parse_commit_internal(r, odb_commit, 0, 0)) {
28022807
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
@@ -189,7 +189,7 @@ void unparse_commit(struct repository *r, const struct object_id *oid)
189189
{
190190
struct commit *c = lookup_commit(r, oid);
191191

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

fetch-pack.c

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

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

object-name.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1121,7 +1121,7 @@ static enum get_oid_result get_parent(struct repository *r,
11211121
if (ret)
11221122
return ret;
11231123
commit = lookup_commit_reference(r, &oid);
1124-
if (repo_parse_commit(r, commit))
1124+
if (!commit || repo_parse_commit(r, commit))
11251125
return MISSING_OBJECT;
11261126
if (!idx) {
11271127
oidcpy(result, &commit->object.oid);

revision.c

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

3339+
if (!ts)
3340+
return 0;
3341+
33393342
for (p = commit->parents, n = 0; p; p = p->next, n++) {
33403343
if (ts->treesame[n]) {
33413344
if (p->item->object.flags & TMP_MARK) {

0 commit comments

Comments
 (0)