Our website uses cookies to enhance your browsing experience.
Accept
to the top
>
>
>
V2644. MISRA. Controlling expression...
menu mobile close menu
Additional information
toggle menu Contents

V2644. MISRA. Controlling expression of generic selection must not have side effects.

Jul 09 2025

This diagnostic rule is based on the MISRA (Motor Industry Software Reliability Association) software development guidelines.

This diagnostic rule is relevant only for C.

The controlling expression of the _Generic (C11) construct shall not contain any side effects if the generic selection expression is not obtained via macro expansion.

This restriction exists because the controlling expression is never evaluated. Therefore, any side effects in the expression do not change the program state.

The side effects include:

  • modifications of any variable (any increment/decrement or assignment);
  • any function calls.

The example:

#if SOME_CONDITION
#define DATA_TYPE int
#else
#define DATA_TYPE unsigned
#endif // SOME_CONDITION

typedef struct CustomType
{
  DATA_TYPE data;
} *const CustomCPTR;

void InitializeData(const void *ptr)
{
  CustomCPTR customData = (CustomCPTR)(ptr);

  _Bool isOk = (_Generic(customData->data = 0
               , unsigned int : 1
               , default: 0));

  if (!isOk) return;
  // Do something
}

The code contains a function that shall initialize a member of a structure. It starts with checking whether the data member has the expected type. The initialization of the data member is executed along with the check. Regardless of the scenario, it is expected that the data member is initialized to zero. However, the initialization does not occur because the customData->data = 0 expression appears in an unevaluated context.

The fix:

void InitializeData(const void *ptr) {
  CustomCPTR customData = (CustomCPTR)(ptr);
  customData->data = 0;

  _Bool isOk = (_Generic(customData->data
               , unsigned int : 1
               , default: 0));
  if (!isOk) return;
  // Do something
}

This diagnostic is classified as:

  • MISRA-C-2023-23.2