]> git.ipfire.org Git - thirdparty/nftables.git/commitdiff
gmputil: add nft_gmp_free() to free strings from mpz_get_str()
authorThomas Haller <thaller@redhat.com>
Tue, 24 Oct 2023 09:57:08 +0000 (11:57 +0200)
committerPablo Neira Ayuso <pablo@netfilter.org>
Thu, 9 Nov 2023 11:35:01 +0000 (12:35 +0100)
mpz_get_str() (with NULL as first argument) will allocate a buffer using
the allocator functions (mp_set_memory_functions()). We should free
those buffers with the corresponding free function.

Add nft_gmp_free() for that and use it.

The name nft_gmp_free() is chosen because "mini-gmp.c" already has an
internal define called gmp_free(). There wouldn't be a direct conflict,
but using the same name is confusing. And maybe our own defines should
have a clear nft prefix.

Signed-off-by: Thomas Haller <thaller@redhat.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
include/gmputil.h
src/evaluate.c
src/gmputil.c

index c524aced16acf120b38c4b4d5c95e813e27e0086..d1f4dcd2f1c38c62114d84b326e3a42ab41f1770 100644 (file)
@@ -77,4 +77,6 @@ extern void __mpz_switch_byteorder(mpz_t rop, unsigned int len);
        __mpz_switch_byteorder(rop, len);                       \
 }
 
+void nft_gmp_free(void *ptr);
+
 #endif /* NFTABLES_GMPUTIL_H */
index 788cac1fc2b5a44e37bc09c0cccabc5aba0decfa..ba6aa300cd092f49d610b2bc26fc8947900a6bba 100644 (file)
@@ -400,7 +400,7 @@ static int expr_evaluate_integer(struct eval_ctx *ctx, struct expr **exprp)
                expr_error(ctx->msgs, expr,
                           "Value %s exceeds valid range 0-%u",
                           valstr, ctx->ectx.maxval);
-               free(valstr);
+               nft_gmp_free(valstr);
                return -1;
        }
 
@@ -416,8 +416,8 @@ static int expr_evaluate_integer(struct eval_ctx *ctx, struct expr **exprp)
                expr_error(ctx->msgs, expr,
                           "Value %s exceeds valid range 0-%s",
                           valstr, rangestr);
-               free(valstr);
-               free(rangestr);
+               nft_gmp_free(valstr);
+               nft_gmp_free(rangestr);
                mpz_clear(mask);
                return -1;
        }
index cb26b55810c2a1f94b278ccdd1280e3b391350e2..b4529259b031eb938f64d96aa58b5957b318965b 100644 (file)
@@ -184,7 +184,7 @@ int mpz_vfprintf(FILE *fp, const char *f, va_list args)
 
                        str = mpz_get_str(NULL, base, *value);
                        ok = str && fwrite(str, 1, len, fp) == len;
-                       free(str);
+                       nft_gmp_free(str);
 
                        if (!ok)
                                return -1;
@@ -196,3 +196,22 @@ int mpz_vfprintf(FILE *fp, const char *f, va_list args)
        return n;
 }
 #endif
+
+void nft_gmp_free(void *ptr)
+{
+       void (*free_fcn)(void *, size_t);
+
+       /* When we get allocated memory from gmp, it was allocated via the
+        * allocator() from mp_set_memory_functions(). We should pair the free
+        * with the corresponding free function, which we get via
+        * mp_get_memory_functions().
+        *
+        * It's not clear what the correct blk_size is. The default allocator
+        * function of gmp just wraps free() and ignores the extra argument.
+        * Assume 0 is fine.
+        */
+
+       mp_get_memory_functions(NULL, NULL, &free_fcn);
+
+       (*free_fcn)(ptr, 0);
+}