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 <martin.agren@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Martin Ågren committed May 20, 2018 at 12:42 UTC e7347cb9ba350880e6ccccd6fa6a33cec04c5111
1 file changed +3 -7
config.c
+3 -7
@@ -2335,6 +2335,7 @@ struct config_store_data {
2335
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);
@@ -2679,7 +2680,6 @@ int git_config_set_multivar_in_file_gently(const char *config_filename,
2680 fd = hold_lock_file_for_update(&lock, config_filename, 0);
2681 if (fd < 0) {
2682 error_errno("could not lock config file %s", config_filename);
2682 - free(store.key);
2683 ret = CONFIG_NO_LOCK;
2684 goto out_free;
2685 }
@@ -2689,8 +2689,6 @@ int git_config_set_multivar_in_file_gently(const char *config_filename,
2689 */
2690 in_fd = open(config_filename, O_RDONLY);
2691 if ( in_fd < 0 ) {
2692 - free(store.key);
2693 -
2692 if ( ENOENT != errno ) {
2693 error_errno("opening %s", config_filename);
2694 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,
2700 goto out_free;
2701 }
2702
2705 - store.key = (char *)key;
2703 + free(store.key);
2704 + store.key = xstrdup(key);
2705 if (write_section(fd, key, &store) < 0 ||
2706 write_pair(fd, key, value, &store) < 0)
2707 goto write_err_out;
@@ -2752,13 +2751,10 @@ int git_config_set_multivar_in_file_gently(const char *config_filename,
2751 config_filename,
2752 &store, &opts)) {
2753 error("invalid config file %s", config_filename);
2755 - free(store.key);
2754 ret = CONFIG_INVALID_FILE;
2755 goto out_free;
2756 }
2757
2760 - free(store.key);
2761 -
2758 /* if nothing to unset, or too many matches, error out */
2759 if ((store.seen_nr == 0 && value == NULL) ||
2760 (store.seen_nr > 1 && multi_replace == 0)) {