Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Added Polyspace Bug Finder

Windows provides several APIs for allocating memory.  While some of these functions have converged over time, it is still important to always properly pair allocations and deallocations.  The following table shows the proper pairings.

AllocatorDeallocator
malloc()free()
realloc()free()
LocalAlloc()LocalFree()
LocalReAlloc() LocalFree()
GlobalAlloc()GlobalFree()
GlobalReAlloc()GlobalFree()
VirtualAlloc()VirtualFree()
VirtualAllocEx()VirtualFreeEx()
VirtualAllocExNuma()VirtualFreeEx()
AllocateUserPhysicalPages()FreeUserPhysicalPages()
AllocateUserPhysicalPagesNuma()FreeUserPhysicalPages()
HeapAlloc()HeapFree()
HeapReAlloc()HeapFree()

Noncompliant Code Example

...

Mixing allocation and deallocation functions can lead to memory corruption issues, or result in accessing out-of-bounds memory.

Rule

Severity

Likelihood

Remediation Cost

Priority

Level

WIN30-C

Low

Probable

Low

P6

L2

Automated Detection

Tool

Version

Checker

Description

CodeSonar
Include Page
CodeSonar_V
CodeSonar_V
ALLOC.TMType mismatch
Coverity
Include Page
Coverity_V
Coverity_V

ALLOC_FREE_MISMATCH (needs improvement)

Partially implemented; needs improvement
Klocwork
Include Page
Klocwork_V
Klocwork_V
FMM.MIGHT
FMM.MUST
 

Parasoft C/C++test
Include Page
c:
Parasoft_V
c:
Parasoft_V
BD-RES-LEAKS
 

Polyspace Bug Finder

R2017bMismatched alloc/dealloc functions on WindowsImproper deallocation function causes memory corruption issues

Related Vulnerabilities

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

Related Guidelines

...


...