]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
MINOR: pattern: add pat_ref_free() helper func
authorAurelien DARRAGON <adarragon@haproxy.com>
Thu, 7 Nov 2024 09:11:17 +0000 (10:11 +0100)
committerAurelien DARRAGON <adarragon@haproxy.com>
Thu, 7 Nov 2024 10:36:13 +0000 (11:36 +0100)
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

index 7996bb8dacc7535c328599cb0501c2239815ebcf..772628677e80265de1d815394095490ef9a41ff7 100644 (file)
@@ -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. <ref> is the reference name.
  * <flags> 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;
        }