Ceedling icon indicating copy to clipboard operation
Ceedling copied to clipboard

sizeof(void) is not defined in ISO C

Open swaldhoer opened this issue 1 year ago • 7 comments

For a function like this

MyFunction(
    void *bla,
) { /* */ }

CMock create a mock like this:

#define MyFunction_ReturnThruPtr_bla(bla) \
    MyFunction_CMockReturnMemThruPtr_bla(__LINE__, bla, sizeof(void))
                                                     /* ^^^^^^^^^^^^^ */

which is not valid ISO C, as sizeof(void) is undefined behavior.

GCC returns 1u for sizeof(void), so maybe it be better to treat that special case like that.

Further, as I have no idea of the CMock, I wonder if the the mock should not use sizeof(void *)? Then of course something like 4u for for 32bit machines would be correct

swaldhoer avatar Jul 25 '23 18:07 swaldhoer

Curious. usually void* gets translated to an unsigned char* internally by CMock. That must have been broken at some point. What version of CMock are you using? Can you past the actual function declaration that is being used? Does it possibly use an alias for void or have other descriptors?

(If it's something you're not comfortable sharing, can you either post something similar or you are welcome to directly message me instead of posting it publicly)

mvandervoord avatar Jul 25 '23 18:07 mvandervoord

Actual function declaration:

DATA_Read4DataBlocks(
    void *pDataToReceiver0,
    void *pDataToReceiver1,
    void *pDataToReceiver2,
    void *pDataToReceiver3);

CMock output :

#define DATA_Read4DataBlocks_ReturnThruPtr_pDataToReceiver0(pDataToReceiver0) \
    DATA_Read4DataBlocks_CMockReturnMemThruPtr_pDataToReceiver0(__LINE__, pDataToReceiver0, sizeof(void))
  • There is no alias for void, however we are using the preprocessor option in the project.yml. Maybe it is related to that?
  • CMock version: I think, the version that is pinned in the 0.31.1 release (https://github.com/ThrowTheSwitch/CMock/tree/3d4ba8d20b8958da5dace7dd5d31155c94b60819). But I need to verify that.

swaldhoer avatar Jul 25 '23 18:07 swaldhoer

Thanks for the details :)

I'll give it a whirl here too... Out of curiosity, does it just seem to be creating this error for the return thru pointer or for expectations as well? Maybe we missed some places that need this protection?

mvandervoord avatar Jul 25 '23 18:07 mvandervoord

Sure!

I need to check that tomorrow on the machine at work. I started to work on getting our test suite compiling with -pedantic and this was the first issue I ran into, so there maybe more to come😅

If I get other findings, I'll share them here too.

swaldhoer avatar Jul 25 '23 18:07 swaldhoer

Version information

ceedling version
Welcome to Ceedling!
    Ceedling:: 0.31.1
       CMock:: 2.5.4
       Unity:: 2.5.4
  CException:: 1.3.3

At least in our code, the problem only occurs for *ReturnThruPtr* mocks:

Examples

Function declaration:

extern STD_RETURN_TYPE_e DATA_Read4DataBlocks(
    void *pDataToReceiver0,
    void *pDataToReceiver1,
    void *pDataToReceiver2,
    void *pDataToReceiver3);

Mocks:

#define DATA_Read4DataBlocks_ReturnThruPtr_pDataToReceiver0(pDataToReceiver0) \
    DATA_Read4DataBlocks_CMockReturnMemThruPtr_pDataToReceiver0(__LINE__, pDataToReceiver0, sizeof(void))
#define DATA_Read4DataBlocks_ReturnArrayThruPtr_pDataToReceiver0(pDataToReceiver0, cmock_len) \
    DATA_Read4DataBlocks_CMockReturnMemThruPtr_pDataToReceiver0(                              \
        __LINE__, pDataToReceiver0, cmock_len * sizeof(*pDataToReceiver0))
#define DATA_Read4DataBlocks_ReturnMemThruPtr_pDataToReceiver0(pDataToReceiver0, cmock_size) \
    DATA_Read4DataBlocks_CMockReturnMemThruPtr_pDataToReceiver0(__LINE__, pDataToReceiver0, cmock_size)

Function declaration:

extern void FTSK_CreateTaskCyclic1ms(void *const pvParameters);

Mocks:

#define FTSK_CreateTaskCyclic1ms_ReturnThruPtr_pvParameters(pvParameters) \
    FTSK_CreateTaskCyclic1ms_CMockReturnMemThruPtr_pvParameters(__LINE__, pvParameters, sizeof(void))
#define FTSK_CreateTaskCyclic1ms_ReturnArrayThruPtr_pvParameters(pvParameters, cmock_len) \
    FTSK_CreateTaskCyclic1ms_CMockReturnMemThruPtr_pvParameters(                          \
        __LINE__, pvParameters, cmock_len * sizeof(*pvParameters))
#define FTSK_CreateTaskCyclic1ms_ReturnMemThruPtr_pvParameters(pvParameters, cmock_size) \
    FTSK_CreateTaskCyclic1ms_CMockReturnMemThruPtr_pvParameters(__LINE__, pvParameters, cmock_size)

swaldhoer avatar Jul 26 '23 09:07 swaldhoer

Thanks. That's what I'm seeing too. :)

mvandervoord avatar Jul 26 '23 11:07 mvandervoord

The following issue seems to be related: https://github.com/ThrowTheSwitch/CMock/issues/319

It seems that the issue (source) might be fixed by changing back to the "old" implementation or adding a check if the argument type is void. Maybe it would be an idea to always use the parameter type in the sizeof expression unless the type is void*?

@mvandervoord If you like then I can prepare a possible? fix.

Hannes103 avatar Jul 31 '23 08:07 Hannes103