@@ -2333,6 +2333,19 @@ struct config_store_data {
2333
2333
unsigned int key_seen :1 , section_seen :1 , is_keys_section :1 ;
2334
2334
};
2335
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 );
2342
+ free (store -> value_regex );
2343
+ }
2344
+ free (store -> parsed );
2345
+ free (store -> seen );
2346
+ memset (store , 0 , sizeof (* store ));
2347
+ }
2348
+
2336
2349
static int matches (const char * key , const char * value ,
2337
2350
const struct config_store_data * store )
2338
2351
{
@@ -2667,7 +2680,6 @@ int git_config_set_multivar_in_file_gently(const char *config_filename,
2667
2680
fd = hold_lock_file_for_update (& lock , config_filename , 0 );
2668
2681
if (fd < 0 ) {
2669
2682
error_errno ("could not lock config file %s" , config_filename );
2670
- free (store .key );
2671
2683
ret = CONFIG_NO_LOCK ;
2672
2684
goto out_free ;
2673
2685
}
@@ -2677,8 +2689,6 @@ int git_config_set_multivar_in_file_gently(const char *config_filename,
2677
2689
*/
2678
2690
in_fd = open (config_filename , O_RDONLY );
2679
2691
if ( in_fd < 0 ) {
2680
- free (store .key );
2681
-
2682
2692
if ( ENOENT != errno ) {
2683
2693
error_errno ("opening %s" , config_filename );
2684
2694
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,
2690
2700
goto out_free ;
2691
2701
}
2692
2702
2693
- store .key = (char * )key ;
2703
+ free (store .key );
2704
+ store .key = xstrdup (key );
2694
2705
if (write_section (fd , key , & store ) < 0 ||
2695
2706
write_pair (fd , key , value , & store ) < 0 )
2696
2707
goto write_err_out ;
@@ -2715,7 +2726,7 @@ int git_config_set_multivar_in_file_gently(const char *config_filename,
2715
2726
if (regcomp (store .value_regex , value_regex ,
2716
2727
REG_EXTENDED )) {
2717
2728
error ("invalid pattern: %s" , value_regex );
2718
- free (store .value_regex );
2729
+ FREE_AND_NULL (store .value_regex );
2719
2730
ret = CONFIG_INVALID_PATTERN ;
2720
2731
goto out_free ;
2721
2732
}
@@ -2740,23 +2751,10 @@ int git_config_set_multivar_in_file_gently(const char *config_filename,
2740
2751
config_filename ,
2741
2752
& store , & opts )) {
2742
2753
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
- }
2749
2754
ret = CONFIG_INVALID_FILE ;
2750
2755
goto out_free ;
2751
2756
}
2752
2757
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
-
2760
2758
/* if nothing to unset, or too many matches, error out */
2761
2759
if ((store .seen_nr == 0 && value == NULL ) ||
2762
2760
(store .seen_nr > 1 && multi_replace == 0 )) {
@@ -2887,6 +2885,7 @@ int git_config_set_multivar_in_file_gently(const char *config_filename,
2887
2885
munmap (contents , contents_sz );
2888
2886
if (in_fd >= 0 )
2889
2887
close (in_fd );
2888
+ config_store_data_clear (& store );
2890
2889
return ret ;
2891
2890
2892
2891
write_err_out :
@@ -3127,6 +3126,7 @@ static int git_config_copy_or_rename_section_in_file(const char *config_filename
3127
3126
rollback_lock_file (& lock );
3128
3127
out_no_rollback :
3129
3128
free (filename_buf );
3129
+ config_store_data_clear (& store );
3130
3130
return ret ;
3131
3131
}
3132
3132
0 commit comments