patch 9.1.1734: Memory leak when allocating match fails

Commit: 
https://github.com/vim/vim/commit/37e77282949786bb178c8fb8d6717afcaa40688e
Author: Damien Lejay <[email protected]>
Date:   Fri Sep 5 17:29:31 2025 +0200

    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 <[email protected]>
    Signed-off-by: Hirohito Higashi <[email protected]>
    Signed-off-by: Christian Brabandt <[email protected]>

diff --git a/src/match.c b/src/match.c
index ef2587947..3b664bdb8 100644
--- a/src/match.c
+++ b/src/match.c
@@ -21,7 +21,7 @@
  * Add match to the match list of window "wp".
  * If "pat" is not NULL the pattern will be highlighted with the group "grp"
  * with priority "prio".
- * If "pos_list" is not NULL the list of posisions defines the highlights.
+ * If "pos_list" is not NULL the list of positions defines the highlights.
  * Optionally, a desired ID "id" can be specified (greater than or equal to 1).
  * If no particular ID is desired, -1 must be specified for "id".
  * Return ID of added match, -1 on failure.
@@ -38,7 +38,7 @@ match_add(
 {
     matchitem_T        *cur;
     matchitem_T        *prev;
-    matchitem_T        *m;
+    matchitem_T        *m = NULL;
     int                hlg_id;
     regprog_T  *regprog = NULL;
     int                rtype = UPD_SOME_VALID;
@@ -86,15 +86,12 @@ match_add(
     // Build new match.
     m = ALLOC_CLEAR_ONE(matchitem_T);
     if (m == NULL)
-       return -1;
+       goto fail;
     if (pos_list != NULL && pos_list->lv_len > 0)
     {
        m->mit_pos_array = ALLOC_CLEAR_MULT(llpos_T, pos_list->lv_len);
        if (m->mit_pos_array == NULL)
-       {
-           vim_free(m);
-           return -1;
-       }
+           goto fail;
        m->mit_pos_count = pos_list->lv_len;
     }
     m->mit_id = id;
@@ -213,9 +210,13 @@ match_add(
     return id;
 
 fail:
-    vim_free(m->mit_pattern);
-    vim_free(m->mit_pos_array);
-    vim_free(m);
+    vim_regfree(regprog);
+    if (m != NULL)
+    {
+       vim_free(m->mit_pattern);
+       vim_free(m->mit_pos_array);
+       vim_free(m);
+    }
     return -1;
 }
 
diff --git a/src/version.c b/src/version.c
index aea569a25..8cddb1271 100644
--- a/src/version.c
+++ b/src/version.c
@@ -724,6 +724,8 @@ static char *(features[]) =
 
 static int included_patches[] =
 {   /* Add new patch number below this line */
+/**/
+    1734,
 /**/
     1733,
 /**/

-- 
-- 
You received this message from the "vim_dev" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

--- 
You received this message because you are subscribed to the Google Groups 
"vim_dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion visit 
https://groups.google.com/d/msgid/vim_dev/E1uuYcY-005mxt-SY%40256bit.org.

Raspunde prin e-mail lui