Skip to content

Commit

Permalink
eal: replace out of bounds VLA with static_assert
Browse files Browse the repository at this point in the history
Both Gcc, clang and MSVC have better way to do compile time
assertions rather than using out of bounds array access.
The old method would fail if -Wvla is enabled because compiler
can't determine size in that code.  Also, the use of new
_Static_assert will catch broken code that is passing non-constant
expression to RTE_BUILD_BUG_ON().

Need to add brackets {} around the static_assert() to workaround
a bug in clang. Clang was not handling static_assert() in
a switch case properly.

Signed-off-by: Stephen Hemminger <stephen@networkplumber.org>
Acked-by: Morten Brørup <mb@smartsharesystems.com>
Acked-by: Tyler Retzlaff <roretzla@linux.microsoft.com>
Signed-off-by: 0-day Robot <robot@bytheb.org>
  • Loading branch information
shemminger authored and ovsrobot committed Jan 16, 2024
1 parent 99212ce commit c5ba779
Showing 1 changed file with 2 additions and 1 deletion.
3 changes: 2 additions & 1 deletion lib/eal/include/rte_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
extern "C" {
#endif

#include <assert.h>
#include <stdint.h>
#include <limits.h>

Expand Down Expand Up @@ -495,7 +496,7 @@ rte_is_aligned(const void * const __rte_restrict ptr, const unsigned int align)
/**
* Triggers an error at compilation time if the condition is true.
*/
#define RTE_BUILD_BUG_ON(condition) ((void)sizeof(char[1 - 2*!!(condition)]))
#define RTE_BUILD_BUG_ON(condition) { static_assert(!(condition), #condition); }

/*********** Cache line related macros ********/

Expand Down

0 comments on commit c5ba779

Please sign in to comment.