C and C++


LANG.TYPE.CGEN.IMPTC : Implicit Pointer Type Conversion in Selection of C Generic

Summary

The default association for a C generic selection expression is selected, but a human reader might expect a different outcome if they (mistakenly) believe that implicit pointer conversion will take place.

The controlling expression of a C generic selection undergoes lvalue conversion, but does not undergo implicit pointer conversion. Once lvalue conversion has taken place, the controlling expression type must exactly match an association type in the association list in order for that association to be selected: otherwise, the default association is selected.

Properties

Class Name Implicit Pointer Type Conversion in Selection of C Generic
Significance style
Mnemonic LANG.TYPE.CGEN.IMPTC
Categories
MisraC2023 MisraC2023:23.5 A generic selection should not depend on implicit pointer type conversion
Misra2012 Misra2012:23.5 A generic selection should not depend on implicit pointer type conversion
Availability Available for C only (not C++).
Enabling Checks for this warning class are disabled by default, and require the unnormalized C ASTs for the project. To enable them, add the following WARNING_FILTER rule and RETAIN_UNNORMALIZED_C_AST specification to the project configuration file.
RETAIN_UNNORMALIZED_C_AST = Yes
WARNING_FILTER += allow class="Implicit Pointer Type Conversion in Selection of C Generic"
Note that retaining the unnormalized ASTs will increase the disk space used to store the project representation, and may make the analysis take longer.

Example

#define QUALINT_PTRS(y) (_Generic(y,                \
                                  const int*: 0,    \
                                  volatile int*: 1, \
                                  default: -1))

#define INT_PTR(y) (_Generic(y,          \
                             int*: 0,    \
                             default: -1))

int lang_type_cgen_imptc(int *pi, void *pv, float *pf){
    int rv = 0;
    rv += QUALINT_PTRS(pi); /* 'Implicit Pointer Type Conversion in Selection of C Generic' warning issued here
                             * - selection type int* does not match any non-default association type {const int*, volatile int*},
                             *   but author/reader might have expected otherwise.
                             */
    rv += INT_PTR(pi);                        /* ok: selection type int* matches int* association type */
    rv += INT_PTR(pv);      /* 'Implicit Pointer Type Conversion in Selection of C Generic' warning issued here
                             * - selection type int* does not match any non-default association type {void*},
                             *   but author/reader might have expected otherwise.
                             */
    rv += INT_PTR(pf);                        /* ok: selection type int* does not match non-default association type float*, but
                                               * this is not likely to reflect (or cause) confusion about implicit conversion.
                                               */
    return rv;
}

Relevant Configuration File Parameters

The following configuration file parameters affect checks for this warning class.