From b7a1e996326ee29a163d67ceb1e6127fdc251c14 Mon Sep 17 00:00:00 2001
From: Michal Domonkos <mdomonko@redhat.com>
Date: Fri, 25 Jun 2021 15:15:08 +0200
Subject: [PATCH] Fix memory leaks in Lua rex extension

This covers the following usage:

expr = rex.newPOSIX(<regex>)
expr:match(<string>)           # A leak occurred here
expr:gmatch(<string>, <func>)  # A leak occurred here

Found by Coverity.
---
 luaext/lrexlib.c | 9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)

diff --git a/luaext/lrexlib.c b/luaext/lrexlib.c
index 09c5a6454..0f29b6371 100644
--- a/luaext/lrexlib.c
+++ b/luaext/lrexlib.c
@@ -80,6 +80,7 @@ static void rex_push_matches(lua_State *L, const char *text, regmatch_t *match,
 
 static int rex_match(lua_State *L)
 {
+  int rc = 0;
   int res;
 #ifdef REG_BASIC
   size_t len;
@@ -109,9 +110,10 @@ static int rex_match(lua_State *L)
     lua_pushstring(L, "n");
     lua_pushnumber(L, ncapt);
     lua_rawset(L, -3);
-    return 3;
-  } else
-    return 0;
+    rc = 3;
+  }
+  free(match);
+  return rc;
 }
 
 static int rex_gmatch(lua_State *L)
@@ -158,6 +160,7 @@ static int rex_gmatch(lua_State *L)
       break;
   }
   lua_pushnumber(L, nmatch);
+  free(match);
   return 1;
 }
 
-- 
2.27.0

