Skip to content

Commit 37e7728

Browse files
dlejaychrisbra
authored andcommitted
patch 9.1.1734: Memory leak when allocating match fails
Problem: Memory leak when allocating match fails Solution: Initialize m to NULL and centralize cleanup via goto fail to avoid leaks on early returns (Damien Lejay) closes: #18204 Signed-off-by: Damien Lejay <damien@lejay.be> Signed-off-by: Hirohito Higashi <h.east.727@gmail.com> Signed-off-by: Christian Brabandt <cb@256bit.org>
1 parent 4daf031 commit 37e7728

2 files changed

Lines changed: 13 additions & 10 deletions

File tree

src/match.c

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
* Add match to the match list of window "wp".
2222
* If "pat" is not NULL the pattern will be highlighted with the group "grp"
2323
* with priority "prio".
24-
* If "pos_list" is not NULL the list of posisions defines the highlights.
24+
* If "pos_list" is not NULL the list of positions defines the highlights.
2525
* Optionally, a desired ID "id" can be specified (greater than or equal to 1).
2626
* If no particular ID is desired, -1 must be specified for "id".
2727
* Return ID of added match, -1 on failure.
@@ -38,7 +38,7 @@ match_add(
3838
{
3939
matchitem_T *cur;
4040
matchitem_T *prev;
41-
matchitem_T *m;
41+
matchitem_T *m = NULL;
4242
int hlg_id;
4343
regprog_T *regprog = NULL;
4444
int rtype = UPD_SOME_VALID;
@@ -86,15 +86,12 @@ match_add(
8686
// Build new match.
8787
m = ALLOC_CLEAR_ONE(matchitem_T);
8888
if (m == NULL)
89-
return -1;
89+
goto fail;
9090
if (pos_list != NULL && pos_list->lv_len > 0)
9191
{
9292
m->mit_pos_array = ALLOC_CLEAR_MULT(llpos_T, pos_list->lv_len);
9393
if (m->mit_pos_array == NULL)
94-
{
95-
vim_free(m);
96-
return -1;
97-
}
94+
goto fail;
9895
m->mit_pos_count = pos_list->lv_len;
9996
}
10097
m->mit_id = id;
@@ -213,9 +210,13 @@ match_add(
213210
return id;
214211

215212
fail:
216-
vim_free(m->mit_pattern);
217-
vim_free(m->mit_pos_array);
218-
vim_free(m);
213+
vim_regfree(regprog);
214+
if (m != NULL)
215+
{
216+
vim_free(m->mit_pattern);
217+
vim_free(m->mit_pos_array);
218+
vim_free(m);
219+
}
219220
return -1;
220221
}
221222

src/version.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -724,6 +724,8 @@ static char *(features[]) =
724724

725725
static int included_patches[] =
726726
{ /* Add new patch number below this line */
727+
/**/
728+
1734,
727729
/**/
728730
1733,
729731
/**/

0 commit comments

Comments
 (0)