Skip to content

fix(logstorage): decrement g_logstorage_cache_size on filter config free#838

Open
aki1770-del wants to merge 1 commit into
COVESA:masterfrom
aki1770-del:fix/logstorage-cache-size-leak-778
Open

fix(logstorage): decrement g_logstorage_cache_size on filter config free#838
aki1770-del wants to merge 1 commit into
COVESA:masterfrom
aki1770-del:fix/logstorage-cache-size-leak-778

Conversation

@aki1770-del
Copy link
Copy Markdown

Problem

dlt_logstorage_filter_config_free() calls free(data->cache) but never decrements the global g_logstorage_cache_size counter.

The counter is incremented during cache allocation in dlt_logstorage_create_cache():

g_logstorage_cache_size += cache_size + sizeof(DltLogStorageCacheFooter);

But on the free path the decrement is missing, so the counter grows unboundedly across disable/re-enable cycles. After enough cycles, g_logstorage_cache_size reaches g_max_logstorage_cache_size even with no active caches, causing all subsequent dlt_logstorage_create_cache() calls to fail with "Max cache size reached".

Reported in #778.

Fix

Before free(data->cache), compute the same cache_size value that was used during allocation and subtract it from g_logstorage_cache_size. Clamp to zero to guard against any bookkeeping inconsistency.

if (data->cache != NULL) {
    unsigned int cache_size = 0;
    if (DLT_OFFLINE_LOGSTORAGE_IS_STRATEGY_SET(data->sync,
            DLT_LOGSTORAGE_SYNC_ON_SPECIFIC_SIZE) > 0)
        cache_size = data->specific_size;
    else
        cache_size = data->file_size;
    cache_size += (unsigned int)sizeof(DltLogStorageCacheFooter);
    if (g_logstorage_cache_size >= cache_size)
        g_logstorage_cache_size -= cache_size;
    else
        g_logstorage_cache_size = 0;
    free(data->cache);
    data->cache = NULL;
}

Testing

Verified manually: repeated logstorage enable/disable cycles no longer exhaust the cache counter.

When dlt_logstorage_filter_config_free() frees data->cache it
never decremented the global g_logstorage_cache_size counter.
This caused the counter to grow unboundedly across
disable/re-enable cycles, eventually preventing any new cache
allocation even when physical memory is available.

Compute the same cache_size value used during allocation
(specific_size or file_size plus footer) and subtract it from
g_logstorage_cache_size before freeing the buffer, clamping
to zero to guard against any bookkeeping inconsistency.

Fixes COVESA#778
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant