CertC-WIN30

Properly pair allocation and deallocation functions

Required inputs: IR, StaticSemanticAnalysis

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.

Allocator Deallocator
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

In this example, the FormatMessage() function allocates a buffer and stores it in the buf parameter.  From the documentation of FORMAT_MESSAGE_ALLOCATE_BUFFER [ MSDN ]:

The function allocates a buffer large enough to hold the formatted message, and places a pointer to the allocated buffer at the address specified by lpBuffer.  The lpBuffer parameter is a pointer to anL PTSTR; you must cast the pointer to an LPTSTR (for example, (LPTSTR)&lpBuffer). The nSize parameter specifies the minimum number of TCHARs to allocate for an output message buffer. The caller should use the LocalFree function to free the buffer when it is no longer needed.

Instead of freeing the memory using LocalFree() , this code example uses GlobalFree() erroneously.

LPTSTR buf;
DWORD n = FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
                        FORMAT_MESSAGE_FROM_SYSTEM |
                        FORMAT_MESSAGE_IGNORE_INSERTS, 0, GetLastError(),
                        LANG_USER_DEFAULT, (LPTSTR)&buf, 1024, 0);
if (n != 0) {
  /* Format and display the error to the user */

  GlobalFree(buf);
}
Compliant Solution

The compliant solution uses the proper deallocation function as described by the documentation.

LPTSTR buf;
DWORD n = FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
                        FORMAT_MESSAGE_FROM_SYSTEM |
                        FORMAT_MESSAGE_IGNORE_INSERTS, 0, GetLastError(),
                        LANG_USER_DEFAULT, (LPTSTR)&buf, 1024, 0);
if (n != 0) {
  /* Format and display the error to the user */

  LocalFree(buf);
}
Risk Assessment

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

Rule Severity Likelihood Detectable Repairable Priority Level
WIN30-C Low Probable No No P2 L3
Related Guidelines
SEI CERT C++ Coding Standard MEM51-CPP. Properly deallocate dynamically allocated resources
Excerpt from SEI CERT C Coding Standard: Rules for Developing Safe, Reliable, and Secure Systems (2016 Edition) and SEI CERT C Coding Standard [https://cmu-sei.github.io/secure-coding-standards/sei-cert-c-coding-standard/rules/microsoft-windows-win/win30-c], Copyright (C) 1995-2026 Carnegie Mellon University. See 9.4 3rd-Party Licenses in the documentation for full details.

Possible Messages

Key

Text

Severity

Disabled

possible_wrong_release

Resource possibly released using wrong function (allocation used {node0})

None

False

wrong_release

Resource released using wrong function (allocation used {node0})

None

False

Options

maximum_number_of_threads

maximum_number_of_threads : int = 0

This option determines how many threads are used during computation of this rule at most. With 0, the maximum number of threads is automatically determined with a heuristic that considers the number of available CPU cores as well as the available memory.
 

resources

resources : set[str] = {'HeapMemory', 'WindowsCoTaskMemory', 'WindowsGlobalMemory', 'WindowsHeapMemory', 'WindowsLocalMemory', 'WindowsVirtualMemory'}

Set of resources to be checked (selection of rules in the Resources group).
 

witness_paths

witness_paths : bool = True

Whether witness paths should be determined and included in the issue.