[v2] eal: force compilation of RTE_ASSERT expression

Message ID 20250204194759.723484-1-stephen@networkplumber.org (mailing list archive)
State New
Delegated to: Thomas Monjalon
Headers
Series [v2] eal: force compilation of RTE_ASSERT expression |

Checks

Context Check Description
ci/checkpatch warning coding style issues
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/iol-marvell-Functional success Functional Testing PASS
ci/github-robot: build success github build: passed
ci/iol-abi-testing success Testing PASS
ci/iol-unit-amd64-testing success Testing PASS
ci/iol-sample-apps-testing success Testing PASS
ci/iol-compile-amd64-testing success Testing PASS
ci/iol-unit-arm64-testing success Testing PASS
ci/iol-compile-arm64-testing success Testing PASS
ci/iol-broadcom-Performance success Performance Testing PASS
ci/iol-intel-Performance success Performance Testing PASS
ci/iol-intel-Functional success Functional Testing PASS

Commit Message

Stephen Hemminger Feb. 4, 2025, 7:46 p.m. UTC
Even if RTE_ENABLE_ASSERT is not enabled, the expression used should
still be checked for compiler warnings. Use sizeof()
and ternary operator in same manner as the assert() macro to
cause the expression to be evaluated but not generate code.

This was motivated by detection of problems in a driver submission
that was not detected until compiled with non default flags.

Signed-off-by: Stephen Hemminger <stephen@networkplumber.org>
---
v2 - reduce checkpatch complaints and reword comment

 lib/eal/include/rte_debug.h | 10 +++++++++-
 1 file changed, 9 insertions(+), 1 deletion(-)
  

Patch

diff --git a/lib/eal/include/rte_debug.h b/lib/eal/include/rte_debug.h
index 74593cd4d4..204e6db017 100644
--- a/lib/eal/include/rte_debug.h
+++ b/lib/eal/include/rte_debug.h
@@ -46,8 +46,16 @@  void rte_dump_stack(void);
 #ifdef RTE_ENABLE_ASSERT
 #define RTE_ASSERT(exp)	RTE_VERIFY(exp)
 #else
-#define RTE_ASSERT(exp) do {} while (0)
+/*
+ * If RTE_ENABLE_ASSERT is not set, the exp is not checked but not evaluated because
+ * of the use of sizeof(). The ternary operator is to allow function pointers
+ * and bit fields, and to suppress the evaluation of any variable length arrays.
+ */
+#define RTE_ASSERT(exp)	do {		\
+	(void)sizeof((exp) ? 1 : 0);	\
+} while (0)
 #endif
+
 #define	RTE_VERIFY(exp)	do {                                                  \
 	if (unlikely(!(exp)))                                                           \
 		rte_panic("line %d\tassert \"%s\" failed\n", __LINE__, #exp); \