Skip to main content

IAR Embedded Workbench for RISC-V 3.40

MISRAC2012-Rule-21.16

In this section:
Synopsis

(Required) 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

Enabled by default

Yes

Severity/Certainty

Medium/Medium

mediummedium.png
Full description

The types of the pointer arguments to memcmp are not essentially signed, unsigned, Boolean or enum type.

Coding standards
MISRA C:2012 Rule-21.16

(Required) 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

Code examples

The following code example fails the check and will give a warning:

struct S;
/*
 * Return value may indicate that 's1' and 's2' are different due to padding.
 */
bool_t f(struct S *s1, struct S *s )
{
  return (memcmp(s1, s2, sizeof(struct S)) != 0);           /* Non-compliant */
}

union U {
  uint32_t range;
  uint32_t height;
};

/*
 * Return value may indicate that 'u1' and 'u2' are the same
 * due to unintentional comparison of 'range' and 'height'. 
 */
bool_t f2(union U *u1, union U *u2)
{
  return (memcmp(u1, u2, sizeof(union U)) != 0 );           /* Non-compliant */
}

const char a[ 6 ] = "task";

/*
 * Return value may incorrectly indicate strings are different as the
 * length of 'a' (4) is less than the number of bytes compared (6).
 */

bool_t f3(const char b[6])
{
  return (memcmp(a, b, 6) != 0 );                           /* Non-compliant */
}

The following code example passes the check and will not give a warning about this issue:

#include <stdbool.h>
#include <string.h>

bool_t f(float* *fp1, float* *fp2)
{
  return (memcmp(fp1, fp2, sizeof(float*)) != 0);
}