JavaScript is not currently enabled, but is required for full CodeSonar manual search and browse functionality.

If you are viewing this file in your hub's Web GUI, enable JavaScript in your browser: you will also need it for GUI functionality.

If you opened this file directly from disk, your browser may be directly suppressing JavaScript functionality: certain browsers perform this suppression on local files (but not files delivered by web servers) for security reasons.

CodeSonar® 9.2p0 CONFIDENTIAL CodeSecure Inc
C and C++
Binaries


ALLOC.TM : Type Mismatch

Summary

A function that is supposed to use or release a resource is called with an argument that was previously allocated or used with a contradictory type.

Warning Class Name Notes
Type Mismatch An argument was previously allocated or used with a contradictory type.
Pool Mismatch A function is passed a pointer argument that should belong to the same heap as its heap handle argument but does not.
LocalLock on LMEM_FIXED Memory
LocalHandle on LMEM_FIXED Memory
LocalUnlock on LMEM_FIXED Memory
An attempt to lock, unlock, or acquire a handle for a local memory object that was allocated with LMEM_FIXED (and thus always has a lock count of 0).
GlobalLock on GMEM_FIXED Memory
GlobalHandle on GMEM_FIXED Memory
GlobalUnlock on GMEM_FIXED Memory
An attempt to lock, unlock, or acquire a handle for a global memory object that was allocated with GMEM_FIXED (and thus always has a lock count of 0).

Warning Classes

CodeSonar performs checks for the following type mismatch warning classes.

Properties

Multiple warning classes share this mnemonic.

Checks for all these warning classes are enabled by default. To disable them all at once, add the following WARNING_FILTER rule to the project configuration file.

WARNING_FILTER += discard categories:ALLOC.TM
Class Name GlobalHandle on GMEM_FIXED Memory
Significance reliability
Mnemonic ALLOC.TM
Categories
AUTOSARC++14 AUTOSARC++14:A18-5-5 Memory management functions shall ensure the following: (a) deterministic behavior resulting with the existence of worst-case execution time, (b) avoiding memory fragmentation, (c) avoid running out of memory, (d) avoiding mismatched allocations or deallocations, (e) no dependence on non-deterministic calls to kernel.
MisraC++2023 MisraC++2023:0.3.2 A function call shall not violate the function's preconditions
CWE CWE:686 Function Call With Incorrect Argument Type
  CWE:843 Access of Resource Using Incompatible Type ('Type Confusion')
CERT-C CERT-C:API07-C Enforce type safety
OWASP-2021 OWASP-2021:A4 Insecure design
OWASP-2025 OWASP-2025:A06 Insecure Design
Availability Available for C and C++.
Enabling Checks for this warning class are enabled by default. To disable them, add the following WARNING_FILTER rule to the project configuration file.
WARNING_FILTER += discard class="GlobalHandle on GMEM_FIXED Memory"
Class Name GlobalLock on GMEM_FIXED Memory
Significance reliability
Mnemonic ALLOC.TM
Categories
AUTOSARC++14 AUTOSARC++14:A18-5-5 Memory management functions shall ensure the following: (a) deterministic behavior resulting with the existence of worst-case execution time, (b) avoiding memory fragmentation, (c) avoid running out of memory, (d) avoiding mismatched allocations or deallocations, (e) no dependence on non-deterministic calls to kernel.
MisraC++2023 MisraC++2023:0.3.2 A function call shall not violate the function's preconditions
CWE CWE:686 Function Call With Incorrect Argument Type
  CWE:843 Access of Resource Using Incompatible Type ('Type Confusion')
