Skip to content

Commit 6bfe19e

Browse files
peffgitster
authored andcommitted
submodule: simplify memory handling in config parsing
We keep a strbuf for the name of the submodule, even though we only ever add one string to it. Let's just use xmemdupz instead, which is slightly more efficient and makes it easier to follow what is going on. Unfortunately, we still end up having to deal with some memory ownership issues in some code branches, as we have to allocate the string in order to do a string list lookup, and then only sometimes want to hand ownership of that string over to the string_list. Still, making that explicit in the code (as opposed to sometimes detaching the strbuf, and then always releasing it) makes it a little more obvious what is going on. Signed-off-by: Jeff King <[email protected]> Reviewed-by: Jonathan Nieder <[email protected]> Acked-by: Jens Lehmann <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 9edbb8b commit 6bfe19e

File tree

1 file changed

+14
-16
lines changed

1 file changed

+14
-16
lines changed

submodule.c

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -127,45 +127,43 @@ void gitmodules_config(void)
127127
int parse_submodule_config_option(const char *var, const char *value)
128128
{
129129
struct string_list_item *config;
130-
struct strbuf submodname = STRBUF_INIT;
131130
const char *name, *key;
132131
int namelen;
133132

134133
if (parse_config_key(var, "submodule", &name, &namelen, &key) < 0 || !name)
135134
return 0;
136135

137136
if (!strcmp(key, "path")) {
138-
strbuf_add(&submodname, name, namelen);
139137
config = unsorted_string_list_lookup(&config_name_for_path, value);
140138
if (config)
141139
free(config->util);
142140
else
143141
config = string_list_append(&config_name_for_path, xstrdup(value));
144-
config->util = strbuf_detach(&submodname, NULL);
145-
strbuf_release(&submodname);
142+
config->util = xmemdupz(name, namelen);
146143
} else if (!strcmp(key, "fetchrecursesubmodules")) {
147-
strbuf_add(&submodname, name, namelen);
148-
config = unsorted_string_list_lookup(&config_fetch_recurse_submodules_for_name, submodname.buf);
144+
char *name_cstr = xmemdupz(name, namelen);
145+
config = unsorted_string_list_lookup(&config_fetch_recurse_submodules_for_name, name_cstr);
149146
if (!config)
150-
config = string_list_append(&config_fetch_recurse_submodules_for_name,
151-
strbuf_detach(&submodname, NULL));
147+
config = string_list_append(&config_fetch_recurse_submodules_for_name, name_cstr);
148+
else
149+
free(name_cstr);
152150
config->util = (void *)(intptr_t)parse_fetch_recurse_submodules_arg(var, value);
153-
strbuf_release(&submodname);
154151
} else if (!strcmp(key, "ignore")) {
152+
char *name_cstr;
153+
155154
if (strcmp(value, "untracked") && strcmp(value, "dirty") &&
156155
strcmp(value, "all") && strcmp(value, "none")) {
157156
warning("Invalid parameter \"%s\" for config option \"submodule.%s.ignore\"", value, var);
158157
return 0;
159158
}
160159

161-
strbuf_add(&submodname, name, namelen);
162-
config = unsorted_string_list_lookup(&config_ignore_for_name, submodname.buf);
163-
if (config)
160+
name_cstr = xmemdupz(name, namelen);
161+
config = unsorted_string_list_lookup(&config_ignore_for_name, name_cstr);
162+
if (config) {
164163
free(config->util);
165-
else
166-
config = string_list_append(&config_ignore_for_name,
167-
strbuf_detach(&submodname, NULL));
168-
strbuf_release(&submodname);
164+
free(name_cstr);
165+
} else
166+
config = string_list_append(&config_ignore_for_name, name_cstr);
169167
config->util = xstrdup(value);
170168
return 0;
171169
}

0 commit comments

Comments
 (0)