From 27c2084dffc2cde6bc44f3d3538ea161e9f4463a Mon Sep 17 00:00:00 2001 From: Evan Gates Date: Thu, 26 Feb 2015 13:44:03 -0800 Subject: [PATCH] keep track of allocations due to enstrdup() in match(), then free them when we are finished (also spaces->tabs for one indentation mistake) --- expr.c | 30 +++++++++++++++++++++++++----- 1 file changed, 25 insertions(+), 5 deletions(-) diff --git a/expr.c b/expr.c index 1d7087b..899b3d6 100644 --- a/expr.c +++ b/expr.c @@ -1,6 +1,7 @@ /* See LICENSE file for copyright and license details. */ #include #include +#include #include #include "utf.h" @@ -17,8 +18,24 @@ typedef struct { intmax_t n; } Val; +/* keep track of memory allocated in match() */ +typedef struct Alloc Alloc; +struct Alloc { + Alloc *next; + char *s; +} *allocs; + static size_t intlen; +static char * +add_alloc(char *s) +{ + Alloc *a = emalloc(sizeof(*a)); + *a = (Alloc){ allocs, s }; + allocs = a; + return s; +} + static void enan(Val v) { @@ -87,13 +104,10 @@ match(Val vstr, Val vregx) d = strtoimax(s, &p, 10); if (*s && !*p) /* string matched by subexpression is an integer */ return (Val){ NULL, d }; - - /* FIXME? string is never free()d, worth fixing? - * need to allocate as it could be in buf1 instead of vstr.s */ - return (Val){ enstrdup(3, s), 0 }; + return (Val){ add_alloc(enstrdup(3, s)), 0 }; } regfree(&re); - str += matches[0].rm_so; + str += matches[0].rm_so; return (Val){ NULL, utfnlen(str, matches[0].rm_eo - matches[0].rm_so) }; } @@ -250,6 +264,12 @@ parse(char **expr, int exprlen) else printf("%"PRIdMAX"\n", valp->n); + for (Alloc *p = allocs; p; p = allocs) { + allocs = p->next; + free(p->s); + free(p); + } + return (valp->s && *valp->s) || valp->n; } -- 2.3.0