CWE Rule 476
Description
Rule Description
A NULL pointer dereference occurs when the application dereferences a pointer that it expects to be valid, but is NULL, typically causing a crash or exit.
Polyspace Implementation
The rule checker checks for Dereference of a null pointer.
Examples
Dereference of a null pointer
This issue occurs when you use a pointer with a value of NULL
as
if it points to a valid memory location. If you dereference the zero address, such as
0x00, Polyspace® considers the null address as equivalent to NULL
and
raises this defect.
Dereferencing a null pointer is undefined behavior. In most implementations, the dereference can cause your program to crash.
Check a pointer for NULL
before dereference.
If the issue occurs despite an earlier check
for NULL
, look for intermediate events between the check and the
subsequent dereference. Often the result details (or source code tooltips in
Polyspace as You Code) show a sequence of events that led to the defect. You can
implement the fix on any event in the sequence. If the result details do not show
this event history, you can search for previous references of variables relevant to
the defect using right-click options in the source code and find related events. See
also Interpret Bug Finder Results in Polyspace Desktop User Interface or Interpret Bug Finder Results in Polyspace Access Web Interface (Polyspace Access).
See examples of fixes below.
A default Bug Finder analysis might not raise this defect when the input values are unknown and only a subset of inputs cause an issue. To check for defects caused by specific system input values, run a stricter Bug Finder analysis. See Extend Bug Finder Checkers to Find Defects from Specific System Input Values.
#include <stdlib.h> int FindMax(int *arr, int Size) { int* p=NULL; *p=arr[0]; //Noncompliant /* Defect: Null pointer dereference */ for(int i=0;i<Size;i++) { if(arr[i] > (*p)) *p=arr[i]; } return *p; }
The pointer p
is initialized
with value of NULL
. However, when the value arr[0]
is
written to *p
, p
is assumed
to point to a valid memory location.
One possible correction is to initialize p
with
a valid memory address before dereference.
#include <stdlib.h> int FindMax(int *arr, int Size) { /* Fix: Assign address to null pointer */ int* p=&arr[0]; for(int i=0;i<Size;i++) { if(arr[i] > (*p)) *p=arr[i]; } return *p; }
Check Information
Category: Pointer Issues |
Version History
Introduced in R2023a
See Also
External Websites
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)