Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

This noncompliant code example is a real-world example taken from a vulnerable version of the libpng library as deployed on a popular ARM-based cell phone [Jack 2007].  The  The libpng implements its own wrapper to malloc() that returns a null pointer on error or on being passed a 0 byte length argument.

Code Block
bgColor#FFCCCC
langc
png_charp chunkdata;
chunkdata = (png_charp)png_malloc(png_ptr, length + 1);

If a length field of -1 −1 is supplied to the code in this noncompliant example, the addition wraps around to 0, and png_malloc() subsequently returns a null pointer, which is assigned to chunkdata.  The  The chunkdata pointer is later used as a destination argument in a call to memcpy(), resulting in user-defined data overwriting memory starting at address 0.  A A write from , or read to , the memory address 0x0 will generally reference invalid or unused memory. In the case of the ARM and XScale architectures, the 0x0 address is mapped in memory and serves as the Exception Vector Table.

...

To correct this error, ensure the pointer returned by malloc() is not null. This also ensures compliance with rule MEM32-C. Detect and handle memory allocation errors.

...

To correct this error, ensure the pointer returned by malloc() is not null. This also ensures compliance with rule MEM32-C. Detect and handle memory allocation errors.

...

Rule

Severity

Likelihood

Remediation Cost

Priority

Level

EXP34-C

high

likely

medium

P18

L1

Automated Detection

sectionFully Implementedimplemented.

Fortify SCA

V. 5.0

can

Can detect violations of this rule. In particular,

Rose

ROSE ensures that any pointer returned by malloc(), calloc(), or realloc() is first checked for NULL before being used (otherwise, it is free()-d).

Rose

ROSE does not handle cases where an allocation is assigned to an lvalue that is not a variable (such as a struct member or C++ function call returning a reference).

)section

finds

Finds instances where a pointer is checked against NULL and then later dereferenced.

Coverity Prevent

ToolVersionCheckerDescription

LDRA tool suite

Include Page
LDRA_V
LDRA_V
section

45 D

Section
Section
Section
  section

Splint

Include Page
Splint_V
Splint_V
  section
Compass/ROSE  
Section

Coverity Prevent

 

Include Page
Coverity_V
Coverity_V
section

CHECKED_RETURN

Section
Section
 
Include Page
Coverity_V
Coverity_V
 section

NULL_RETURNS

section
 

identifies

Identifies functions that can return a null pointer but are not checked.

 section

Coverity Prevent

 
Include Page
Coverity_V
Coverity_V
 section

REVERSE_INULL

section
 

identifies

Identifies code that dereferences a pointer and then checks the pointer against NULL.

 section

Coverity Prevent

 
Include Page
Coverity_V
Coverity_V
 section

FORWARD_NULL

section
 

can

Can find the instances where NULL is explicitly dereferenced or a pointer is checked

against null

against NULL but then dereferenced anyway.


Coverity Prevent cannot discover all violations of this rule, so further verification is necessary.

 section

Klocwork

 
Include Page
Klocwork_V
Klocwork_V
 section

NPD.* *RNPD.*

 

 

Related Vulnerabilities

Search for vulnerabilities resulting from the violation of this rule on the CERT website.

...

The CERT Oracle Secure Coding Standard for Java: EXP01-J. Never dereference null pointers

ISO/IEC 9899:19992011 Section 6.3.2.3, "Pointers"

ISO/IEC TR 17961 (Draft) Dereferencing an out-of-domain pointer [nullref]

ISO/IEC TR 24772 "HFC Pointer casting and pointer type changes" and "XYH Null Pointer Dereference"

...