C and C++


MISC.MEM.RPNT : Read Past Null Terminator

要旨

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.

プロパティ

クラス名 Read Past Null Terminator
日本語クラス名 Read Past Null Terminator
クラス分類 信頼性 (reliability)
ニーモニック MISC.MEM.RPNT
カテゴリー
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
対応言語 C および C++ で利用可能です。
有効/無効設定 このワーニングクラスのチェックはデフォルトで有効になっています。チェックを無効にするにはプロジェクト設定ファイル (configuration file)に以下の WARNING_FILTER ルールを追加してください。
WARNING_FILTER += discard class="Read Past Null Terminator"

#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;
}

関連のある設定ファイルパラメータ

設定ファイルの以下のパラメータがこのワーニングクラスのチェックに影響します。