PTR-null-assign-pos
In this section:
Synopsis
A pointer is assigned a value that might be NULL, and then dereferenced.
Enabled by default
No
Severity/Certainty
High/Low

Full description
A pointer is assigned a value that might be NULL, and then dereferenced. Often the source of the potential NULL pointer is a memory allocation function like malloc(), or a sentinel value provided in a user function. This check is identical to CERT-EXP34-C_c.
Coding standards
- CERT EXP34-C
Do not dereference null pointers
- CWE 476
NULL Pointer Dereference
Code examples
The following code example fails the check and will give a warning:
#include <string.h>
char *getenv(const char *name)
{
return strcmp(name, "HOME")==0 ? "/" : NULL;
}
int ex(void)
{
char *p = getenv("USER");
return *p; //p might be NULL
}
The following code example passes the check and will not give a warning about this issue:
#include <stdlib.h>
int main(void)
{
int *p = malloc(sizeof(int));
if (p != 0) {
*p = 4;
}
return (int)p;
}