CERT-C CERT-C:API07-C Enforce type safety
OWASP-2021 OWASP-2021:A4 Insecure design
OWASP-2025 OWASP-2025:A06 Insecure Design
Availability Available for C and C++.
Enabling Checks for this warning class are enabled by default. To disable them, add the following WARNING_FILTER rule to the project configuration file.
WARNING_FILTER += discard class="GlobalLock on GMEM_FIXED Memory"
Class Name GlobalUnlock on GMEM_FIXED Memory
Significance reliability
Mnemonic ALLOC.TM
Categories
AUTOSARC++14 AUTOSARC++14:A18-5-5 Memory management functions shall ensure the following: (a) deterministic behavior resulting with the existence of worst-case execution time, (b) avoiding memory fragmentation, (c) avoid running out of memory, (d) avoiding mismatched allocations or deallocations, (e) no dependence on non-deterministic calls to kernel.
MisraC++2023 MisraC++2023:0.3.2 A function call shall not violate the function's preconditions
CWE CWE:686 Function Call With Incorrect Argument Type
  CWE:843 Access of Resource Using Incompatible Type ('Type Confusion')
CERT-C CERT-C:API07-C Enforce type safety
OWASP-2021 OWASP-2021:A4 Insecure design
OWASP-2025 OWASP-2025:A06 Insecure Design
Availability Available for C and C++.
Enabling Checks for this warning class are enabled by default. To disable them, add the following WARNING_FILTER rule to the project configuration file.
WARNING_FILTER += discard class="GlobalUnlock on GMEM_FIXED Memory"
Class Name LocalHandle on LMEM_FIXED Memory
Significance reliability
Mnemonic ALLOC.TM
Categories
AUTOSARC++14 AUTOSARC++14:A18-5-5 Memory management functions shall ensure the following: (a) deterministic behavior resulting with the existence of worst-case execution time, (b) avoiding memory fragmentation, (c) avoid running out of memory, (d) avoiding mismatched allocations or deallocations, (e) no dependence on non-deterministic calls to kernel.
MisraC++2023 MisraC++2023:0.3.2 A function call shall not violate the function's preconditions
CWE CWE:686 Function Call With Incorrect Argument Type
  CWE:843 Access of Resource Using Incompatible Type ('Type Confusion')
CERT-C CERT-C:API07-C Enforce type safety
OWASP-2021 OWASP-2021:A4 Insecure design
OWASP-2025 OWASP-2025:A06 Insecure Design
Availability Available for C and C++.
Enabling Checks for this warning class are enabled by default. To disable them, add the following WARNING_FILTER rule to the project configuration file.
WARNING_FILTER += discard class="LocalHandle on LMEM_FIXED Memory"
Class Name LocalLock on LMEM_FIXED Memory
Significance reliability
Mnemonic ALLOC.TM
Categories
AUTOSARC++14 AUTOSARC++14:A18-5-5 Memory management functions shall ensure the following: (a) deterministic behavior resulting with the existence of worst-case execution time, (b) avoiding memory fragmentation, (c) avoid running out of memory, (d) avoiding mismatched allocations or deallocations, (e) no dependence on non-deterministic calls to kernel.
MisraC++2023 MisraC++2023:0.3.2 A function call shall not violate the function's preconditions
CWE CWE:686 Function Call With Incorrect Argument Type
  CWE:843 Access of Resource Using Incompatible Type ('Type Confusion')
CERT-C CERT-C:API07-C Enforce type safety
OWASP-2021 OWASP-2021:A4 Insecure design
OWASP-2025 OWASP-2025:A06 Insecure Design
Availability Available for C and C++.
Enabling Checks for this warning class are enabled by default. To disable them, add the following WARNING_FILTER rule to the project configuration file.
WARNING_FILTER += discard class="LocalLock on LMEM_FIXED Memory"
Class Name LocalUnlock on LMEM_FIXED Memory
Significance reliability
Mnemonic ALLOC.TM
Categories
AUTOSARC++14 AUTOSARC++14:A18-5-5 Memory management functions shall ensure the following: (a) deterministic behavior resulting with the existence of worst-case execution time, (b) avoiding memory fragmentation, (c) avoid running out of memory, (d) avoiding mismatched allocations or deallocations, (e) no dependence on non-deterministic calls to kernel.
MisraC++2023 MisraC++2023:0.3.2 A function call shall not violate the function's preconditions
CWE CWE:686 Function Call With Incorrect Argument Type
  CWE:843 Access of Resource Using Incompatible Type ('Type Confusion')
