Skip to content

CodeQL-inspired fixes #1891

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
2 changes: 1 addition & 1 deletion builtin/commit.c
Original file line number Diff line number Diff line change
Expand Up @@ -1022,7 +1022,7 @@ static int prepare_to_commit(const char *index_file, const char *prefix,
for (i = 0; i < the_repository->index->cache_nr; i++)
if (ce_intent_to_add(the_repository->index->cache[i]))
ita_nr++;
committable = the_repository->index->cache_nr - ita_nr > 0;
committable = the_repository->index->cache_nr > ita_nr;
} else {
/*
* Unless the user did explicitly request a submodule
Expand Down
5 changes: 3 additions & 2 deletions builtin/fetch.c
Original file line number Diff line number Diff line change
Expand Up @@ -1755,10 +1755,10 @@ static int do_fetch(struct transport *transport,
} else {
struct branch *branch = branch_get(NULL);

if (transport->remote->fetch.nr)
if (branch && transport->remote->fetch.nr)
refspec_ref_prefixes(&transport->remote->fetch,
&transport_ls_refs_options.ref_prefixes);
if (branch_has_merge_config(branch) &&
if (branch && branch_has_merge_config(branch) &&
!strcmp(branch->remote_name, transport->remote->name)) {
int i;
for (i = 0; i < branch->merge_nr; i++) {
Expand Down Expand Up @@ -2570,6 +2570,7 @@ int cmd_fetch(int argc,
if (server_options.nr)
gtransport->server_options = &server_options;
result = transport_fetch_refs(gtransport, NULL);
gtransport->smart_options->acked_commits = NULL;

oidset_iter_init(&acked_commits, &iter);
while ((oid = oidset_iter_next(&iter)))
Expand Down
12 changes: 7 additions & 5 deletions bundle-uri.c
Original file line number Diff line number Diff line change
Expand Up @@ -532,11 +532,13 @@ static int fetch_bundles_by_token(struct repository *r,
*/
if (!repo_config_get_value(r,
"fetch.bundlecreationtoken",
&creationTokenStr) &&
sscanf(creationTokenStr, "%"PRIu64, &maxCreationToken) == 1 &&
bundles.items[0]->creationToken <= maxCreationToken) {
free(bundles.items);
return 0;
&creationTokenStr)) {
if (sscanf(creationTokenStr, "%"PRIu64, &maxCreationToken) != 1)
maxCreationToken = 0;
if (bundles.items[0]->creationToken <= maxCreationToken) {
free(bundles.items);
return 0;
}
}

