@@ -58,6 +58,8 @@ struct merge_options_internal {
5858 * * these keys serve to intern all the path strings, which allows
5959 * us to do pointer comparison on directory names instead of
6060 * 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.)
6163 *
6264 * The values of paths:
6365 * * either a pointer to a merged_info, or a conflict_info struct
@@ -92,6 +94,16 @@ struct merge_options_internal {
9294 */
9395 struct strmap conflicted ;
9496
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+
95107 /*
96108 * current_dir_name: temporary var used in collect_merge_info_callback()
97109 *
@@ -249,6 +261,17 @@ static void clear_internal_opts(struct merge_options_internal *opti,
249261 * don't free the keys and we pass 0 for free_values.
250262 */
251263 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 ;
252275}
253276
254277static 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)
12431266 * Although we initialize opt->priv->paths with strdup_strings=0,
12441267 * that's just to avoid making yet another copy of an allocated
12451268 * 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.
12471270 *
12481271 * In contrast, conflicted just has a subset of keys from paths, so
12491272 * we don't want to free those (it'd be a duplicate free).
12501273 */
12511274 strmap_init_with_options (& opt -> priv -> paths , NULL , 0 );
12521275 strmap_init_with_options (& opt -> priv -> conflicted , NULL , 0 );
1276+ string_list_init (& opt -> priv -> paths_to_free , 0 );
12531277}
12541278
12551279/*
0 commit comments