C and C++


MISC.MEM.RPNT : Read Past Null Terminator

Summary

A call to memcmp() or bcmp() specifies a length parameter that exceeds the length of one or both memory parameters.

When READ_PAST_NTERM_CONSERVATIVE_CHECK=Yes, warnings of this class are only issued if both memory parameters are arrays having essentially char type. This behavior matches the technical definition of Misra2012:21.14.

Properties

Class Name Read Past Null Terminator
Significance reliability
Mnemonic MISC.MEM.RPNT
Categories
MisraC2023 MisraC2023:21.14 The Standard Library function memcmp shall not be used to compare null terminated strings
Misra2012 Misra2012:21.14 The Standard Library function memcmp shall not be used to compare null terminated strings
CWE CWE:1025 Comparison Using Wrong Factors
Availability Available for C and C++.
Enabling Checks for this warning class are enabled by default. To disable them, add the following WARNING_FILTER rule to the project configuration file.
WARNING_FILTER += discard class="Read Past Null Terminator"

Example

#include <string.h>
#include <stdlib.h>

char buffer1[ 12 ];
char buffer2[ 12 ];

int misc_mem_rpnt_charstar ( void ){
    (void) strcpy(buffer1, "abc");
    (void) strcpy(buffer2, "abc");
    if (memcmp ((void *) buffer1,
                (void *) buffer2,
                sizeof(buffer1)) != 0) { /* 'Read Past Null Terminator' warning issued here */
        return 1;
    }
    return memcmp((void *) buffer1,
                  (void *) buffer2,
                  (unsigned int) 3);               /* ok: only comparing up to null terminator */
}

int misc_mem_rpnt_voidstar( void ){
    void * s1 = buffer1;                            /* not an array of essentially char type */
    void * s2 = buffer2;                            /* not an array of essentially char type */
    (void) strcpy(s1, "abc");
    (void) strcpy(s2, "abc");
    if (memcmp(s1,
               s2,
               sizeof(buffer1)) != 0){   /* 'Read Past Null Terminator' warning issued here
                                          * only if READ_PAST_NTERM_CONSERVATIVE_CHECK=No */
        return 1;
    }
    return 0;
}

Relevant Configuration File Parameters

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