/*
Expand Down
147 changes: 77 additions & 70 deletions commit-graph.c
Original file line number Diff line number Diff line change
Expand Up @@ -2508,7 +2508,7 @@ int write_commit_graph(struct object_directory *odb,
const struct commit_graph_opts *opts)
{
struct repository *r = the_repository;
struct write_commit_graph_context *ctx;
struct write_commit_graph_context ctx;
uint32_t i;
int res = 0;
int replace = 0;
Expand All @@ -2530,16 +2530,16 @@ int write_commit_graph(struct object_directory *odb,
return 0;
}

CALLOC_ARRAY(ctx, 1);
ctx->r = r;
ctx->odb = odb;
ctx->append = flags & COMMIT_GRAPH_WRITE_APPEND ? 1 : 0;
ctx->report_progress = flags & COMMIT_GRAPH_WRITE_PROGRESS ? 1 : 0;
ctx->split = flags & COMMIT_GRAPH_WRITE_SPLIT ? 1 : 0;
ctx->opts = opts;
ctx->total_bloom_filter_data_size = 0;
ctx->write_generation_data = (get_configured_generation_version(r) == 2);
ctx->num_generation_data_overflows = 0;
memset(&ctx, 0, sizeof(ctx));
ctx.r = r;
ctx.odb = odb;
ctx.append = flags & COMMIT_GRAPH_WRITE_APPEND ? 1 : 0;
ctx.report_progress = flags & COMMIT_GRAPH_WRITE_PROGRESS ? 1 : 0;
ctx.split = flags & COMMIT_GRAPH_WRITE_SPLIT ? 1 : 0;
ctx.opts = opts;
ctx.total_bloom_filter_data_size = 0;
ctx.write_generation_data = (get_configured_generation_version(r) == 2);
ctx.num_generation_data_overflows = 0;

bloom_settings.hash_version = r->settings.commit_graph_changed_paths_version;
bloom_settings.bits_per_entry = git_env_ulong("GIT_TEST_BLOOM_SETTINGS_BITS_PER_ENTRY",
Expand All @@ -2548,14 +2548,14 @@ int write_commit_graph(struct object_directory *odb,
bloom_settings.num_hashes);
bloom_settings.max_changed_paths = git_env_ulong("GIT_TEST_BLOOM_SETTINGS_MAX_CHANGED_PATHS",
bloom_settings.max_changed_paths);
ctx->bloom_settings = &bloom_settings;
ctx.bloom_settings = &bloom_settings;

init_topo_level_slab(&topo_levels);
ctx->topo_levels = &topo_levels;
ctx.topo_levels = &topo_levels;

prepare_commit_graph(ctx->r);
if (ctx->r->objects->commit_graph) {
struct commit_graph *g = ctx->r->objects->commit_graph;
prepare_commit_graph(ctx.r);
if (ctx.r->objects->commit_graph) {
struct commit_graph *g = ctx.r->objects->commit_graph;

while (g) {
g->topo_levels = &topo_levels;
Expand All @@ -2564,15 +2564,15 @@ int write_commit_graph(struct object_directory *odb,
}

if (flags & COMMIT_GRAPH_WRITE_BLOOM_FILTERS)
ctx->changed_paths = 1;
ctx.changed_paths = 1;
if (!(flags & COMMIT_GRAPH_NO_WRITE_BLOOM_FILTERS)) {
struct commit_graph *g;

g = ctx->r->objects->commit_graph;
g = ctx.r->objects->commit_graph;

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

/* don't propagate the hash_version unless unspecified */
if (bloom_settings.hash_version == -1)
Expand All @@ -2585,116 +2585,123 @@ int write_commit_graph(struct object_directory *odb,

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

if (ctx->split) {
struct commit_graph *g = ctx->r->objects->commit_graph;
if (ctx.split) {
struct commit_graph *g = ctx.r->objects->commit_graph;

while (g) {
ctx->num_commit_graphs_before++;
ctx.num_commit_graphs_before++;
g = g->base_graph;
}

if (ctx->num_commit_graphs_before) {
ALLOC_ARRAY(ctx->commit_graph_filenames_before, ctx->num_commit_graphs_before);
i = ctx->num_commit_graphs_before;
g = ctx->r->objects->commit_graph;
if (ctx.num_commit_graphs_before) {
ALLOC_ARRAY(ctx.commit_graph_filenames_before, ctx.num_commit_graphs_before);
i = ctx.num_commit_graphs_before;
g = ctx.r->objects->commit_graph;

while (g) {
ctx->commit_graph_filenames_before[--i] = xstrdup(g->filename);
ctx.commit_graph_filenames_before[--i] = xstrdup(g->filename);
g = g->base_graph;
}
}

if (ctx->opts)
replace = ctx->opts->split_flags & COMMIT_GRAPH_SPLIT_REPLACE;
if (ctx.opts)
replace = ctx.opts->split_flags & COMMIT_GRAPH_SPLIT_REPLACE;
}

ctx->approx_nr_objects = repo_approximate_object_count(the_repository);
ctx.approx_nr_objects = repo_approximate_object_count(the_repository);

if (ctx->append && ctx->r->objects->commit_graph) {
struct commit_graph *g = ctx->r->objects->commit_graph;
if (ctx.append && ctx.r->objects->commit_graph) {
struct commit_graph *g = ctx.r->objects->commit_graph;
for (i = 0; i < g->num_commits; i++) {
struct object_id oid;
oidread(&oid, g->chunk_oid_lookup + st_mult(g->hash_len, i),
the_repository->hash_algo);
oid_array_append(&ctx->oids, &oid);
oid_array_append(&ctx.oids, &oid);
}
}

if (pack_indexes) {
ctx->order_by_pack = 1;
if ((res = fill_oids_from_packs(ctx, pack_indexes)))
ctx.order_by_pack = 1;
if ((res = fill_oids_from_packs(&ctx, pack_indexes)))
goto cleanup;
}

if (commits) {
if ((res = fill_oids_from_commits(ctx, commits)))
if ((res = fill_oids_from_commits(&ctx, commits)))
goto cleanup;
}

if (!pack_indexes && !commits) {
ctx->order_by_pack = 1;
fill_oids_from_all_packs(ctx);
ctx.order_by_pack = 1;
fill_oids_from_all_packs(&ctx);
}

close_reachable(ctx);
close_reachable(&ctx);

copy_oids_to_commits(ctx);
copy_oids_to_commits(&ctx);

if (ctx->commits.nr >= GRAPH_EDGE_LAST_MASK) {
if (ctx.commits.nr >= GRAPH_EDGE_LAST_MASK) {
error(_("too many commits to write graph"));
res = -1;
goto cleanup;
}

if (!ctx->commits.nr && !replace)
if (!ctx.commits.nr && !replace)
goto cleanup;

if (ctx->split) {
split_graph_merge_strategy(ctx);
if (ctx.split) {
split_graph_merge_strategy(&ctx);

if (!replace)
merge_commit_graphs(ctx);
merge_commit_graphs(&ctx);
} else
ctx->num_commit_graphs_after = 1;
ctx.num_commit_graphs_after = 1;

ctx->trust_generation_numbers = validate_mixed_generation_chain(ctx->r->objects->commit_graph);
ctx.trust_generation_numbers = validate_mixed_generation_chain(ctx.r->objects->commit_graph);

compute_topological_levels(ctx);
if (ctx->write_generation_data)
compute_generation_numbers(ctx);
compute_topological_levels(&ctx);
if (ctx.write_generation_data)
compute_generation_numbers(&ctx);

if (ctx->changed_paths)
compute_bloom_filters(ctx);
if (ctx.changed_paths)
compute_bloom_filters(&ctx);

res = write_commit_graph_file(ctx);
res = write_commit_graph_file(&ctx);

if (ctx->changed_paths)
if (ctx.changed_paths)
deinit_bloom_filters();

if (ctx->split)
mark_commit_graphs(ctx);
if (ctx.split)
mark_commit_graphs(&ctx);

expire_commit_graphs(ctx);
expire_commit_graphs(&ctx);

cleanup:
free(ctx->graph_name);
free(ctx->base_graph_name);
free(ctx->commits.list);
oid_array_clear(&ctx->oids);
free(ctx.graph_name);
free(ctx.base_graph_name);
free(ctx.commits.list);
oid_array_clear(&ctx.oids);
clear_topo_level_slab(&topo_levels);

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

for (i = 0; i < ctx->num_commit_graphs_after; i++) {
free(ctx->commit_graph_filenames_after[i]);
free(ctx->commit_graph_hash_after[i]);
while (g) {
g->topo_levels = NULL;
g = g->base_graph;
}
}
free(ctx->commit_graph_filenames_after);
free(ctx->commit_graph_hash_after);

free(ctx);
for (i = 0; i < ctx.num_commit_graphs_before; i++)
free(ctx.commit_graph_filenames_before[i]);
free(ctx.commit_graph_filenames_before);

for (i = 0; i < ctx.num_commit_graphs_after; i++) {
free(ctx.commit_graph_filenames_after[i]);
free(ctx.commit_graph_hash_after[i]);
}
free(ctx.commit_graph_filenames_after);
free(ctx.commit_graph_hash_after);

return res;
}
Expand Down
2 changes: 1 addition & 1 deletion help.c
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ void exclude_cmds(struct cmdnames *cmds, struct cmdnames *excludes)
else if (cmp == 0) {
ei++;
free(cmds->names[ci++]);
} else if (cmp > 0)
} else
ei++;
}

Expand Down
55 changes: 13 additions & 42 deletions read-cache.c
Original file line number Diff line number Diff line change
Expand Up @@ -1117,48 +1117,19 @@ static int has_dir_name(struct index_state *istate,
*
* Compare the entry's full path with the last path in the index.
*/
if (istate->cache_nr > 0) {
cmp_last = strcmp_offset(name,
istate->cache[istate->cache_nr - 1]->name,
&len_eq_last);
if (cmp_last > 0) {
if (name[len_eq_last] != '/') {
/*
* The entry sorts AFTER the last one in the
* index.
*
* If there were a conflict with "file", then our
* name would start with "file/" and the last index
* entry would start with "file" but not "file/".
*
* The next character after common prefix is
* not '/', so there can be no conflict.
*/
return retval;
} else {
/*
* The entry sorts AFTER the last one in the
* index, and the next character after common
* prefix is '/'.
*
* Either the last index entry is a file in
* conflict with this entry, or it has a name
* which sorts between this entry and the
* potential conflicting file.
*
* In both cases, we fall through to the loop
* below and let the regular search code handle it.
*/
}
} else if (cmp_last == 0) {
/*
* The entry exactly matches the last one in the
* index, but because of multiple stage and CE_REMOVE
* items, we fall through and let the regular search
* code handle it.
*/
}
}
if (!istate->cache_nr)
return 0;

cmp_last = strcmp_offset(name,
istate->cache[istate->cache_nr - 1]->name,
&len_eq_last);
if (cmp_last > 0 && len_eq_last == 0)
/*
* The entry sorts AFTER the last one in the
* index and their paths have no common prefix,
* so there cannot be a F/D conflict.
*/
return 0;

for (;;) {
size_t len;
Expand Down
9 changes: 6 additions & 3 deletions sequencer.c
Original file line number Diff line number Diff line change
Expand Up @@ -2609,9 +2609,12 @@ static int is_command(enum todo_command command, const char **bol)
const char nick = todo_command_info[command].c;
const char *p = *bol;

return (skip_prefix(p, str, &p) || (nick && *p++ == nick)) &&
(*p == ' ' || *p == '\t' || *p == '\n' || *p == '\r' || !*p) &&
(*bol = p);
if ((skip_prefix(p, str, &p) || (nick && *p++ == nick)) &&
(*p == ' ' || *p == '\t' || *p == '\n' || *p == '\r' || !*p)) {
*bol = p;
return 1;
}
return 0;
}

static int check_label_or_ref_arg(enum todo_command command, const char *arg)
Expand Down
Loading
Loading