Skip to content

Commit f9cdaa2

Browse files
committed
Merge branch 'js/misc-fixes'
Assorted fixes for issues found with CodeQL. * js/misc-fixes: sequencer: stop pretending that an assignment is a condition bundle-uri: avoid using undefined output of `sscanf()` commit-graph: avoid using stale stack addresses trace2: avoid "futile conditional" Avoid redundant conditions fetch: avoid unnecessary work when there is no current branch has_dir_name(): make code more obvious upload-pack: rename `enum` to reflect the operation commit-graph: avoid malloc'ing a local variable fetch: carefully clear local variable's address after use commit: simplify code
2 parents d8b48af + 2248833 commit f9cdaa2

File tree

10 files changed

+130
-161
lines changed

10 files changed

+130
-161
lines changed

builtin/commit.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1022,7 +1022,7 @@ static int prepare_to_commit(const char *index_file, const char *prefix,
10221022
for (i = 0; i < the_repository->index->cache_nr; i++)
10231023
if (ce_intent_to_add(the_repository->index->cache[i]))
10241024
ita_nr++;
1025-
committable = the_repository->index->cache_nr - ita_nr > 0;
1025+
committable = the_repository->index->cache_nr > ita_nr;
10261026
} else {
10271027
/*
10281028
* Unless the user did explicitly request a submodule

builtin/fetch.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1728,7 +1728,7 @@ static int do_fetch(struct transport *transport,
17281728
if (transport->remote->follow_remote_head != FOLLOW_REMOTE_NEVER)
17291729
do_set_head = 1;
17301730
}
1731-
if (branch_has_merge_config(branch) &&
1731+
if (branch && branch_has_merge_config(branch) &&
17321732
!strcmp(branch->remote_name, transport->remote->name)) {
17331733
int i;
17341734
for (i = 0; i < branch->merge_nr; i++) {
@@ -2560,6 +2560,7 @@ int cmd_fetch(int argc,
25602560
if (server_options.nr)
25612561
gtransport->server_options = &server_options;
25622562
result = transport_fetch_refs(gtransport, NULL);
2563+
gtransport->smart_options->acked_commits = NULL;
25632564

25642565
oidset_iter_init(&acked_commits, &iter);
25652566
while ((oid = oidset_iter_next(&iter)))

bundle-uri.c

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -532,11 +532,13 @@ static int fetch_bundles_by_token(struct repository *r,
532532
*/
533533
if (!repo_config_get_value(r,
534534
"fetch.bundlecreationtoken",
535-
&creationTokenStr) &&
536-
sscanf(creationTokenStr, "%"PRIu64, &maxCreationToken) == 1 &&
537-
bundles.items[0]->creationToken <= maxCreationToken) {
538-
free(bundles.items);
539-
return 0;
535+
&creationTokenStr)) {
536+
if (sscanf(creationTokenStr, "%"PRIu64, &maxCreationToken) != 1)
537+
maxCreationToken = 0;
538+
if (bundles.items[0]->creationToken <= maxCreationToken) {
539+
free(bundles.items);
540+
return 0;
541+
}
540542
}
541543

