Hi John,
I do not know how I can test this fully with certainty (yes, I can set up a project for myself in Coverity in general) so I share it here too. I hope it helps.
Goal
Create a Coverity model that distinguishes between manual management (when allocator == NULL) and scoped management (when allocator is a valid pointer).
- When the allocator is NULL, treat the function like a standard malloc.
- When there is a scope, "sink" the pointer into that scope so Coverity understands the allocator will handle the cleanup.
Expected Results
Scenario: Manual
Allocator Passed: NULL
Coverity Behavior: Calls __coverity_alloc__. If no wmem_free(NULL, ptr) is found, a RESOURCE_LEAK is raised.
Scenario: Modern
Allocator Passed: Dissector
Coverity Behavior: pinfo->pool Calls wmem_alloc. Pointer is assigned to pool->storage_sink. No RESOURCE_LEAK raised.
Scenario: Epan Scope
Allocator Passed: wmem_epan_scope()
Coverity Behavior: Returns the singleton. Pointer is assigned to sink. No RESOURCE_LEAK raised.
Scenario: File Scope
Allocator Passed: wmem_file_scope()
Coverity Behavior: Returns the singleton. Pointer is assigned to sink. No RESOURCE_LEAK raised.
Examples
Scenario: Manual
Code Example: ptr = wmem_strdup(NULL, "test");
Coverity Result: RESOURCE_LEAK raised
Scenario: Manual
Code Example: ptr = wmem_strdup(NULL, "test");
Coverity Result: No issue
Scenario: Dissector Scope
Code Example: wmem_alloc(pinfo->pool, 10);
Coverity Result: No leak (escapes to pinfo->pool)
Scenario: EPAN/File Scope
Code Example: wmem_strdup(wmem_file_scope(), "data");
Coverity Result: No leak (escapes to singleton sink)
Regards,
Tamas