@@ -58,6 +58,8 @@ struct merge_options_internal {
58
58
* * these keys serve to intern all the path strings, which allows
59
59
* us to do pointer comparison on directory names instead of
60
60
* strcmp; we just have to be careful to use the interned strings.
61
+ * (Technically paths_to_free may track some strings that were
62
+ * removed from froms paths.)
61
63
*
62
64
* The values of paths:
63
65
* * either a pointer to a merged_info, or a conflict_info struct
@@ -92,6 +94,16 @@ struct merge_options_internal {
92
94
*/
93
95
struct strmap conflicted ;
94
96
97
+ /*
98
+ * paths_to_free: additional list of strings to free
99
+ *
100
+ * If keys are removed from "paths", they are added to paths_to_free
101
+ * to ensure they are later freed. We avoid free'ing immediately since
102
+ * other places (e.g. conflict_info.pathnames[]) may still be
103
+ * referencing these paths.
104
+ */
105
+ struct string_list paths_to_free ;
106
+
95
107
/*
96
108
* current_dir_name: temporary var used in collect_merge_info_callback()
97
109
*
@@ -249,6 +261,17 @@ static void clear_internal_opts(struct merge_options_internal *opti,
249
261
* don't free the keys and we pass 0 for free_values.
250
262
*/
251
263
strmap_clear (& opti -> conflicted , 0 );
264
+
265
+ /*
266
+ * opti->paths_to_free is similar to opti->paths; we created it with
267
+ * strdup_strings = 0 to avoid making _another_ copy of the fullpath
268
+ * but now that we've used it and have no other references to these
269
+ * strings, it is time to deallocate them. We do so by temporarily
270
+ * setting strdup_strings to 1.
271
+ */
272
+ opti -> paths_to_free .strdup_strings = 1 ;
273
+ string_list_clear (& opti -> paths_to_free , 0 );
274
+ opti -> paths_to_free .strdup_strings = 0 ;
252
275
}
253
276
254
277
static int err (struct merge_options * opt , const char * err , ...)
@@ -1243,13 +1266,14 @@ static void merge_start(struct merge_options *opt, struct merge_result *result)
1243
1266
* Although we initialize opt->priv->paths with strdup_strings=0,
1244
1267
* that's just to avoid making yet another copy of an allocated
1245
1268
* string. Putting the entry into paths means we are taking
1246
- * ownership, so we will later free it.
1269
+ * ownership, so we will later free it. paths_to_free is similar.
1247
1270
*
1248
1271
* In contrast, conflicted just has a subset of keys from paths, so
1249
1272
* we don't want to free those (it'd be a duplicate free).
1250
1273
*/
1251
1274
strmap_init_with_options (& opt -> priv -> paths , NULL , 0 );
1252
1275
strmap_init_with_options (& opt -> priv -> conflicted , NULL , 0 );
1276
+ string_list_init (& opt -> priv -> paths_to_free , 0 );
1253
1277
}
1254
1278
1255
1279
/*
0 commit comments