Unicorn with delicious cookie
Nous utilisons des cookies pour améliorer votre expérience de navigation. En savoir plus
Accepter
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.

04 Avr 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

Remplissez le formulaire ci‑dessous en 2 étapes simples :

Vos coordonnées :

Étape 1
Félicitations ! Voici votre code promo !

Type de licence souhaité :

Étape 2
Team license
Enterprise licence
** En cliquant sur ce bouton, vous déclarez accepter notre politique de confidentialité
close form
Demandez des tarifs
Nouvelle licence
Renouvellement de licence
--Sélectionnez la devise--
USD
EUR
* En cliquant sur ce bouton, vous déclarez accepter notre politique de confidentialité

close form
La licence PVS‑Studio gratuit pour les spécialistes Microsoft MVP
close form
Pour obtenir la licence de votre projet open source, s’il vous plait rempliez ce formulaire
* En cliquant sur ce bouton, vous déclarez accepter notre politique de confidentialité

close form
I want to join the test
* En cliquant sur ce bouton, vous déclarez accepter notre politique de confidentialité

close form
check circle
Votre message a été envoyé.

Nous vous répondrons à


Si l'e-mail n'apparaît pas dans votre boîte de réception, recherchez-le dans l'un des dossiers suivants:

  • Promotion
  • Notifications
  • Spam