[RFC] kvargs: add free attribute annotation

Message ID 20241031234809.213517-1-stephen@networkplumber.org (mailing list archive)
State New
Delegated to: Thomas Monjalon
Headers
Series [RFC] kvargs: add free attribute annotation |

Checks

Context Check Description
ci/checkpatch success coding style OK
ci/loongarch-compilation success Compilation OK
ci/loongarch-unit-testing success Unit Testing PASS
ci/Intel-compilation success Compilation OK
ci/intel-Testing success Testing PASS
ci/intel-Functional success Functional PASS
ci/github-robot: build success github build: passed
ci/iol-mellanox-Performance success Performance Testing PASS
ci/iol-marvell-Functional success Functional Testing PASS
ci/iol-intel-Functional success Functional Testing PASS
ci/iol-compile-arm64-testing success Testing PASS
ci/iol-unit-arm64-testing success Testing PASS
ci/iol-broadcom-Performance success Performance Testing PASS
ci/iol-unit-amd64-testing success Testing PASS
ci/iol-compile-amd64-testing success Testing PASS
ci/iol-sample-apps-testing success Testing PASS

Commit Message

Stephen Hemminger Oct. 31, 2024, 11:47 p.m. UTC
Pointers created from parsing kvargs must be freed via
rte_kvargs_free. Use compiler attributes to catch misuse.

Signed-off-by: Stephen Hemminger <stephen@networkplumber.org>
---
 lib/kvargs/rte_kvargs.c | 10 ++++++++--
 lib/kvargs/rte_kvargs.h | 40 ++++++++++++++++++++++++----------------
 2 files changed, 32 insertions(+), 18 deletions(-)
  

Patch

diff --git a/lib/kvargs/rte_kvargs.c b/lib/kvargs/rte_kvargs.c
index 1d355516bc..cdfb7318f1 100644
--- a/lib/kvargs/rte_kvargs.c
+++ b/lib/kvargs/rte_kvargs.c
@@ -249,6 +249,13 @@  rte_kvargs_get(const struct rte_kvargs *kvlist, const char *key)
 	return rte_kvargs_get_with_value(kvlist, key, NULL);
 }
 
+/* Helper function to get a pointer with kvargs_free attribute */
+static __rte_dealloc_kvargs_free struct rte_kvargs *
+rte_kvargs_alloc(void)
+{
+	return calloc(1, sizeof(struct rte_kvargs));
+}
+
 /*
  * Parse the arguments "key=value,key=value,..." string and return
  * an allocated structure that contains a key/value list. Also
@@ -259,10 +266,9 @@  rte_kvargs_parse(const char *args, const char * const valid_keys[])
 {
 	struct rte_kvargs *kvlist;
 
-	kvlist = malloc(sizeof(*kvlist));
+	kvlist = rte_kvargs_alloc();
 	if (kvlist == NULL)
 		return NULL;
-	memset(kvlist, 0, sizeof(*kvlist));
 
 	if (rte_kvargs_tokenize(kvlist, args) < 0) {
 		rte_kvargs_free(kvlist);
diff --git a/lib/kvargs/rte_kvargs.h b/lib/kvargs/rte_kvargs.h
index 73fa1e621b..11a78a109b 100644
--- a/lib/kvargs/rte_kvargs.h
+++ b/lib/kvargs/rte_kvargs.h
@@ -21,6 +21,8 @@ 
  * ethernet devices at initialization for arguments parsing.
  */
 
+#include <rte_common.h>
+
 #ifdef __cplusplus
 extern "C" {
 #endif
@@ -62,6 +64,22 @@  struct rte_kvargs {
 	struct rte_kvargs_pair pairs[RTE_KVARGS_MAX]; /**< list of key/values */
 };
 
+/**
+ * Functions that expect return value to be freed with rte_kvargs_free()
+ */
+#define __rte_dealloc_kvargs_free __rte_dealloc(rte_kvargs_free, 1)
+
+/**
+ * Free a rte_kvargs structure
+ *
+ * Free a rte_kvargs structure previously allocated with
+ * rte_kvargs_parse().
+ *
+ * @param kvlist
+ *   The rte_kvargs structure. No error if NULL.
+ */
+void rte_kvargs_free(struct rte_kvargs *kvlist);
+
 /**
  * Allocate a rte_kvargs and store key/value associations from a string
  *
@@ -80,8 +98,9 @@  struct rte_kvargs {
  *   - A pointer to an allocated rte_kvargs structure on success
  *   - NULL on error
  */
-struct rte_kvargs *rte_kvargs_parse(const char *args,
-		const char *const valid_keys[]);
+struct rte_kvargs *
+rte_kvargs_parse(const char *args, const char *const valid_keys[])
+	__rte_malloc __rte_dealloc_kvargs_free;
 
 /**
  * Allocate a rte_kvargs and store key/value associations from a string.
@@ -108,20 +127,9 @@  struct rte_kvargs *rte_kvargs_parse(const char *args,
  *   - A pointer to an allocated rte_kvargs structure on success
  *   - NULL on error
  */
-struct rte_kvargs *rte_kvargs_parse_delim(const char *args,
-		const char *const valid_keys[],
-		const char *valid_ends);
-
-/**
- * Free a rte_kvargs structure
- *
- * Free a rte_kvargs structure previously allocated with
- * rte_kvargs_parse().
- *
- * @param kvlist
- *   The rte_kvargs structure. No error if NULL.
- */
-void rte_kvargs_free(struct rte_kvargs *kvlist);
+struct rte_kvargs *
+rte_kvargs_parse_delim(const char *args, const char *const valid_keys[], const char *valid_ends)
+	__rte_malloc __rte_dealloc_kvargs_free;
 
 /**
  * Get the value associated with a given key.