Fix late initialization of custom memory allocators in ndpiReader#3164
Merged
Conversation
ndpiReader was setting custom memory allocation hooks only inside test_lib(), which is executed after several code paths that already use nDPI APIs (e.g., parseOptions, help/extcap paths, host checks, DoH initialization, etc.). This could lead to inconsistent allocator usage where memory is allocated using the default libc allocator (when hooks are unset) and later freed using custom allocators, resulting in undefined behavior with non-trivial allocators. This change introduces ndpiReader_install_memory_hooks() and calls it early in main(), immediately after the API version check and before any nDPI-related operations. The duplicate hook initialization in test_lib() has been removed to ensure a single initialization point. Additionally, example/README.md has been updated to document the correct usage pattern for custom allocators. This partially addresses ntop#1280.
4575e30 to
e3986cf
Compare
IvanNardi
approved these changes
Apr 20, 2026
IvanNardi
left a comment
Collaborator
There was a problem hiding this comment.
Thanks for the fix, the issue is real.
I slightly simplified your patch.
|
Collaborator
|
@fabiodepin, thank you |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.



ndpiReader was installing custom memory allocator hooks too late
(inside
test_lib()), after several execution paths had alreadyinvoked nDPI APIs that may allocate memory.
This could result in mixed allocator usage:
This is undefined behavior when using non-trivial allocators
(e.g., jemalloc, tcmalloc, custom pools).
Please sign (check) the below before submitting the Pull Request:
Link to the related 1280:
Changes:
ndpiReader_install_memory_hooks()main(), immediately after API version checktest_lib()example/README.mdto document correct allocator usageRationale
Ensuring that memory hooks are installed before any nDPI API usage
prevents inconsistent allocation/free pairs and avoids potential
memory corruption issues.
This change is limited to the example application (
ndpiReader)and does not modify libnDPI behavior or public APIs.
Relation to existing discussions
This partially addresses concerns raised in #1280 regarding
custom allocator usage and consistency.
Notes