common/sfc: replace out of bounds condition with static_assert

Message ID 20240118201843.189978-1-stephen@networkplumber.org (mailing list archive)
State Superseded, archived
Delegated to: Ferruh Yigit
Headers
Series common/sfc: replace out of bounds condition with static_assert |

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/github-robot: build fail github build: failed
ci/Intel-compilation fail Compilation issues
ci/intel-Testing success Testing PASS
ci/intel-Functional success Functional PASS
ci/iol-abi-testing success Testing PASS
ci/iol-broadcom-Performance success Performance Testing PASS
ci/iol-broadcom-Functional success Functional Testing PASS
ci/iol-unit-arm64-testing success Testing PASS
ci/iol-compile-amd64-testing fail Testing issues
ci/iol-unit-amd64-testing success Testing PASS
ci/iol-mellanox-Performance success Performance Testing PASS
ci/iol-compile-arm64-testing fail Testing issues
ci/iol-sample-apps-testing success Testing PASS
ci/iol-intel-Functional success Functional Testing PASS
ci/iol-intel-Performance success Performance Testing PASS

Commit Message

Stephen Hemminger Jan. 18, 2024, 8:18 p.m. UTC
  The sfc base code had its own definition of static assertions
using the out of bound array access hack. Replace it with a
static_assert like rte_common.h.

Fixes: f67e4719147d ("net/sfc/base: fix coding style")
Signed-off-by: Stephen Hemminger <stephen@networkplumber.org>
---
 drivers/common/sfc_efx/base/efx.h | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
  

Comments

Morten Brørup Jan. 18, 2024, 11:05 p.m. UTC | #1
> From: Stephen Hemminger [mailto:stephen@networkplumber.org]
> Sent: Thursday, 18 January 2024 21.18
> 
> The sfc base code had its own definition of static assertions
> using the out of bound array access hack. Replace it with a
> static_assert like rte_common.h.
> 
> Fixes: f67e4719147d ("net/sfc/base: fix coding style")
> Signed-off-by: Stephen Hemminger <stephen@networkplumber.org>
> ---
>  drivers/common/sfc_efx/base/efx.h | 4 ++--
>  1 file changed, 2 insertions(+), 2 deletions(-)
> 
> diff --git a/drivers/common/sfc_efx/base/efx.h
> b/drivers/common/sfc_efx/base/efx.h
> index 3312c2fa8f81..9ce266c43610 100644
> --- a/drivers/common/sfc_efx/base/efx.h
> +++ b/drivers/common/sfc_efx/base/efx.h
> @@ -17,8 +17,8 @@
>  extern "C" {
>  #endif
> 
> -#define	EFX_STATIC_ASSERT(_cond)		\
> -	((void)sizeof (char[(_cond) ? 1 : -1]))
> +#define	EFX_STATIC_ASSERT(_cond) \
> +	do { static_assert((_cond), "assert failed" #_cond); } while (0)

This probably works for the DPDK project.

For other projects using the same file, it might also need "#include <assert.h>" (containing the static_assert convenience macro for C), and possibly your workaround for toolchain issues with missing C11 macro in FreeBSD. Maybe not in this file, but somewhere.

Acked-by: Morten Brørup <mb@smartsharesystems.com>
  
Andrew Rybchenko Feb. 13, 2024, 7:47 a.m. UTC | #2
On 1/19/24 02:05, Morten Brørup wrote:
>> From: Stephen Hemminger [mailto:stephen@networkplumber.org]
>> Sent: Thursday, 18 January 2024 21.18
>>
>> The sfc base code had its own definition of static assertions
>> using the out of bound array access hack. Replace it with a
>> static_assert like rte_common.h.
>>
>> Fixes: f67e4719147d ("net/sfc/base: fix coding style")
>> Signed-off-by: Stephen Hemminger <stephen@networkplumber.org>
>> ---
>>   drivers/common/sfc_efx/base/efx.h | 4 ++--
>>   1 file changed, 2 insertions(+), 2 deletions(-)
>>
>> diff --git a/drivers/common/sfc_efx/base/efx.h
>> b/drivers/common/sfc_efx/base/efx.h
>> index 3312c2fa8f81..9ce266c43610 100644
>> --- a/drivers/common/sfc_efx/base/efx.h
>> +++ b/drivers/common/sfc_efx/base/efx.h
>> @@ -17,8 +17,8 @@
>>   extern "C" {
>>   #endif
>>
>> -#define	EFX_STATIC_ASSERT(_cond)		\
>> -	((void)sizeof (char[(_cond) ? 1 : -1]))
>> +#define	EFX_STATIC_ASSERT(_cond) \
>> +	do { static_assert((_cond), "assert failed" #_cond); } while (0)
> 
> This probably works for the DPDK project.
> 
> For other projects using the same file, it might also need "#include <assert.h>" (containing the static_assert convenience macro for C), and possibly your workaround for toolchain issues with missing C11 macro in FreeBSD. Maybe not in this file, but somewhere.
> 
> Acked-by: Morten Brørup <mb@smartsharesystems.com>
> 

Acked-by: Andrew Rybchenko <andrew.rybchenko@oktetlabs.ru>
  

Patch

diff --git a/drivers/common/sfc_efx/base/efx.h b/drivers/common/sfc_efx/base/efx.h
index 3312c2fa8f81..9ce266c43610 100644
--- a/drivers/common/sfc_efx/base/efx.h
+++ b/drivers/common/sfc_efx/base/efx.h
@@ -17,8 +17,8 @@ 
 extern "C" {
 #endif
 
-#define	EFX_STATIC_ASSERT(_cond)		\
-	((void)sizeof (char[(_cond) ? 1 : -1]))
+#define	EFX_STATIC_ASSERT(_cond) \
+	do { static_assert((_cond), "assert failed" #_cond); } while (0)
 
 #define	EFX_ARRAY_SIZE(_array)			\
 	(sizeof (_array) / sizeof ((_array)[0]))