Skip to content

Commit e7347cb

Browse files
Martin Ågrengitster
authored andcommitted
config: let config_store_data_clear() handle key
Instead of remembering to free `key` in each code path, let `config_store_data_clear()` handle that. We still need to free it before replacing it, though. Move that freeing closer to the replacing to be safe. Note that in that same part of the code, we can no longer set `key` to the original pointer, but need to `xstrdup()` it. Signed-off-by: Martin Ågren <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 3b82542 commit e7347cb

File tree

1 file changed

+3
-7
lines changed

1 file changed

+3
-7
lines changed

config.c

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2335,6 +2335,7 @@ struct config_store_data {
23352335

23362336
static void config_store_data_clear(struct config_store_data *store)
23372337
{
2338+
free(store->key);
23382339
if (store->value_regex != NULL &&
23392340
store->value_regex != CONFIG_REGEX_NONE) {
23402341
regfree(store->value_regex);
@@ -2679,7 +2680,6 @@ int git_config_set_multivar_in_file_gently(const char *config_filename,
26792680
fd = hold_lock_file_for_update(&lock, config_filename, 0);
26802681
if (fd < 0) {
26812682
error_errno("could not lock config file %s", config_filename);
2682-
free(store.key);
26832683
ret = CONFIG_NO_LOCK;
26842684
goto out_free;
26852685
}
@@ -2689,8 +2689,6 @@ int git_config_set_multivar_in_file_gently(const char *config_filename,
26892689
*/
26902690
in_fd = open(config_filename, O_RDONLY);
26912691
if ( in_fd < 0 ) {
2692-
free(store.key);
2693-
26942692
if ( ENOENT != errno ) {
26952693
error_errno("opening %s", config_filename);
26962694
ret = CONFIG_INVALID_FILE; /* same as "invalid config file" */
@@ -2702,7 +2700,8 @@ int git_config_set_multivar_in_file_gently(const char *config_filename,
27022700
goto out_free;
27032701
}
27042702

2705-
store.key = (char *)key;
2703+
free(store.key);
2704+
store.key = xstrdup(key);
27062705
if (write_section(fd, key, &store) < 0 ||
27072706
write_pair(fd, key, value, &store) < 0)
27082707
goto write_err_out;
@@ -2752,13 +2751,10 @@ int git_config_set_multivar_in_file_gently(const char *config_filename,
27522751
config_filename,
27532752
&store, &opts)) {
27542753
error("invalid config file %s", config_filename);
2755-
free(store.key);
27562754
ret = CONFIG_INVALID_FILE;
27572755
goto out_free;
27582756
}
27592757

2760-
free(store.key);
2761-
27622758
/* if nothing to unset, or too many matches, error out */
27632759
if ((store.seen_nr == 0 && value == NULL) ||
27642760
(store.seen_nr > 1 && multi_replace == 0)) {

0 commit comments

Comments
 (0)