C and C++


LANG.TYPE.IARGT.MEMCMP : Inappropriate Argument to memcmp

Summary

A call to memcmp() where the type of one or both pointer arguments is not "pointer to X" with X one of the following.

See MISRA Checks: Essential Type Category for more information.

Properties

Class Name Inappropriate Argument to memcmp
Significance style
Mnemonic LANG.TYPE.IARGT.MEMCMP
Categories
MisraC2023 MisraC2023:21.16 The pointer arguments to the Standard Library function memcmp shall point to either a pointer type, an essentially signed type, an essentially unsigned type, an essentially Boolean type or an essentially enum type
Misra2012 Misra2012:21.16 The pointer arguments to the Standard Library function memcmp shall point to either a pointer type, an essentially signed type, an essentially unsigned type, an essentially Boolean type or an essentially enum type
Availability Available for C and 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="Inappropriate Argument to memcmp"
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

#include <string.h>

typedef struct { int color; } S;

int lang_type_iargt_bad(const float *f1, const float *f2,
                        S *s1,  S *s2, size_t sn,
                        const void *v1, const void *v2, size_t n
                       ){
    if (!memcmp(f1, f2, sizeof(float))){ /* 'Inappropriate Argument to memcmp' warning issued here
                                          * - pointed-to type for f1 and f2 is 'float', which
                                          *   is not one of the types permitted for this warning class.
                                          * - There are two warning instances (one for each pointer
                                          *   argument), in the same warning group.
                                          */
        return 0;
    }
    if (!memcmp(s1, s2, sn)){            /* 'Inappropriate Argument to memcmp' warning issued here
                                          * - pointed-to type for s1 and s2 is 'S' (a struct type)
                                          * - There are two warning instances, in the same warning group.
                                          */
        return 0;
    }
    if (!memcmp(v1, v2, n)) {            /* 'Inappropriate Argument to memcmp' warning issued here
                                          * -pointed-to type for v1 and v2 is 'void'
                                          * - There are two warning instances, in the same warning group.
                                          */
        return 0;
    }
    return 1;
}

int lang_type_iargt_ok(int *i1, int *i2, size_t sz){
    if (!memcmp(i1, i2, sizeof(int))){ return 0; } /* ok: pointed-to type for i1 and i2 is 'int',
                                                    * which is an essentially signed type */
    if (!memcmp(&i1, &sz, sz)){ return 0; }        /* ok: pointed-to types are as follows
                                                    * - &i1: int* (pointer type)
                                                    * - &sz: size_t (essentially unsigned)
                                                   */
    return 1;
}

Relevant Configuration File Parameters

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