Skip to content

Fix late initialization of custom memory allocators in ndpiReader#3164

Merged
IvanNardi merged 4 commits into
ntop:devfrom
fabiodepin:fix-late-init-memory
Apr 21, 2026
Merged

Fix late initialization of custom memory allocators in ndpiReader#3164
IvanNardi merged 4 commits into
ntop:devfrom
fabiodepin:fix-late-init-memory

Conversation

@fabiodepin

Copy link
Copy Markdown
Contributor

ndpiReader was installing custom memory allocator hooks too late
(inside test_lib()), after several execution paths had already
invoked nDPI APIs that may allocate memory.

This could result in mixed allocator usage:

  • allocations performed using default libc (hooks not yet set)
  • deallocations performed using custom allocators

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:

  • Introduced ndpiReader_install_memory_hooks()
  • Installed memory hooks early in main(), immediately after API version check
  • Removed duplicate hook initialization from test_lib()
  • Updated example/README.md to document correct allocator usage

Rationale

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

  • No functional changes to DPI logic
  • No API/ABI impact
  • Safe for existing users

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.
@fabiodepin fabiodepin force-pushed the fix-late-init-memory branch from 4575e30 to e3986cf Compare April 16, 2026 21:55

@IvanNardi IvanNardi left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the fix, the issue is real.
I slightly simplified your patch.

@sonarqubecloud

Copy link
Copy Markdown

@IvanNardi IvanNardi merged commit 1462085 into ntop:dev Apr 21, 2026
26 checks passed
@IvanNardi

Copy link
Copy Markdown
Collaborator

@fabiodepin, thank you

@fabiodepin fabiodepin deleted the fix-late-init-memory branch April 21, 2026 22:40
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.

2 participants