[PATCH v2] lib/ring: do not allow zero size ring

Isaac Boukris iboukris at gmail.com
Tue Nov 11 00:15:45 CET 2025


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

Signed-off-by: Isaac Boukris <iboukris at gmail.com>
---

v2:
* move the fix to the POWEROF2 macro itself.
* fixed style in the test code.

 app/test/test_ring.c | 7 +++++++
 lib/ring/rte_ring.c  | 2 +-
 2 files changed, 8 insertions(+), 1 deletion(-)

diff --git a/app/test/test_ring.c b/app/test/test_ring.c
index ba1fec1de3..3078348b2f 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..0a9fc31530 100644
--- a/lib/ring/rte_ring.c
+++ b/lib/ring/rte_ring.c
@@ -47,7 +47,7 @@ EAL_REGISTER_TAILQ(rte_ring_tailq)
 		     RING_F_MP_HTS_ENQ | RING_F_MC_HTS_DEQ)
 
 /* true if x is a power of 2 */
-#define POWEROF2(x) ((((x)-1) & (x)) == 0)
+#define POWEROF2(x) ((x != 0) && (((x)-1) & (x)) == 0)
 
 /* by default set head/tail distance as 1/8 of ring capacity */
 #define HTD_MAX_DEF	8
-- 
2.49.0



More information about the dev mailing list