From: Thomas Haller <thaller@redhat.com>
To: NetFilter <netfilter-devel@vger.kernel.org>
Cc: Thomas Haller <thaller@redhat.com>
Subject: [PATCH nft v2 2/4] gmputil: add nft_gmp_free() to free strings from mpz_get_str()
Date: Tue, 24 Oct 2023 11:57:08 +0200 [thread overview]
Message-ID: <20231024095820.1068949-3-thaller@redhat.com> (raw)
In-Reply-To: <20231024095820.1068949-1-thaller@redhat.com>
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>
---
include/gmputil.h | 2 ++
src/evaluate.c | 6 +++---
src/gmputil.c | 21 ++++++++++++++++++++-
3 files changed, 25 insertions(+), 4 deletions(-)
diff --git a/include/gmputil.h b/include/gmputil.h
index c524aced16ac..d1f4dcd2f1c3 100644
--- a/include/gmputil.h
+++ b/include/gmputil.h
@@ -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 */
diff --git a/src/evaluate.c b/src/evaluate.c
index 2196e92813d0..9d5f0e4d94ad 100644
--- a/src/evaluate.c
+++ b/src/evaluate.c
@@ -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;
}
diff --git a/src/gmputil.c b/src/gmputil.c
index cb26b55810c2..b4529259b031 100644
--- a/src/gmputil.c
+++ b/src/gmputil.c
@@ -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);
+}
--
2.41.0
next prev parent reply other threads:[~2023-10-24 10:01 UTC|newest]
Thread overview: 10+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-10-24 9:57 [PATCH nft v2 0/4] [RESENT] remove xfree() and add free_const()+nft_gmp_free() Thomas Haller
2023-10-24 9:57 ` [PATCH nft v2 1/4] datatype: don't return a const string from cgroupv2_get_path() Thomas Haller
2023-10-24 9:57 ` Thomas Haller [this message]
2023-10-24 9:57 ` [PATCH nft v2 3/4] all: add free_const() and use it instead of xfree() Thomas Haller
2023-10-24 9:57 ` [PATCH nft v2 4/4] all: remove xfree() and use plain free() Thomas Haller
2023-11-06 13:35 ` [PATCH nft v2 0/4] [RESENT] remove xfree() and add free_const()+nft_gmp_free() Pablo Neira Ayuso
2023-11-09 16:05 ` Pablo Neira Ayuso
2023-11-09 17:14 ` Thomas Haller
2023-11-09 19:15 ` Pablo Neira Ayuso
2023-11-09 20:02 ` Thomas Haller
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20231024095820.1068949-3-thaller@redhat.com \
--to=thaller@redhat.com \
--cc=netfilter-devel@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).