CERT-C CERT-C:API07-C Enforce type safety
OWASP-2021 OWASP-2021:A4 Insecure design
OWASP-2025 OWASP-2025:A06 Insecure Design
Availability Available for C and C++.
Enabling Checks for this warning class are enabled by default. To disable them, add the following WARNING_FILTER rule to the project configuration file.
WARNING_FILTER += discard class="LocalUnlock on LMEM_FIXED Memory"
Class Name Pool Mismatch
Significance security
Mnemonic ALLOC.TM
Categories
AUTOSARC++14 AUTOSARC++14:A18-5-5 Memory management functions shall ensure the following: (a) deterministic behavior resulting with the existence of worst-case execution time, (b) avoiding memory fragmentation, (c) avoid running out of memory, (d) avoiding mismatched allocations or deallocations, (e) no dependence on non-deterministic calls to kernel.
MisraC++2023 MisraC++2023:0.3.2 A function call shall not violate the function's preconditions
CWE CWE:762 Mismatched Memory Management Routines
CERT-C CERT-C:API07-C Enforce type safety
  CERT-C:WIN30-C Properly pair allocation and deallocation functions
Availability Available for C and C++.
Enabling Checks for this warning class are enabled by default. To disable them, add the following WARNING_FILTER rule to the project configuration file.
WARNING_FILTER += discard class="Pool Mismatch"
Class Name Type Mismatch
Significance security
Mnemonic ALLOC.TM
Categories
MisraC2025 MisraC2025:1.3 There shall be no occurrence of undefined or critical unspecified behaviour
  MisraC2025:22.2 A block of memory shall only be freed if it was allocated by means of a Standard Library function
  MisraC2025:D.4.1 Run-time failures shall be minimized
MisraC2023 MisraC2023:1.3 There shall be no occurrence of undefined or critical unspecified behaviour
  MisraC2023:22.2 A block of memory shall only be freed if it was allocated by means of a Standard Library function
  MisraC2023:D.4.1 Run-time failures shall be minimized
Misra2012 Misra2012:1.3 There shall be no occurrence of undefined or critical unspecified behaviour
  Misra2012:22.2 A block of memory shall only be freed if it was allocated by means of a Standard Library function
  Misra2012:D.4.1 Run-time failures shall be minimized
AUTOSARC++14 AUTOSARC++14:A18-5-3 The form of the delete expression shall match the form of the new expression used to allocate the memory.
  AUTOSARC++14:A18-5-5 Memory management functions shall ensure the following: (a) deterministic behavior resulting with the existence of worst-case execution time, (b) avoiding memory fragmentation, (c) avoid running out of memory, (d) avoiding mismatched allocations or deallocations, (e) no dependence on non-deterministic calls to kernel.
MisraC++2023 MisraC++2023:0.3.2 A function call shall not violate the function's preconditions
CWE CWE:590 Free of Memory not on the Heap
  CWE:686 Function Call With Incorrect Argument Type
  CWE:761 Free of Pointer not at Start of Buffer
  CWE:762 Mismatched Memory Management Routines
  CWE:843 Access of Resource Using Incompatible Type ('Type Confusion')
TS17961 TS17961:5.33-xfree Reallocating or freeing memory that was not dynamically allocated
CERT-C CERT-C:API07-C Enforce type safety
  CERT-C:MEM34-C Only free memory allocated dynamically
  CERT-C:WIN30-C Properly pair allocation and deallocation functions
CERT-CPP CERT-CPP:EXP51-CPP Do not delete an array through a pointer of the incorrect type
  CERT-CPP:MEM51-CPP Properly deallocate dynamically allocated resources
OWASP-2021 OWASP-2021:A4 Insecure design
OWASP-2025 OWASP-2025:A06 Insecure Design
Availability Available for C and C++.
Enabling Checks for this warning class are enabled by default. To disable them, add the following WARNING_FILTER rule to the project configuration file.
WARNING_FILTER += discard class="Type Mismatch"

