Unicorn with delicious cookie
Our website uses cookies to enhance your browsing experience.
Accept
to the top
>
>
>
V2638. MISRA. Generic association shoul…
menu mobile close menu
Analyzer diagnostics
General Analysis (C++)
General Analysis (C#)
General Analysis (Java)
Micro-Optimizations (C++)
Diagnosis of 64-bit errors (Viva64, C++)
Customer specific requests (C++)
MISRA errors
AUTOSAR errors
OWASP errors (C++)
OWASP errors (C#)
OWASP errors (Java)
Problems related to code analyzer
Additional information
toggle menu Contents

V2638. MISRA. Generic association should list an appropriate type.

Apr 04 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 _Generic construct (C11) used with certain types from the association list can lead to unexpected results.

Before a controlling expression is matched to the association list, it undergoes a chain of implicit conversions (lvalue conversion):

  • const / volatile / restrict top-level qualifiers are discarded;
  • type atomicity is discarded;
  • an array is converted to a pointer;
  • a function is converted to a function pointer.

The C standard does not impose any restrictions on the types specified in the association list, therefore a certain branch may never be selected. So, the association list must not contain any of the following types:

  • a const / volatile / restrict qualified type;
  • an atomic type;
  • an array;
  • a function;
  • an unnamed structure or union (can only be handled by the default association).

The example:

#define builtin_typename(expr)                      \
  (_Generic( (expr)                                 \
     , char: "char"                                 \
     , const char: "const char"                     \
     , volatile char: "volatile char"               \
     , const volatile char: "const volatile char"   \
     , short: "short"                               \
     , const short: "const short"                   \
     , volatile short: "volatile short"             \
     , const volatile short: "const volatile short" \
     , ....) )

In the code, the builtin_typename macro is declared, converting the passed expression to a string literal that contains the expression type. To do this, all built-in types with combinations of const and volatile qualifiers are enumerated. However, the controlling expression will never be cv-qualified after implicit conversions. As a result, such a _Generic construct includes associations that will never be selected.

The fixed code:

#define builtin_typename(expr) \
  (_Generic( (expr) \
     , char: "char" \
     , short: "short" \
     , ....) )

This diagnostic is classified as:

  • MISRA-C-23.4
close form

Fill out the form in 2 simple steps below:

Your contact information:

Step 1
Congratulations! This is your promo code!

Desired license type:

Step 2
Team license
Enterprise license
** By clicking this button you agree to our Privacy Policy statement
close form
Request our prices
New License
License Renewal
--Select currency--
USD
EUR
* By clicking this button you agree to our Privacy Policy statement

close form
Free PVS‑Studio license for Microsoft MVP specialists
* By clicking this button you agree to our Privacy Policy statement

close form
To get the licence for your open-source project, please fill out this form
* By clicking this button you agree to our Privacy Policy statement

close form
I want to join the test
* By clicking this button you agree to our Privacy Policy statement

close form
check circle
Message submitted.

Your message has been sent. We will email you at


If you do not see the email in your inbox, please check if it is filtered to one of the following folders:

  • Promotion
  • Updates
  • Spam