[PATCH] config: Fix invalid static assertions in C

Joel Sherrill joel at rtems.org
Fri Dec 11 13:32:52 UTC 2020


On Fri, Dec 11, 2020, 7:26 AM Gedare Bloom <gedare at rtems.org> wrote:

> Hi Sebastian,
>
> On Fri, Dec 11, 2020 at 5:57 AM Sebastian Huber <
> sebastian.huber at embedded-brains.de> wrote:
>
>> Expressions in static assertions must be integral constant expressions.
>> In
>> integral constant expressions the use of address constant expressions is
>> not
>> allowed.
>>
>> In static initializers the address constant expressions are allowed.
>> Introduce
>> a new macro _CONFIGURE_ASSERT_NOT_NULL() which leads to a compile time
>> error if
>> the second parameter is NULL.  It generates error messages like this if
>> for
>> example
>>
>>   #define CONFIGURE_INIT_TASK_ENTRY_POINT NULL
>>
>> is provided by the application:
>>
>> cpukit/include/rtems/confdefs/inittask.h:51:26: error: size of unnamed
>> array is negative
>>    51 |     ( _type ) sizeof( int[ ( _value ) != NULL ? 1 : -1 ] ) )
>>       |                          ^
>> cpukit/include/rtems/confdefs/inittask.h:170:3: note: in expansion of
>> macro '_CONFIGURE_ASSERT_NOT_NULL'
>>   170 |   _CONFIGURE_ASSERT_NOT_NULL(
>>       |   ^~~~~~~~~~~~~~~~~~~~~~~~~~
>>
>>
> This is a nice fix. The initial error is opaque, but the macro expansion
> seems to be clear enough to help a user debug. Thanks
>
+1

If this was done because of a Coverity flagged issue, please include the
CID in the commit message





>
>
>> Update #4181.
>> ---
>>  cpukit/include/rtems/confdefs/inittask.h | 27 +++++++++++--------
>>  cpukit/include/rtems/confdefs/wkspace.h  | 34 +++++++++++-------------
>>  2 files changed, 32 insertions(+), 29 deletions(-)
>>
>> diff --git a/cpukit/include/rtems/confdefs/inittask.h
>> b/cpukit/include/rtems/confdefs/inittask.h
>> index d5ba521bf9..9dbf0b967b 100644
>> --- a/cpukit/include/rtems/confdefs/inittask.h
>> +++ b/cpukit/include/rtems/confdefs/inittask.h
>> @@ -46,6 +46,10 @@
>>
>>  #ifdef CONFIGURE_INIT
>>
>> +#define _CONFIGURE_ASSERT_NOT_NULL( _type, _value ) \
>> +  ( ( _value ) != NULL ? ( _value ) : \
>> +    ( _type ) sizeof( int[ ( _value ) != NULL ? 1 : -1 ] ) )
>> +
>>
> Magic ;)
>
>
>>  #ifdef CONFIGURE_RTEMS_INIT_TASKS_TABLE
>>
>>  #include <rtems/confdefs/percpu.h>
>> @@ -94,8 +98,8 @@ extern "C" {
>>  #endif
>>
>>  /*
>> - * Ignore the following warnings from g++ and clang in the static
>> assertion
>> - * below:
>> + * Ignore the following warnings from g++ and clang in the uses of
>> + * _CONFIGURE_ASSERT_NOT_NULL() below:
>>   *
>>   * warning: the address of 'void Init()' will never be NULL [-Waddress]
>>   *
>> @@ -107,13 +111,6 @@ extern "C" {
>>  #pragma GCC diagnostic ignored "-Wpragmas"
>>  #pragma GCC diagnostic ignored "-Wtautological-pointer-compare"
>>
>> -RTEMS_STATIC_ASSERT(
>> -  CONFIGURE_INIT_TASK_ENTRY_POINT != NULL,
>> -  CONFIGURE_INIT_TASK_ENTRY_POINT_MUST_NOT_BE_NULL
>> -);
>> -
>> -#pragma GCC diagnostic pop
>> -
>>  #ifdef CONFIGURE_INIT_TASK_CONSTRUCT_STORAGE_SIZE
>>
>>  #ifdef CONFIGURE_INIT_TASK_STACK_SIZE
>> @@ -139,7 +136,10 @@ const RTEMS_tasks_User_task_config
>> _RTEMS_tasks_User_task_config = {
>>      CONFIGURE_INIT_TASK_INITIAL_MODES,
>>      CONFIGURE_INIT_TASK_ATTRIBUTES,
>>    },
>> -  CONFIGURE_INIT_TASK_ENTRY_POINT,
>> +  _CONFIGURE_ASSERT_NOT_NULL(
>> +    rtems_task_entry,
>> +    CONFIGURE_INIT_TASK_ENTRY_POINT
>> +  ),
>>    CONFIGURE_INIT_TASK_ARGUMENTS
>>  };
>>
>> @@ -167,7 +167,10 @@ const rtems_initialization_tasks_table
>> _RTEMS_tasks_User_task_table = {
>>    CONFIGURE_INIT_TASK_STACK_SIZE,
>>    CONFIGURE_INIT_TASK_PRIORITY,
>>    CONFIGURE_INIT_TASK_ATTRIBUTES,
>> -  CONFIGURE_INIT_TASK_ENTRY_POINT,
>> +  _CONFIGURE_ASSERT_NOT_NULL(
>> +    rtems_task_entry,
>> +    CONFIGURE_INIT_TASK_ENTRY_POINT
>> +  ),
>>    CONFIGURE_INIT_TASK_INITIAL_MODES,
>>    CONFIGURE_INIT_TASK_ARGUMENTS
>>  };
>> @@ -180,6 +183,8 @@ RTEMS_SYSINIT_ITEM(
>>
>>  #endif /* CONFIGURE_INIT_TASK_CONSTRUCT_STORAGE_SIZE */
>>
>> +#pragma GCC diagnostic pop
>> +
>>  #ifdef __cplusplus
>>  }
>>  #endif
>> diff --git a/cpukit/include/rtems/confdefs/wkspace.h
>> b/cpukit/include/rtems/confdefs/wkspace.h
>> index 39014d7f1d..eac4bdb4bd 100644
>> --- a/cpukit/include/rtems/confdefs/wkspace.h
>> +++ b/cpukit/include/rtems/confdefs/wkspace.h
>> @@ -139,8 +139,8 @@ const uintptr_t _Stack_Space_size =
>> _CONFIGURE_STACK_SPACE_SIZE;
>>  #if defined(CONFIGURE_TASK_STACK_ALLOCATOR) \
>>    && defined(CONFIGURE_TASK_STACK_DEALLOCATOR)
>>    /*
>> -   * Ignore the following warnings from g++ and clang in the static
>> assertions
>> -   * below:
>> +   * Ignore the following warnings from g++ and clang in the uses of
>> +   * _CONFIGURE_ASSERT_NOT_NULL() below:
>>     *
>>     * warning: the address of 'f()' will never be NULL [-Waddress]
>>     *
>> @@ -159,13 +159,11 @@ const uintptr_t _Stack_Space_size =
>> _CONFIGURE_STACK_SPACE_SIZE;
>>    #endif
>>
>>    #ifdef CONFIGURE_TASK_STACK_ALLOCATOR_INIT
>> -    RTEMS_STATIC_ASSERT(
>> -      CONFIGURE_TASK_STACK_ALLOCATOR_INIT != NULL,
>> -      CONFIGURE_TASK_STACK_ALLOCATOR_INIT_MUST_NOT_BE_NULL
>> -    );
>> -
>>      const Stack_Allocator_initialize _Stack_Allocator_initialize =
>> -      CONFIGURE_TASK_STACK_ALLOCATOR_INIT;
>> +      _CONFIGURE_ASSERT_NOT_NULL(
>> +        Stack_Allocator_initialize,
>> +        CONFIGURE_TASK_STACK_ALLOCATOR_INIT
>> +      );
>>
>>      RTEMS_SYSINIT_ITEM(
>>        _Stack_Allocator_do_initialize,
>> @@ -174,21 +172,21 @@ const uintptr_t _Stack_Space_size =
>> _CONFIGURE_STACK_SPACE_SIZE;
>>      );
>>    #endif
>>
>> -  RTEMS_STATIC_ASSERT(
>> -    CONFIGURE_TASK_STACK_ALLOCATOR != NULL,
>> -    CONFIGURE_TASK_STACK_ALLOCATOR_MUST_NOT_BE_NULL
>> -  );
>> +  Stack_Allocator_allocate
>> CONFIGURE_TASK_STACK_ALLOCATOR_MUST_NOT_BE_NULL;
>>
>>    const Stack_Allocator_allocate _Stack_Allocator_allocate =
>> -    CONFIGURE_TASK_STACK_ALLOCATOR;
>> +    _CONFIGURE_ASSERT_NOT_NULL(
>> +      Stack_Allocator_allocate,
>> +      CONFIGURE_TASK_STACK_ALLOCATOR
>> +    );
>>
>> -  RTEMS_STATIC_ASSERT(
>> -    CONFIGURE_TASK_STACK_DEALLOCATOR != NULL,
>> -    CONFIGURE_TASK_STACK_DEALLOCATOR_MUST_NOT_BE_NULL
>> -  );
>> +  Stack_Allocator_free CONFIGURE_TASK_STACK_DEALLOCATOR_MUST_NOT_BE_NULL;
>>
>>    const Stack_Allocator_free _Stack_Allocator_free =
>> -    CONFIGURE_TASK_STACK_DEALLOCATOR;
>> +    _CONFIGURE_ASSERT_NOT_NULL(
>> +      Stack_Allocator_free,
>> +      CONFIGURE_TASK_STACK_DEALLOCATOR
>> +    );
>>
>>    #pragma GCC diagnostic pop
>>  #elif defined(CONFIGURE_TASK_STACK_ALLOCATOR) \
>> --
>> 2.26.2
>>
>> _______________________________________________
>> devel mailing list
>> devel at rtems.org
>> http://lists.rtems.org/mailman/listinfo/devel
>>
> _______________________________________________
> devel mailing list
> devel at rtems.org
> http://lists.rtems.org/mailman/listinfo/devel
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.rtems.org/pipermail/devel/attachments/20201211/1a1fff8d/attachment.html>


More information about the devel mailing list