C and C++

C/C++ Allocators and Deallocators

The CodeSonar library models include models for allocators and deallocators from standard system libraries. These models support checks for warning classes such as Leak and Dynamic Allocation After Initialization.

If you are writing a model for an allocator (deallocator) function, write the model in terms of the already-modeled function that is most similar. This will ensure that CodeSonar correctly treats the function as an allocator (deallocator) and includes it in the appropriate checks.

If you are adding a new check related to dynamic allocation or deallocation, you can use csonar_replace_*() functions to intercept calls to the relevant allocators or deallocators. Note that malloc() and free() are treated specially by CodeSonar: a check applied to malloc() will be applied to all allocators and one applied to free() will be applied to all deallocators.

Allocators

CodeSonar ships with library models that allow it to recognize a large number of allocation functions across many different libraries. Some examples are listed below.

Allocators recognized include...
C++ operators operator new()
FreeRTOS pvPortMalloc(), xQueueCreateMutex()
gcc Builtins __builtin_strndup()
LDAP ldap_search_ext(), ldap_search_ext_s()
libc calloc(), fdopen(), malloc()
Linux Kernel get_free_page(), kmalloc()
Mac OS X _MALLOC(), kalloc()
Netscape Portable Runtime (NSPR) PR_CreateThread(), PR_QueueJob()
Nucleus DMCE_Allocate_Memory(), PMCE_Create_Partition_Pool()
OpenSSL CRYPTO_malloc(), CRYPTO_strdup()
Qt qMalloc(), qReallocAligned()
VxWorks memPartAlignedAlloc(), msgQCreate()
Win32 LocalAlloc(), RtlCreateHeap(), freopen_s()

Deallocators

CodeSonar ships with library models that allow it to recognizea large number of deallocation functions across many different libraries. Some examples are listed below.

Deallocators recognized include...
Apache Portable Runtime (APR) apr_pool_destroy()
C++ operators operator delete()
FreeRTOS vQueueDelete()
LDAP ldap_msgfree()
libc free(), pclose()
Linux Kernel free_page(), kfree()
Mac OS X _FREE(), zfree()
Nucleus DMCE_Deallocate_Memory(), PMCE_Delete_Partition_Pool()
OpenSSL CRYPTO_free(), CRYPTO_remalloc()
Qt qFree(), qReallocAligned()
VxWorks lstFree(), semDelete()
Win32 FreeLibrary(), _aligned_free()