From aba3ed62ae98ede29cad5708ab89c6aa37772778 Mon Sep 17 00:00:00 2001 From: Aurelien DARRAGON Date: Thu, 7 Nov 2024 10:11:17 +0100 Subject: [PATCH] MINOR: pattern: add pat_ref_free() helper func For now, pat_ref struct are never freed, except during init in case of error. The freeing is done directly in the init functions because we don't have an helper for that. No having an helper func to properly free pat_ref struct doesn't encourage us to free unused pat_ref structs, plus it is error-prone if new dynamic members are added to pat_ref struct in the future. To fix that, let's add a pat_ref_free() helper func and use it where relevant (which means only under pat_ref init function for now..) --- src/pattern.c | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/src/pattern.c b/src/pattern.c index 7996bb8da..772628677 100644 --- a/src/pattern.c +++ b/src/pattern.c @@ -1830,6 +1830,14 @@ static struct pat_ref *_pat_ref_new(const char *display, unsigned int flags) return ref; } +/* helper func to properly de-initialize and free pat_ref struct */ +static void pat_ref_free(struct pat_ref *ref) +{ + ha_free(&ref->reference); + ha_free(&ref->display); + free(ref); +} + /* This function creates a new reference. is the reference name. * are PAT_REF_*. /!\ The reference is not checked, and must * be unique. The user must check the reference with "pat_ref_lookup()" @@ -1861,8 +1869,7 @@ struct pat_ref *pat_ref_new(const char *reference, const char *display, unsigned ref->reference = strdup(reference); if (!ref->reference) { - free(ref->display); - free(ref); + pat_ref_free(ref); return NULL; }