Warning Cases

The following sections describe the Type Mismatch warning cases.

"Type Mismatch" Warnings

CodeSonar will issue a Type Mismatch warning when a checked function is called with an argument that was previously allocated or used with a contradictory type. This includes an attempt to free memory that was not allocated on the heap.

The checked function/argument pairs are divided into type families based on the required type of the argument. For example, fclose(FILE *stream) and feof(FILE *stream) are in the same type family because they both require a valid FILE* argument.

"Pool Mismatch" Warnings

CodeSonar ships with library models that allow it to recognize a large number of functions that take multiple memory region arguments that should not overlap. If one of these functions is called with incompatible arguments, a warning will be issued.

If you have created a custom library model for some function f() in terms of one of these existing models, calls to f() will also be capable of triggering Pool Mismatch warnings.

Fixed Memory Object Warnings

CodeSonar has specific warning classes for attempts to call certain Win32 API functions on fixed local or global memory objects.

Class Name Triggering Function Triggered If
LocalLock on LMEM_FIXED Memory LocalLock() The local memory object was originally allocated with the LMEM_FIXED flag.
LocalHandle on LMEM_FIXED Memory LocalHandle()
LocalUnlock on LMEM_FIXED Memory LocalUnlock()
GlobalLock on GMEM_FIXED Memory GlobalLock() The global memory object was originally allocated with the GMEM_FIXED flag.
GlobalHandle on GMEM_FIXED Memory GlobalHandle()
GlobalUnlock on GMEM_FIXED Memory GlobalUnlock()

Example

#include <stdlib.h>
#include <stdio.h>
#include <dlfcn.h>
#include <windows.h>

void mix_malloc_delete()
{
    char *p = (char*)malloc( 10 ); 
    if( p )
        delete[] p; /* "Type Mismatch" warning issued here */
}

void free_stack_variable()
{
    char buf[10];
    char *q = &buf[0];
    free(q); /* "Type Mismatch" warning issued here */
}

void conflicting_types(void *p)
{
    (void)ftell((FILE*)p);
    (void)dlsym(p, "f"); /* "Type Mismatch" warning issued here -- p can't
                          * be both a FILE* and a dynamic shared object at
                          * the same time.
                          */
}

void wrong_pool()
{
    HANDLE pool1 = HeapCreate(0, 0, 1024*1024);
    HANDLE pool2 = HeapCreate(0, 0, 1024*1024);
    LPVOID ha = pool1 ? HeapAlloc(pool1, 0, 5) : NULL;
    if( pool2 && ha )
        (void)HeapFree(pool2, 0, ha); /* "Pool Mismatch" warning issued here; should have used pool1 */
    exit(0);
}

void conflicting_types_win32(HANDLE h)
{
    (void)GetThreadId(h);
    (void)GetFileSize(h, 0); /* "Type Mismatch" warning issued here --
                              * h can't be both a thread and a window
                              * at the same time.
                              */
    exit(0);
}

void ghandle(void)
{
    HGLOBAL x = GlobalAlloc(GMEM_FIXED, 16);
    if( x )
    {
        (void)GlobalHandle(x); // GlobalHandle on GMEM_FIXED Memory issued here
        (void)GlobalLock(x);   // GlobalLock on GMEM_FIXED Memory issued here
        (void)GlobalUnlock(x); // GlobalUnlock on GMEM_FIXED Memory issued here
    }
    exit(0);
}

void lhandle(void)
{
    HLOCAL x = LocalAlloc(LMEM_FIXED, 16);
    if( x )
    {
        (void)LocalHandle(x); // LocalHandle on LMEM_FIXED Memory issued here
        (void)LocalLock(x);   // LocalLock on LMEM_FIXED Memory issued here
        (void)LocalUnlock(x); // LocalUnlock on LMEM_FIXED Memory issued here
    }
    exit(0);
}

Relevant Configuration File Parameters

The following configuration file parameters affect checks for this warning class.

 

To report problems with this documentation, please visit https://support.codesecure.com/.