C and C++


BADMACRO.NULL : Use of NULL

Summary

A use of the NULL macro.

Some standards forbid the use of NULL in C++ code, and recommend using the nullptr keyword instead.

Properties

Class Name Use of NULL
Significance style
Mnemonic BADMACRO.NULL
Categories
AUTOSARC++14 AUTOSARC++14:A4-10-1 Only nullptr literal shall be used as the null-pointer-constant.
  AUTOSARC++14:M4-10-1 NULL shall not be used as an integer value.
  AUTOSARC++14:M15-1-2 NULL shall not be thrown explicitly.
MisraC++2008 MisraC++2008:4-10-1 NULL shall not be used as an integer value.
  MisraC++2008:15-1-2 NULL shall not be thrown explicitly.
MisraC++2023 MisraC++2023:7.11.1 nullptr shall be the only form of the null-pointer-constant
CWE CWE:1076 Insufficient Adherence to Expected Conventions
JSF++ JSF++:175 A pointer shall not be compared to NULL or be assigned NULL; use plain 0 instead.
Availability Available for C++ only (not C).
Enabling Checks for this warning class are disabled by default. To enable them, add the following WARNING_FILTER rule to the project configuration file.
WARNING_FILTER += allow class="Use of NULL"

Example

#include <stdio.h>

extern void panic(void);

void panic_if_null(char *pA, char *pB) {
    if (pA == NULL)    /* 'Use of NULL' warning issued here */
        panic();
    if (pB == nullptr)           /* ok: uses nullptr keyword */
        panic();
}

Relevant Configuration File Parameters

This class is implemented using a BAD_MACRO_* rule set in the general template configuration file.

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