From 3fdd5211d96952989fbd7b0cfacd30628c6ab57c Mon Sep 17 00:00:00 2001 From: Bruce Hill Date: Thu, 27 Oct 2022 13:24:35 -0400 Subject: Facilitate memory cleanup --- Lua/lbp.c | 6 ++++-- match.c | 6 +++--- match.h | 2 +- 3 files changed, 8 insertions(+), 6 deletions(-) diff --git a/Lua/lbp.c b/Lua/lbp.c index 7c5db3e..462995d 100644 --- a/Lua/lbp.c +++ b/Lua/lbp.c @@ -33,9 +33,11 @@ static void push_match(lua_State *L, match_t *m, const char *start); lua_State *cur_state = NULL; -static void match_error(const char *msg) +static void match_error(char **msg) { - lua_pushstring(cur_state, msg); + lua_pushstring(cur_state, *msg); + free(*msg); + *msg = NULL; lua_error(cur_state); } diff --git a/match.c b/match.c index a7b9799..2aa6054 100644 --- a/match.c +++ b/match.c @@ -51,8 +51,8 @@ typedef struct match_ctx_s { static match_t *unused_matches = NULL; static match_t *in_use_matches = NULL; -static void default_error_handler(const char *msg) { - errx(EXIT_FAILURE, "%s", msg); +static void default_error_handler(char **msg) { + errx(EXIT_FAILURE, "%s", *msg); } static bp_errhand_t error_handler = default_error_handler; @@ -881,7 +881,7 @@ bool next_match(match_t **m, const char *start, const char *end, pat_t *pat, pat cache_destroy(&ctx); *m = NULL; if (error_handler) - error_handler(error_message); + error_handler(&error_message); if (error_message) { free(error_message); diff --git a/match.h b/match.h index 9ead032..c873420 100644 --- a/match.h +++ b/match.h @@ -25,7 +25,7 @@ typedef struct match_s { struct match_s *_children[3]; } match_t; -typedef void (*bp_errhand_t)(const char *err_msg); +typedef void (*bp_errhand_t)(char **err_msg); __attribute__((nonnull)) void recycle_match(match_t **at_m); -- cgit v1.2.3