builtin/remote: fix leaking strings in `branch_list`
The `struct string_list branch_list` is declared as `NODUP`, which makes it not copy strings inserted into it. This causes memory leaks though, as this means it also won't be responsible for _freeing_ inserted strings. Thus, every branch we add to this will leak. Fix this by marking the list as `DUP` instead and free the local copy we have of the variable. Signed-off-by: Patrick Steinhardt <ps@pks.im> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Patrick Steinhardt committed
Aug 1, 2024 at 12:40 UTC
e06c1d1640bc3e0b4da4d0e8119bf22f83c36634
1 file changed
+3
-2
builtin/remote.c
+3
-2
@@ -258,7 +258,7 @@ struct branch_info {
258
char *push_remote_name;
259
};
260
261
-static struct string_list branch_list = STRING_LIST_INIT_NODUP;
261
+static struct string_list branch_list = STRING_LIST_INIT_DUP;
262
263
static const char *abbrev_ref(const char *name, const char *prefix)
264
{
@@ -292,8 +292,8 @@ static int config_read_branches(const char *key, const char *value,
292
type = PUSH_REMOTE;
293
else
294
return 0;
295
- name = xmemdupz(key, key_len);
295
296
+ name = xmemdupz(key, key_len);
297
item = string_list_insert(&branch_list, name);
298
299
if (!item->util)
@@ -337,6 +337,7 @@ static int config_read_branches(const char *key, const char *value,
337
BUG("unexpected type=%d", type);
338
}
339
340
+ free(name);
341
return 0;
342
}
343