542544
/*

commit-graph.c

Lines changed: 77 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -2511,7 +2511,17 @@ int write_commit_graph(struct object_directory *odb,
25112511
const struct commit_graph_opts *opts)
25122512
{
25132513
struct repository *r = the_repository;
2514-
struct write_commit_graph_context *ctx;
2514+
struct write_commit_graph_context ctx = {
2515+
.r = r,
2516+
.odb = odb,
2517+
.append = flags & COMMIT_GRAPH_WRITE_APPEND ? 1 : 0,
2518+
.report_progress = flags & COMMIT_GRAPH_WRITE_PROGRESS ? 1 : 0,
2519+
.split = flags & COMMIT_GRAPH_WRITE_SPLIT ? 1 : 0,
2520+
.opts = opts,
2521+
.total_bloom_filter_data_size = 0,
2522+
.write_generation_data = (get_configured_generation_version(r) == 2),
2523+
.num_generation_data_overflows = 0,
2524+
};
25152525
uint32_t i;
25162526
int res = 0;
25172527
int replace = 0;
@@ -2533,32 +2543,21 @@ int write_commit_graph(struct object_directory *odb,
25332543
return 0;
25342544
}
25352545

2536-
CALLOC_ARRAY(ctx, 1);
2537-
ctx->r = r;
2538-
ctx->odb = odb;
2539-
ctx->append = flags & COMMIT_GRAPH_WRITE_APPEND ? 1 : 0;
2540-
ctx->report_progress = flags & COMMIT_GRAPH_WRITE_PROGRESS ? 1 : 0;
2541-
ctx->split = flags & COMMIT_GRAPH_WRITE_SPLIT ? 1 : 0;
2542-
ctx->opts = opts;
2543-
ctx->total_bloom_filter_data_size = 0;
2544-
ctx->write_generation_data = (get_configured_generation_version(r) == 2);
2545-
ctx->num_generation_data_overflows = 0;
2546-
25472546
bloom_settings.hash_version = r->settings.commit_graph_changed_paths_version;
25482547
bloom_settings.bits_per_entry = git_env_ulong("GIT_TEST_BLOOM_SETTINGS_BITS_PER_ENTRY",
25492548
bloom_settings.bits_per_entry);
25502549
bloom_settings.num_hashes = git_env_ulong("GIT_TEST_BLOOM_SETTINGS_NUM_HASHES",
25512550
bloom_settings.num_hashes);
25522551
bloom_settings.max_changed_paths = git_env_ulong("GIT_TEST_BLOOM_SETTINGS_MAX_CHANGED_PATHS",
25532552
bloom_settings.max_changed_paths);
2554-
ctx->bloom_settings = &bloom_settings;
2553+
ctx.bloom_settings = &bloom_settings;
25552554

25562555
init_topo_level_slab(&topo_levels);
2557-
ctx->topo_levels = &topo_levels;
2556+
ctx.topo_levels = &topo_levels;
25582557

2559-
prepare_commit_graph(ctx->r);
2560-
if (ctx->r->objects->commit_graph) {
2561-
struct commit_graph *g = ctx->r->objects->commit_graph;
2558+
prepare_commit_graph(ctx.r);
2559+
if (ctx.r->objects->commit_graph) {
2560+
struct commit_graph *g = ctx.r->objects->commit_graph;
25622561

25632562
while (g) {
25642563
g->topo_levels = &topo_levels;
@@ -2567,15 +2566,15 @@ int write_commit_graph(struct object_directory *odb,
25672566
}
25682567

25692568
if (flags & COMMIT_GRAPH_WRITE_BLOOM_FILTERS)
2570-
ctx->changed_paths = 1;
2569+
ctx.changed_paths = 1;
25712570
if (!(flags & COMMIT_GRAPH_NO_WRITE_BLOOM_FILTERS)) {
25722571
struct commit_graph *g;
25732572

2574-
g = ctx->r->objects->commit_graph;
2573+
g = ctx.r->objects->commit_graph;
25752574

25762575
/* We have changed-paths already. Keep them in the next graph */
25772576
if (g && g->bloom_filter_settings) {
2578-
ctx->changed_paths = 1;
2577+
ctx.changed_paths = 1;
25792578

25802579
/* don't propagate the hash_version unless unspecified */
25812580
if (bloom_settings.hash_version == -1)
@@ -2588,116 +2587,123 @@ int write_commit_graph(struct object_directory *odb,
25882587

25892588
bloom_settings.hash_version = bloom_settings.hash_version == 2 ? 2 : 1;
25902589

2591-
if (ctx->split) {
2592-
struct commit_graph *g = ctx->r->objects->commit_graph;
2590+
if (ctx.split) {
2591+
struct commit_graph *g = ctx.r->objects->commit_graph;
25932592

25942593
while (g) {
2595-
ctx->num_commit_graphs_before++;
2594+
ctx.num_commit_graphs_before++;
25962595
g = g->base_graph;
25972596
}
25982597

2599-
if (ctx->num_commit_graphs_before) {
2600-
ALLOC_ARRAY(ctx->commit_graph_filenames_before, ctx->num_commit_graphs_before);
2601-
i = ctx->num_commit_graphs_before;
2602-
g = ctx->r->objects->commit_graph;
2598+
if (ctx.num_commit_graphs_before) {
2599+
ALLOC_ARRAY(ctx.commit_graph_filenames_before, ctx.num_commit_graphs_before);
2600+
i = ctx.num_commit_graphs_before;
2601+
g = ctx.r->objects->commit_graph;
26032602

26042603
while (g) {
2605-
ctx->commit_graph_filenames_before[--i] = xstrdup(g->filename);
2604+
ctx.commit_graph_filenames_before[--i] = xstrdup(g->filename);
26062605
g = g->base_graph;
26072606
}
26082607
}
26092608

2610-
if (ctx->opts)
2611-
replace = ctx->opts->split_flags & COMMIT_GRAPH_SPLIT_REPLACE;
2609+
if (ctx.opts)
2610+
replace = ctx.opts->split_flags & COMMIT_GRAPH_SPLIT_REPLACE;
26122611
}
26132612

2614-
ctx->approx_nr_objects = repo_approximate_object_count(the_repository);
2613+
ctx.approx_nr_objects = repo_approximate_object_count(the_repository);
26152614

2616-
if (ctx->append && ctx->r->objects->commit_graph) {
2617-
struct commit_graph *g = ctx->r->objects->commit_graph;
2615+
if (ctx.append && ctx.r->objects->commit_graph) {
2616+
struct commit_graph *g = ctx.r->objects->commit_graph;
26182617
for (i = 0; i < g->num_commits; i++) {
26192618
struct object_id oid;
26202619
oidread(&oid, g->chunk_oid_lookup + st_mult(g->hash_len, i),
26212620
the_repository->hash_algo);
2622-
oid_array_append(&ctx->oids, &oid);
2621+
oid_array_append(&ctx.oids, &oid);
26232622
}
26242623
}
26252624

26262625
if (pack_indexes) {
2627-
ctx->order_by_pack = 1;
2628-
if ((res = fill_oids_from_packs(ctx, pack_indexes)))
2626+
ctx.order_by_pack = 1;
2627+
if ((res = fill_oids_from_packs(&ctx, pack_indexes)))
26292628
goto cleanup;
26302629
}
26312630

26322631
if (commits) {
2633-
if ((res = fill_oids_from_commits(ctx, commits)))
2632+
if ((res = fill_oids_from_commits(&ctx, commits)))
26342633
goto cleanup;
26352634
}
26362635

26372636
if (!pack_indexes && !commits) {
2638-
ctx->order_by_pack = 1;
2639-
fill_oids_from_all_packs(ctx);
2637+
ctx.order_by_pack = 1;
2638+
fill_oids_from_all_packs(&ctx);
26402639
}
26412640

2642-
close_reachable(ctx);
2641+
close_reachable(&ctx);
26432642

2644-
copy_oids_to_commits(ctx);
2643+
copy_oids_to_commits(&ctx);
26452644

2646-
if (ctx->commits.nr >= GRAPH_EDGE_LAST_MASK) {
2645+
if (ctx.commits.nr >= GRAPH_EDGE_LAST_MASK) {
26472646
error(_("too many commits to write graph"));
26482647
res = -1;
26492648
goto cleanup;
26502649
}
26512650

2652-
if (!ctx->commits.nr && !replace)
2651+
if (!ctx.commits.nr && !replace)
26532652
goto cleanup;
26542653

2655-
if (ctx->split) {
2656-
split_graph_merge_strategy(ctx);
2654+
if (ctx.split) {
2655+
split_graph_merge_strategy(&ctx);
26572656

26582657
if (!replace)
2659-
merge_commit_graphs(ctx);
2658+
merge_commit_graphs(&ctx);
26602659
} else
2661-
ctx->num_commit_graphs_after = 1;
2660+
ctx.num_commit_graphs_after = 1;
26622661

2663-
ctx->trust_generation_numbers = validate_mixed_generation_chain(ctx->r->objects->commit_graph);
2662+
ctx.trust_generation_numbers = validate_mixed_generation_chain(ctx.r->objects->commit_graph);
26642663

2665-
compute_topological_levels(ctx);
2666-
if (ctx->write_generation_data)
2667-
compute_generation_numbers(ctx);
2664+
compute_topological_levels(&ctx);
2665+
if (ctx.write_generation_data)
2666+
compute_generation_numbers(&ctx);
26682667

2669-
if (ctx->changed_paths)
2670-
compute_bloom_filters(ctx);
2668+
if (ctx.changed_paths)
2669+
compute_bloom_filters(&ctx);
26712670

2672-
res = write_commit_graph_file(ctx);
2671+
res = write_commit_graph_file(&ctx);
26732672

2674-
if (ctx->changed_paths)
2673+
if (ctx.changed_paths)
26752674
deinit_bloom_filters();
26762675

2677-
if (ctx->split)
2678-
mark_commit_graphs(ctx);
2676+
if (ctx.split)
2677+
mark_commit_graphs(&ctx);
26792678

2680-
expire_commit_graphs(ctx);
2679+
expire_commit_graphs(&ctx);
26812680

26822681
cleanup:
2683-
free(ctx->graph_name);
2684-
free(ctx->base_graph_name);
2685-
free(ctx->commits.list);
2686-
oid_array_clear(&ctx->oids);
2682+
free(ctx.graph_name);
2683+
free(ctx.base_graph_name);
2684+
free(ctx.commits.list);
2685+
oid_array_clear(&ctx.oids);
26872686
clear_topo_level_slab(&topo_levels);
26882687

2689-
for (i = 0; i < ctx->num_commit_graphs_before; i++)
2690-
free(ctx->commit_graph_filenames_before[i]);
2691-
free(ctx->commit_graph_filenames_before);
2688+
if (ctx.r->objects->commit_graph) {
2689+
struct commit_graph *g = ctx.r->objects->commit_graph;
26922690

2693-
for (i = 0; i < ctx->num_commit_graphs_after; i++) {
2694-
free(ctx->commit_graph_filenames_after[i]);
2695-
free(ctx->commit_graph_hash_after[i]);
2691+
while (g) {
2692+
g->topo_levels = NULL;
2693+
g = g->base_graph;
2694+
}
26962695
}
2697-
free(ctx->commit_graph_filenames_after);
2698-
free(ctx->commit_graph_hash_after);
26992696

2700-
free(ctx);
2697+
for (i = 0; i < ctx.num_commit_graphs_before; i++)
2698+
free(ctx.commit_graph_filenames_before[i]);
2699+
free(ctx.commit_graph_filenames_before);
2700+
2701+
for (i = 0; i < ctx.num_commit_graphs_after; i++) {
2702+
free(ctx.commit_graph_filenames_after[i]);
2703+
free(ctx.commit_graph_hash_after[i]);
2704+
}
2705+
free(ctx.commit_graph_filenames_after);
2706+
free(ctx.commit_graph_hash_after);
27012707

27022708
return res;
27032709
}

help.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ void exclude_cmds(struct cmdnames *cmds, struct cmdnames *excludes)
214214
else if (cmp == 0) {
215215
ei++;
216216
free(cmds->names[ci++]);
217-
} else if (cmp > 0)
217+
} else
218218
ei++;
219219
}
220220

read-cache.c

Lines changed: 13 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1117,48 +1117,19 @@ static int has_dir_name(struct index_state *istate,
11171117
*
11181118
* Compare the entry's full path with the last path in the index.
11191119
*/
1120-
if (istate->cache_nr > 0) {
1121-
cmp_last = strcmp_offset(name,
1122-
istate->cache[istate->cache_nr - 1]->name,
1123-
&len_eq_last);
1124-
if (cmp_last > 0) {
1125-
if (name[len_eq_last] != '/') {
1126-
/*
1127-
* The entry sorts AFTER the last one in the
1128-
* index.
1129-
*
1130-
* If there were a conflict with "file", then our
1131-
* name would start with "file/" and the last index
1132-
* entry would start with "file" but not "file/".
1133-
*
1134-
* The next character after common prefix is
1135-
* not '/', so there can be no conflict.
1136-
*/
1137-
return retval;
1138-
} else {
1139-
/*
1140-
* The entry sorts AFTER the last one in the
1141-
* index, and the next character after common
1142-
* prefix is '/'.
1143-
*
1144-
* Either the last index entry is a file in
1145-
* conflict with this entry, or it has a name
1146-
* which sorts between this entry and the
1147-
* potential conflicting file.
1148-
*
1149-
* In both cases, we fall through to the loop
1150-
* below and let the regular search code handle it.
1151-
*/
1152-
}
1153-
} else if (cmp_last == 0) {
1154-
/*
1155-
* The entry exactly matches the last one in the
1156-
* index, but because of multiple stage and CE_REMOVE
1157-
* items, we fall through and let the regular search
1158-
* code handle it.
1159-
*/
1160-
}
1161-
}
1120+
if (!istate->cache_nr)
1121+
return 0;
1122+
1123+
cmp_last = strcmp_offset(name,
1124+
istate->cache[istate->cache_nr - 1]->name,
1125+
&len_eq_last);
1126+
if (cmp_last > 0 && name[len_eq_last] != '/')
1127+
/*
1128+
* The entry sorts AFTER the last one in the
1129+
* index and their paths have no common prefix,
1130+
* so there cannot be a F/D conflict.
1131+
*/
1132+
return 0;
11621133

11631134
for (;;) {
11641135
size_t len;

sequencer.c

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2636,9 +2636,12 @@ static int is_command(enum todo_command command, const char **bol)
26362636
const char nick = todo_command_info[command].c;
26372637
const char *p = *bol;
26382638

2639-
return (skip_prefix(p, str, &p) || (nick && *p++ == nick)) &&
2640-
(*p == ' ' || *p == '\t' || *p == '\n' || *p == '\r' || !*p) &&
2641-
(*bol = p);
2639+
if ((skip_prefix(p, str, &p) || (nick && *p++ == nick)) &&
2640+
(*p == ' ' || *p == '\t' || *p == '\n' || *p == '\r' || !*p)) {
2641+
*bol = p;
2642+
return 1;
2643+
}
2644+
return 0;
26422645
}
26432646

26442647
static int check_label_or_ref_arg(enum todo_command command, const char *arg)

0 commit comments

Comments
 (0)