Skip to content

Commit 77241a6

Browse files
pks-tgitster
authored andcommitted
builtin/merge: fix leaking struct cmdnames in get_strategy()
In "builtin/merge.c" we use the helper infrastructure to figure out what merge strategies there are. We never free contents of the `cmdnames` structures though and thus leak their memory. Fix this by exposing the already existing `clean_cmdnames()` function to release their memory. As this name isn't quite idiomatic, rename it to `cmdnames_release()` while at it. Signed-off-by: Patrick Steinhardt <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 6e95f4e commit 77241a6

File tree

4 files changed

+16
-9
lines changed

4 files changed

+16
-9
lines changed

builtin/merge.c

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ static struct strategy *get_strategy(const char *name)
164164
{
165165
int i;
166166
struct strategy *ret;
167-
static struct cmdnames main_cmds, other_cmds;
167+
static struct cmdnames main_cmds = {0}, other_cmds = {0};
168168
static int loaded;
169169
char *default_strategy = getenv("GIT_TEST_MERGE_ALGORITHM");
170170

@@ -182,10 +182,9 @@ static struct strategy *get_strategy(const char *name)
182182
return &all_strategy[i];
183183

184184
if (!loaded) {
185-
struct cmdnames not_strategies;
185+
struct cmdnames not_strategies = {0};
186186
loaded = 1;
187187

188-
memset(&not_strategies, 0, sizeof(struct cmdnames));
189188
load_command_list("git-merge-", &main_cmds, &other_cmds);
190189
for (i = 0; i < main_cmds.cnt; i++) {
191190
int j, found = 0;
@@ -197,6 +196,8 @@ static struct strategy *get_strategy(const char *name)
197196
add_cmdname(&not_strategies, ent->name, ent->len);
198197
}
199198
exclude_cmds(&main_cmds, &not_strategies);
199+
200+
cmdnames_release(&not_strategies);
200201
}
201202
if (!is_in_cmdlist(&main_cmds, name) && !is_in_cmdlist(&other_cmds, name)) {
202203
fprintf(stderr, _("Could not find merge strategy '%s'.\n"), name);
@@ -216,6 +217,9 @@ static struct strategy *get_strategy(const char *name)
216217
CALLOC_ARRAY(ret, 1);
217218
ret->name = xstrdup(name);
218219
ret->attr = NO_TRIVIAL;
220+
221+
cmdnames_release(&main_cmds);
222+
cmdnames_release(&other_cmds);
219223
return ret;
220224
}
221225

help.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ void add_cmdname(struct cmdnames *cmds, const char *name, int len)
157157
cmds->names[cmds->cnt++] = ent;
158158
}
159159

160-
static void clean_cmdnames(struct cmdnames *cmds)
160+
void cmdnames_release(struct cmdnames *cmds)
161161
{
162162
int i;
163163
for (i = 0; i < cmds->cnt; ++i)
@@ -359,8 +359,8 @@ void list_all_main_cmds(struct string_list *list)
359359
for (i = 0; i < main_cmds.cnt; i++)
360360
string_list_append(list, main_cmds.names[i]->name);
361361

362-
clean_cmdnames(&main_cmds);
363-
clean_cmdnames(&other_cmds);
362+
cmdnames_release(&main_cmds);
363+
cmdnames_release(&other_cmds);
364364
}
365365

366366
void list_all_other_cmds(struct string_list *list)
@@ -375,8 +375,8 @@ void list_all_other_cmds(struct string_list *list)
375375
for (i = 0; i < other_cmds.cnt; i++)
376376
string_list_append(list, other_cmds.names[i]->name);
377377

378-
clean_cmdnames(&main_cmds);
379-
clean_cmdnames(&other_cmds);
378+
cmdnames_release(&main_cmds);
379+
cmdnames_release(&other_cmds);
380380
}
381381

382382
void list_cmds_by_category(struct string_list *list,
@@ -689,7 +689,7 @@ const char *help_unknown_cmd(const char *cmd)
689689
if (autocorrect && n == 1 && SIMILAR_ENOUGH(best_similarity)) {
690690
const char *assumed = main_cmds.names[0]->name;
691691
main_cmds.names[0] = NULL;
692-
clean_cmdnames(&main_cmds);
692+
cmdnames_release(&main_cmds);
693693
fprintf_ln(stderr,
694694
_("WARNING: You called a Git command named '%s', "
695695
"which does not exist."),

help.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ struct cmdnames {
1313
} **names;
1414
};
1515

16+
void cmdnames_release(struct cmdnames *cmds);
17+
1618
static inline void mput_char(char c, unsigned int num)
1719
{
1820
while (num--)

t/t7606-merge-custom.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ Testing a custom strategy.
1414
* (tag: c0) c0
1515
"
1616

17+
TEST_PASSES_SANITIZE_LEAK=true
1718
. ./test-lib.sh
1819

1920
test_expect_success 'set up custom strategy' '

0 commit comments

Comments
 (0)