Skip to content

Commit 3f384aa

Browse files
committed
Merge branch 'ma/config-store-data-clear'
Leak plugging. * ma/config-store-data-clear: config: let `config_store_data_clear()` handle `key` config: let `config_store_data_clear()` handle `value_regex` config: free resources of `struct config_store_data`
2 parents 7c3d15f + e7347cb commit 3f384aa

File tree

1 file changed

+18
-18
lines changed

1 file changed

+18
-18
lines changed

config.c

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2333,6 +2333,19 @@ struct config_store_data {
23332333
unsigned int key_seen:1, section_seen:1, is_keys_section:1;
23342334
};
23352335

2336+
static void config_store_data_clear(struct config_store_data *store)
2337+
{
2338+
free(store->key);
2339+
if (store->value_regex != NULL &&
2340+
store->value_regex != CONFIG_REGEX_NONE) {
2341+
regfree(store->value_regex);
2342+
free(store->value_regex);
2343+
}
2344+
free(store->parsed);
2345+
free(store->seen);
2346+
memset(store, 0, sizeof(*store));
2347+
}
2348+
23362349
static int matches(const char *key, const char *value,
23372350
const struct config_store_data *store)
23382351
{
@@ -2667,7 +2680,6 @@ int git_config_set_multivar_in_file_gently(const char *config_filename,
26672680
fd = hold_lock_file_for_update(&lock, config_filename, 0);
26682681
if (fd < 0) {
26692682
error_errno("could not lock config file %s", config_filename);
2670-
free(store.key);
26712683
ret = CONFIG_NO_LOCK;
26722684
goto out_free;
26732685
}
@@ -2677,8 +2689,6 @@ int git_config_set_multivar_in_file_gently(const char *config_filename,
26772689
*/
26782690
in_fd = open(config_filename, O_RDONLY);
26792691
if ( in_fd < 0 ) {
2680-
free(store.key);
2681-
26822692
if ( ENOENT != errno ) {
26832693
error_errno("opening %s", config_filename);
26842694
ret = CONFIG_INVALID_FILE; /* same as "invalid config file" */
@@ -2690,7 +2700,8 @@ int git_config_set_multivar_in_file_gently(const char *config_filename,
26902700
goto out_free;
26912701
}
26922702

2693-
store.key = (char *)key;
2703+
free(store.key);
2704+
store.key = xstrdup(key);
26942705
if (write_section(fd, key, &store) < 0 ||
26952706
write_pair(fd, key, value, &store) < 0)
26962707
goto write_err_out;
@@ -2715,7 +2726,7 @@ int git_config_set_multivar_in_file_gently(const char *config_filename,
27152726
if (regcomp(store.value_regex, value_regex,
27162727
REG_EXTENDED)) {
27172728
error("invalid pattern: %s", value_regex);
2718-
free(store.value_regex);
2729+
FREE_AND_NULL(store.value_regex);
27192730
ret = CONFIG_INVALID_PATTERN;
27202731
goto out_free;
27212732
}
@@ -2740,23 +2751,10 @@ int git_config_set_multivar_in_file_gently(const char *config_filename,
27402751
config_filename,
27412752
&store, &opts)) {
27422753
error("invalid config file %s", config_filename);
2743-
free(store.key);
2744-
if (store.value_regex != NULL &&
2745-
store.value_regex != CONFIG_REGEX_NONE) {
2746-
regfree(store.value_regex);
2747-
free(store.value_regex);
2748-
}
27492754
ret = CONFIG_INVALID_FILE;
27502755
goto out_free;
27512756
}
27522757

2753-
free(store.key);
2754-
if (store.value_regex != NULL &&
2755-
store.value_regex != CONFIG_REGEX_NONE) {
2756-
regfree(store.value_regex);
2757-
free(store.value_regex);
2758-
}
2759-
27602758
/* if nothing to unset, or too many matches, error out */
27612759
if ((store.seen_nr == 0 && value == NULL) ||
27622760
(store.seen_nr > 1 && multi_replace == 0)) {
@@ -2887,6 +2885,7 @@ int git_config_set_multivar_in_file_gently(const char *config_filename,
28872885
munmap(contents, contents_sz);
28882886
if (in_fd >= 0)
28892887
close(in_fd);
2888+
config_store_data_clear(&store);
28902889
return ret;
28912890

28922891
write_err_out:
@@ -3127,6 +3126,7 @@ static int git_config_copy_or_rename_section_in_file(const char *config_filename
31273126
rollback_lock_file(&lock);
31283127
out_no_rollback:
31293128
free(filename_buf);
3129+
config_store_data_clear(&store);
31303130
return ret;
31313131
}
31323132

0 commit comments

Comments
 (0)