mirror of
https://github.com/git/git.git
synced 2026-08-05 07:31:02 +00:00
list-objects-filter: add and use initializers
In 7e2619d8ff (list_objects_filter_options: plug leak of filter_spec
strings, 2022-09-08), we noted that the filter_spec string_list was
inconsistent in how it handled memory ownership of strings stored in the
list. The fix there was a bit of a band-aid to set the "strdup_strings"
variable right before adding anything.
That works OK, and it lets the users of the API continue to
zero-initialize the struct. But it makes the code a bit hard to follow
and accident-prone, as any other spots appending the filter_spec need to
think about whether to set the strdup_strings value, too (there's one
such spot in partial_clone_get_default_filter_spec(), which is probably
a possible memory leak).
So let's do that full cleanup now. We'll introduce a
LIST_OBJECTS_FILTER_INIT macro and matching function, and use them as
appropriate (though it is for the "_options" struct, this matches the
corresponding list_objects_filter_release() function).
This is harder than it seems! Many other structs, like
git_transport_data, embed the filter struct. So they need to initialize
it themselves even if the rest of the enclosing struct is OK with
zero-initialization. I found all of the relevant spots by grepping
manually for declarations of list_objects_filter_options. And then doing
so recursively for structs which embed it, and ones which embed those,
and so on.
I'm pretty sure I got everything, but there's no change that would alert
the compiler if any topics in flight added new declarations. To catch
this case, we now double-check in the parsing function that things were
initialized as expected and BUG() if appropriate.
Signed-off-by: Jeff King <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:
committed by
Junio C Hamano
parent
aff4bfcf0a
commit
2a01bdedf8
@@ -72,7 +72,7 @@ static struct string_list option_optional_reference = STRING_LIST_INIT_NODUP;
|
||||
static int option_dissociate;
|
||||
static int max_jobs = -1;
|
||||
static struct string_list option_recurse_submodules = STRING_LIST_INIT_NODUP;
|
||||
static struct list_objects_filter_options filter_options;
|
||||
static struct list_objects_filter_options filter_options = LIST_OBJECTS_FILTER_INIT;
|
||||
static int option_filter_submodules = -1; /* unspecified */
|
||||
static int config_filter_submodules = -1; /* unspecified */
|
||||
static struct string_list server_options = STRING_LIST_INIT_NODUP;
|
||||
|
||||
@@ -62,6 +62,7 @@ int cmd_fetch_pack(int argc, const char **argv, const char *prefix)
|
||||
packet_trace_identity("fetch-pack");
|
||||
|
||||
memset(&args, 0, sizeof(args));
|
||||
list_objects_filter_init(&args.filter_options);
|
||||
args.uploadpack = "git-upload-pack";
|
||||
|
||||
for (i = 1; i < argc && *argv[i] == '-'; i++) {
|
||||
|
||||
@@ -80,7 +80,7 @@ static int recurse_submodules_cli = RECURSE_SUBMODULES_DEFAULT;
|
||||
static int recurse_submodules_default = RECURSE_SUBMODULES_ON_DEMAND;
|
||||
static int shown_url = 0;
|
||||
static struct refspec refmap = REFSPEC_INIT_FETCH;
|
||||
static struct list_objects_filter_options filter_options;
|
||||
static struct list_objects_filter_options filter_options = LIST_OBJECTS_FILTER_INIT;
|
||||
static struct string_list server_options = STRING_LIST_INIT_DUP;
|
||||
static struct string_list negotiation_tip = STRING_LIST_INIT_NODUP;
|
||||
static int fetch_write_commit_graph = -1;
|
||||
|
||||
@@ -1754,7 +1754,8 @@ static int module_clone(int argc, const char **argv, const char *prefix)
|
||||
{
|
||||
int dissociate = 0, quiet = 0, progress = 0, require_init = 0;
|
||||
struct module_clone_data clone_data = MODULE_CLONE_DATA_INIT;
|
||||
struct list_objects_filter_options filter_options;
|
||||
struct list_objects_filter_options filter_options =
|
||||
LIST_OBJECTS_FILTER_INIT;
|
||||
|
||||
struct option module_clone_options[] = {
|
||||
OPT_STRING(0, "prefix", &clone_data.prefix,
|
||||
@@ -1796,7 +1797,6 @@ static int module_clone(int argc, const char **argv, const char *prefix)
|
||||
NULL
|
||||
};
|
||||
|
||||
memset(&filter_options, 0, sizeof(filter_options));
|
||||
argc = parse_options(argc, argv, prefix, module_clone_options,
|
||||
git_submodule_helper_usage, 0);
|
||||
|
||||
@@ -2581,7 +2581,8 @@ static int module_update(int argc, const char **argv, const char *prefix)
|
||||
{
|
||||
struct pathspec pathspec;
|
||||
struct update_data opt = UPDATE_DATA_INIT;
|
||||
struct list_objects_filter_options filter_options;
|
||||
struct list_objects_filter_options filter_options =
|
||||
LIST_OBJECTS_FILTER_INIT;
|
||||
int ret;
|
||||
|
||||
struct option module_update_options[] = {
|
||||
@@ -2639,7 +2640,6 @@ static int module_update(int argc, const char **argv, const char *prefix)
|
||||
update_clone_config_from_gitmodules(&opt.max_jobs);
|
||||
git_config(git_update_clone_config, &opt.max_jobs);
|
||||
|
||||
memset(&filter_options, 0, sizeof(filter_options));
|
||||
argc = parse_options(argc, argv, prefix, module_update_options,
|
||||
git_submodule_helper_usage, 0);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user