Start here

Home
About Klocwork
What's new
Fixed issues
Release notes
Installation

Reference

C/C++ checkers
Java checkers
C# checkers
MISRA C 2004 checkers
MISRA C++ 2008 checkers
MISRA C 2012 checkers
MISRA C 2012 checkers with Amendment 1
Commands
Metrics
Troubleshooting
Reference

Product components

C/C++ Integration build analysis
Java Integration build analysis
Desktop analysis
Refactoring
Klocwork Static Code Analysis
Klocwork Code Review
Structure101
Tuning
Custom checkers

Coding environments

Visual Studio
Eclipse for C/C++
Eclipse for Java
IntelliJ IDEA
Other

Administration

Project configuration
Build configuration
Administration
Analysis performance
Server performance
Security/permissions
Licensing
Klocwork Static Code Analysis Web API
Klocwork Code Review Web API

Community

View help online
Visit RogueWave.com
Klocwork Support
Rogue Wave Videos

Legal

Legal information

MISRA.LITERAL.NULL.PTR.CONST.2012

The macro NULL is the only permitted form of integer null pointer constant

MISRA C 2012 Rule 11.9: The macro NULL shall be the only permitted form of integer null pointer constant

Category: Required

Analysis: Decidable, Single Translation Unit

Applies to: C90, C99

Amplification

An integer constant expression with the value 0 shall be derived from expansion of the macro NULL if it appears in any of the following contexts:
  • As the value being assigned to a pointer;
  • As an operand of an == or != operator whose other operand is a pointer;
  • As the second operand of a ?: operator whose third operand is a pointer;
  • As the third operand of a ?: operator whose second operand is a pointer.

Ignoring whitespace and any surrounding parentheses, any such integer constant expression shall represent the entire expansion of NULL.

Note: a null pointer constant of the form (void *)0 is permitted, whether or not it was expanded from NULL.

Rationale

Using NULL rather than 0 makes it clear that a null pointer constant was intended.

Example

In the following example, the initialization of p2 is compliant because the integer constant expression 0 does not appear in one of the contexts prohibited by this rule.

int32_t *p1 = 0;                          /* Non-compliant */
int32_t *p2 = ( void * ) 0;               /* Compliant     */

In the following example, the comparison between p2 and (void *)0 is compliant because the integer constant expression 0 appears as the operand of a cast and not in one of the contexts prohibited by this rule.

#define MY_NULL_1 0 
#define MY_NULL_2 ( void * ) 0

if ( p1 == MY_NULL_1 )       /* Non-compliant */ 
{ 
} 
if ( p2 == MY_NULL_2 )       /* Compliant     */ 
{
}

The following example is compliant because use of the macro NULL provided by the implementation is always permitted, even if it expands to an integer constant expression with value 0.

/* Could also be stdio.h, stdlib.h and others */ 
#include <stddef.h>

extern void f ( uint8_t *p );

/* Compliant for any conforming definition of NULL, such as:
 *    0 
 *    ( void * ) 0 
 *    ( ( ( 0 ) ) ) 
 *    ( ( ( 1 - 1 ) ) ) 
 */ 
f ( NULL );

See also

Rule 11.4