[PATCH v3] hash: separate param checks in hash create func

Niall Meade niall.meade at intel.com
Mon Oct 14 12:19:16 CEST 2024


Separated name, entries and key_len parameter checks in
rte_hash_create().  Also made the error messages more
informative/verbose to help with debugging. Also added myself to the
mailing list.

Signed-off-by: Niall Meade <niall.meade at intel.com>

---
v3:
* code indentation fix and rte_errno set correctly
v2:
* change hash log messages to be one line

I had name set to NULL in the parameters I was passing to
rte_hash_create() and the error message I got didn't specify which
parameter was invalid.
---
 .mailmap                   |  1 +
 lib/hash/rte_cuckoo_hash.c | 22 ++++++++++++++++++----
 2 files changed, 19 insertions(+), 4 deletions(-)

diff --git a/.mailmap b/.mailmap
index a66da3c8cb..93df2effb2 100644
--- a/.mailmap
+++ b/.mailmap
@@ -1055,6 +1055,7 @@ Nelson Escobar <neescoba at cisco.com>
 Nemanja Marjanovic <nemanja.marjanovic at intel.com>
 Netanel Belgazal <netanel at amazon.com>
 Netanel Gonen <netanelg at mellanox.com>
+Niall Meade <niall.meade at intel.com>
 Niall Power <niall.power at intel.com>
 Nicholas Pratte <npratte at iol.unh.edu>
 Nick Connolly <nick.connolly at arm.com> <nick.connolly at mayadata.io>
diff --git a/lib/hash/rte_cuckoo_hash.c b/lib/hash/rte_cuckoo_hash.c
index 577b5839d3..9575e8aa0c 100644
--- a/lib/hash/rte_cuckoo_hash.c
+++ b/lib/hash/rte_cuckoo_hash.c
@@ -184,17 +184,24 @@ rte_hash_create(const struct rte_hash_parameters *params)
 	hash_list = RTE_TAILQ_CAST(rte_hash_tailq.head, rte_hash_list);
 
 	if (params == NULL) {
+		rte_errno = EINVAL;
 		HASH_LOG(ERR, "%s has no parameters", __func__);
 		return NULL;
 	}
 
 	/* Check for valid parameters */
 	if ((params->entries > RTE_HASH_ENTRIES_MAX) ||
-			(params->entries < RTE_HASH_BUCKET_ENTRIES) ||
-			(params->name == NULL) ||
-			(params->key_len == 0)) {
+			(params->entries < RTE_HASH_BUCKET_ENTRIES)) {
+		rte_errno = EINVAL;
+		HASH_LOG(ERR, "%s() entries (%u) must be in range [%d, %d] inclusive",
+			__func__, params->entries, RTE_HASH_BUCKET_ENTRIES,
+			RTE_HASH_ENTRIES_MAX);
+		return NULL;
+	}
+
+	if (params->key_len == 0) {
 		rte_errno = EINVAL;
-		HASH_LOG(ERR, "%s has invalid parameters", __func__);
+		HASH_LOG(ERR, "%s() key_len must be greater than 0", __func__);
 		return NULL;
 	}
 
@@ -204,6 +211,13 @@ rte_hash_create(const struct rte_hash_parameters *params)
 		return NULL;
 	}
 
+	if (params->name == NULL) {
+		rte_errno = EINVAL;
+		HASH_LOG(ERR, "%s() has invalid parameters, name can't be NULL",
+			__func__);
+		return NULL;
+	}
+
 	/* Validate correct usage of extra options */
 	if ((params->extra_flag & RTE_HASH_EXTRA_FLAGS_RW_CONCURRENCY) &&
 	    (params->extra_flag & RTE_HASH_EXTRA_FLAGS_RW_CONCURRENCY_LF)) {
-- 
2.34.1



More information about the dev mailing list