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

NPD.CHECK.MUST

Previously checked null pointer is dereferenced

An attempt to access data using a null pointer causes a runtime error. When a program dereferences a pointer that is expected to be valid but turns out to be null, a null pointer dereference occurs. Null-pointer dereference defects often occur due to ineffective error handling or race conditions, and typically cause abnormal program termination. Before a pointer is dereferenced in C/C++ code, it must be checked to confirm that it is not equal to null.

The NPD checkers look for instances in which a null or possibly null pointer is dereferenced.

The NPD.CHECK.MUST checker flags situations in which a pointer that's been checked for a null value is subsequently dereferenced explicitly or through a function call without being checked for null.

Vulnerability and risk

Null-pointer dereferences usually result in the failure of the process. These issues typically occur due to ineffective exception handling.

Mitigation and prevention

To avoid this vulnerability:

  • Check for a null value in the results of all functions that return values
  • Make sure all external inputs are validated
  • Explicitly initialize variables
  • Make sure that unusual exceptions are handled correctly

Vulnerable code example

1  void npd_check_must() {
2    char *p = getSomeValue();
3    if (p != NULL) { }
4    p[0] = 0;
5  }

Although *p is checked for null at line 3, it's explicitly dereferenced at line 4 without being checked for null. (This example makes the defect obvious, but in more typical application code, the dereference would be harder to spot.) This type of vulnerability can produce unexpected and unintended results.

Fixed code example

1  void npd_check_must() {
2    char *p = getSomeValue();
3    if (p != NULL) {p[0] = 0;}
4  }

In the fixed version, the dereference is dependent on the check for null.

Extension

This checker can be extended through the Klocwork knowledge base. See Tuning C/C++ analysis for more information.