DPDK patches and discussions
 help / color / mirror / Atom feed
* [PATCH] lib/ring: do not allow zero size ring
@ 2025-11-10 22:28 Isaac Boukris
  2025-11-10 23:15 ` [PATCH v2] " Isaac Boukris
  2025-11-11  6:22 ` [PATCH v3] " Isaac Boukris
  0 siblings, 2 replies; 7+ messages in thread
From: Isaac Boukris @ 2025-11-10 22:28 UTC (permalink / raw)
  To: Honnappa Nagarahalli, Konstantin Ananyev; +Cc: dev, Isaac Boukris

this might happen when for instance the ring size is read from
config, and would cause runtime crash.

Signed-off-by: Isaac Boukris <iboukris@gmail.com>
---
 app/test/test_ring.c | 7 +++++++
 lib/ring/rte_ring.c  | 4 ++--
 2 files changed, 9 insertions(+), 2 deletions(-)

diff --git a/app/test/test_ring.c b/app/test/test_ring.c
index ba1fec1de3..81231ecfc2 100644
--- a/app/test/test_ring.c
+++ b/app/test/test_ring.c
@@ -505,6 +505,13 @@ test_ring_negative_tests(void)
 	struct rte_ring *rt = NULL;
 	unsigned int i;
 
+        /* Test zero size ring */
+	rp = test_ring_create("test_zero_size_ring", -1, 0, SOCKET_ID_ANY, 0);
+	if (rp != NULL) {
+		printf("Test failed to detect zero size ring\n");
+		goto test_fail;
+	}
+
 	/* Test with esize not a multiple of 4 */
 	rp = test_ring_create("test_bad_element_size", 23,
 				RING_SIZE + 1, SOCKET_ID_ANY, 0);
diff --git a/lib/ring/rte_ring.c b/lib/ring/rte_ring.c
index edd63aa535..f9434de536 100644
--- a/lib/ring/rte_ring.c
+++ b/lib/ring/rte_ring.c
@@ -67,7 +67,7 @@ rte_ring_get_memsize_elem(unsigned int esize, unsigned int count)
 	}
 
 	/* count must be a power of 2 */
-	if ((!POWEROF2(count)) || (count > RTE_RING_SZ_MASK )) {
+	if ((count == 0) || (!POWEROF2(count)) || (count > RTE_RING_SZ_MASK )) {
 		RING_LOG(ERR,
 			"Requested number of elements is invalid, must be power of 2, and not exceed %u",
 			RTE_RING_SZ_MASK);
@@ -227,7 +227,7 @@ rte_ring_init(struct rte_ring *r, const char *name, unsigned int count,
 		r->mask = r->size - 1;
 		r->capacity = count;
 	} else {
-		if ((!POWEROF2(count)) || (count > RTE_RING_SZ_MASK)) {
+		if ((count == 0) || (!POWEROF2(count)) || (count > RTE_RING_SZ_MASK)) {
 			RING_LOG(ERR,
 				"Requested size is invalid, must be power of 2, and not exceed the size limit %u",
 				RTE_RING_SZ_MASK);
-- 
2.49.0


^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2025-11-11 12:25 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2025-11-10 22:28 [PATCH] lib/ring: do not allow zero size ring Isaac Boukris
2025-11-10 23:15 ` [PATCH v2] " Isaac Boukris
2025-11-10 23:26   ` Wathsala Vithanage
2025-11-11  0:40   ` Morten Brørup
2025-11-11  6:22 ` [PATCH v3] " Isaac Boukris
2025-11-11  8:24   ` Konstantin Ananyev
2025-11-11 12:25     ` Thomas Monjalon

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).