changeset 712:838189b856f3

Fix various memory leaks in th_regex. Not that it is usable anyway yet.
author Matti Hamalainen <ccr@tnsp.org>
date Mon, 27 Apr 2020 01:31:53 +0300
parents c91902120e79
children 3ea040bb0dca
files th_regex.c
diffstat 1 files changed, 14 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- a/th_regex.c	Mon Apr 27 01:28:14 2020 +0300
+++ b/th_regex.c	Mon Apr 27 01:31:53 2020 +0300
@@ -708,7 +708,17 @@
 out:
     *pexpr = ctx.data;
 
-    // Free temporary buffers
+    // Free parse context
+    for (size_t n = 0; n < ctx.nstack; n++)
+    {
+        if (ctx.stack[n] != ctx.data)
+        {
+            th_regex_free(ctx.stack[n]);
+            th_free(ctx.stack[n]);
+        }
+    }
+    th_free(ctx.stack);
+
     th_free(tmp);
     th_free(ctx.buf);
     return res;
@@ -736,6 +746,7 @@
         }
 
         th_free(expr->nodes);
+        th_free(expr);
     }
 }
 
@@ -1186,6 +1197,6 @@
  */
 void th_regex_free_matches(th_regex_match_t *matches)
 {
-    th_llist_free_func_node((th_llist_t *) matches,
-        (void (*)(th_llist_t *)) th_regex_free_match);
+    th_llist_free_func_data((th_llist_t *) matches,
+        (void (*)(void *)) th_regex_free_match);
 }