Skip to content

Commit f793bf8

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 789e115 + 838a96c commit f793bf8

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
@@ -284,7 +284,7 @@ static int reset_tree(struct object_id *i_tree, int update, int reset)
284284
memset(&opts, 0, sizeof(opts));
285285

286286
tree = parse_tree_indirect(i_tree);
287-
if (parse_tree(tree))
287+
if (!tree || parse_tree(tree))
288288
return -1;
289289

290290
init_tree_desc(t, &tree->object.oid, tree->buffer, tree->size);
@@ -1395,6 +1395,11 @@ static int do_create_stash(const struct pathspec *ps, struct strbuf *stash_msg_b
13951395
goto done;
13961396
} else {
13971397
head_commit = lookup_commit(the_repository, &info->b_commit);
1398+
if (!head_commit) {
1399+
ret = error(_("could not look up commit '%s'"),
1400+
oid_to_hex (&info->b_commit));
1401+
goto done;
1402+
}
13981403
}
13991404

14001405
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
@@ -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
@@ -155,7 +155,7 @@ static struct commit *deref_without_lazy_fetch(const struct object_id *oid,
155155
struct tag *tag = (struct tag *)
156156
parse_object(the_repository, oid);
157157

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

object-name.c

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

revision.c

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

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

0 commit comments

Comments
 (0)