Skip to content

Conversation

@0xfullex
Copy link
Contributor

@0xfullex 0xfullex commented Jan 6, 2026

Summary

  • Add pthread_mutex to protect all public functions for thread safety
  • Add integer overflow check before chunk size multiplication to prevent memory corruption
  • Add double-free detection using magic number at end of node
  • Align node_size to max_align_t for proper memory alignment
  • Fix format string argument order in memory_pool_check_node()
  • Add NULL pointer check in memory_pool_free()

Changes

Thread Safety

  • Added pthread_mutex_t mutex to MEMORY_POOL struct
  • Initialize mutex in memory_pool_init(), destroy in memory_pool_cleanup()
  • Lock/unlock mutex in memory_pool_alloc() and memory_pool_free()

Integer Overflow Protection

  • Check node_count_per_chunk > SIZE_MAX / node_size before multiplication in memory_pool_add_chunk()

Double-Free Detection

  • Added magic number constants MEMORY_POOL_MAGIC_ALLOCATED and MEMORY_POOL_MAGIC_FREE
  • Check and set magic number in memory_pool_free() to detect double-free attempts

Memory Alignment

  • Added align_size() helper function to align node_size to max_align_t

Bug Fixes

  • Fixed format string argument order in memory_pool_check_node() log message
  • Added NULL check for p_node parameter in memory_pool_free()

Test Plan

  • Compile verification passed
  • Manual testing with existing application
  • Concurrent access testing

🤖 Generated with Claude Code

0xfullex and others added 2 commits January 6, 2026 14:07
… protection

- Add pthread_mutex to protect all public functions for thread safety
- Add integer overflow check before chunk size multiplication
- Add double-free detection using magic number at end of node
- Align node_size to max_align_t for proper memory alignment
- Fix format string argument order in memory_pool_check_node()
- Add NULL pointer check in memory_pool_free()

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <[email protected]>
Copy link
Collaborator

@leafok88 leafok88 left a comment

Choose a reason for hiding this comment

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

This memory pool is used in multi-process application. Therefore, pthread_mutex is not a proper solution for concurrency control.

@leafok88 leafok88 closed this Jan 7, 2026
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