Main Content

MISRA C:2025 Rule 11.11

R2026b

Pointers shall not be implicitly compared to NULL

Since R2026b

Description

Pointers shall not be implicitly compared to NULL.1

Rationale

Control expressions in if, while, do, and for statements, as well as operands of logical operators and the first operand of the conditional operator, require objects that are essentially Boolean type. A When a pointer appears directly in these contexts, the compiler implicitly converts the pointer to a Boolean value. For example, in this code, the pointer ptr is implicitly converted to Boolean:

void foo(char* ptr){
//...
  if(ptr){
    // code...
  }
}
Writing if (ptr != NULL) instead produces an explicitly Boolean result from the comparison operator.

Polyspace Implementation

Polyspace® reports a violation of this rule when a pointer expression is:

  • Converted to bool or a user-defined Boolean type

  • Used as an operand of a logical operator (!, &&, ||)

  • Used as the controlling expression of an if, while, do, or for statement

  • Used as the first operand of a conditional (ternary) operator

To report violation on user-defined Boolean types, specify the types using the option Effective boolean types (-boolean-types).

Troubleshooting

If you expect a rule violation but Polyspace does not report it, see Diagnose Why Coding Standard Violations Do Not Appear as Expected.

Examples

expand all

In this example, pointer expressions appear directly in contexts that expect a Boolean operand.


#include <stdint.h>
#include <stdbool.h>
#include <stddef.h>

typedef unsigned char BOOLEAN;

uint32_t *get_pointer(void);

void implicit_checks(uint32_t *ptr, bool flag) {
    if (ptr) {}                 // Noncompliant
    if (!ptr) {}                // Noncompliant
    while (ptr) {}              // Noncompliant
    if (ptr && flag) {}         // Noncompliant
    ptr ? flag : flag;          // Noncompliant
    if (get_pointer()) {}       // Noncompliant
    BOOLEAN b = (BOOLEAN)ptr;   // Noncompliant
}

void explicit_checks(uint32_t *ptr, bool flag) {
    if (ptr != NULL) {}                 // Compliant
    if (ptr == NULL) {}                 // Compliant
    while (ptr != NULL) {}              // Compliant
    if ((ptr != NULL) && flag) {}       // Compliant
    (ptr != NULL) ? flag : flag;        // Compliant
    if (get_pointer() != NULL) {}       // Compliant
    BOOLEAN b = (BOOLEAN)(ptr != NULL); // Compliant
}

In implicit_checks(), Polyspace reports violations because pointer expressions are used directly where a Boolean value is expected. In explicit_checks(), each pointer is compared against NULL using a relational operator, producing an essentially Boolean result. No violations are reported.

Check Information

Group: Pointer type conversions
Category: Required
AGC Category: Required
PQL Name: std.misra_c_2025.R11_11

Version History

Introduced in R2026b


1 All MISRA coding rules and directives are © Copyright The MISRA Consortium Limited 2021.

The MISRA coding standards referenced in the Polyspace Bug Finder™ documentation are from the following MISRA standards:

  • MISRA C:2004

  • MISRA C:2012

  • MISRA C:2023

  • MISRA C:2025

  • MISRA C++:2008

  • MISRA C++:2023

MISRA and MISRA C are registered trademarks of The MISRA Consortium Limited 2021.