-
Notifications
You must be signed in to change notification settings - Fork 2
Wip/Add header signature safety measure #7
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Conversation
Check whether provided pointer is an actual MIDA data, by checking its signature header
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
This PR adds a signature header safety measure to the MIDA library to validate that pointers passed to MIDA functions are actually MIDA-managed data. The implementation adds an optional (enabled by default) hidden signature prefix that can be checked to detect misuse.
Key changes:
- Introduces signature header functionality with configurable bytes and size
- Updates all allocation/wrapping functions to write signatures and account for prefix size
- Adds validation helper
mida_is_valid()to check signature integrity
Reviewed Changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 6 comments.
| File | Description |
|---|---|
| mida.h | Core implementation of signature header system with macros, inline functions, and updated allocation/wrapping logic |
| test/test.c | Updates test macro to account for new prefix size in bytemap calculations |
| README.md | Documents the new signature header feature with configuration options and usage examples |
Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.
| for (size_t i = 0; i < (size_t)MIDA_SIGNATURE_SIZE; ++i) { | ||
| buffer[i] = sig[i]; | ||
| } | ||
| for (size_t i = (size_t)MIDA_SIGNATURE_SIZE; i < (size_t)MIDA_PREFIX_SIZE; | ||
| ++i) | ||
| { |
Copilot
AI
Oct 13, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[nitpick] The explicit cast to (size_t) for MIDA_SIGNATURE_SIZE and MIDA_PREFIX_SIZE is redundant since these are already defined as size_t compatible values. Consider removing the casts for cleaner code.
| for (size_t i = 0; i < (size_t)MIDA_SIGNATURE_SIZE; ++i) { | ||
| if (buffer[i] != sig[i]) { | ||
| return 0; | ||
| } | ||
| } |
Copilot
AI
Oct 13, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[nitpick] Similar to the previous function, the explicit cast to (size_t) for MIDA_SIGNATURE_SIZE is redundant and can be removed for cleaner code.
| if (MIDA_PREFIX_SIZE) { | ||
| __mida_write_signature(buffer); | ||
| } |
Copilot
AI
Oct 13, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The condition if (MIDA_PREFIX_SIZE) is evaluated at runtime but could be optimized with a compile-time check. Consider using #if MIDA_PREFIX_SIZE instead to eliminate the runtime branch when signatures are disabled.
| if (MIDA_PREFIX_SIZE) { | ||
| __mida_write_signature(buffer); | ||
| } |
Copilot
AI
Oct 13, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same as in __mida_malloc, this runtime condition should be a compile-time check using #if MIDA_PREFIX_SIZE for better performance when signatures are disabled.
| if (MIDA_PREFIX_SIZE) { | ||
| __mida_write_signature(buffer); | ||
| } |
Copilot
AI
Oct 13, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This runtime condition should also be replaced with #if MIDA_PREFIX_SIZE for consistency and to eliminate unnecessary runtime checks when signatures are disabled.
| if (MIDA_PREFIX_SIZE) { | |
| __mida_write_signature(buffer); | |
| } | |
| #if MIDA_PREFIX_SIZE | |
| __mida_write_signature(buffer); | |
| #endif |
| if (MIDA_PREFIX_SIZE) { | ||
| /* container points to the beginning of the container region; header is | ||
| * just before */ | ||
| __mida_write_signature(container - MIDA_PREFIX_SIZE); | ||
| } |
Copilot
AI
Oct 13, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Replace this runtime condition with #if MIDA_PREFIX_SIZE to maintain consistency with the optimization pattern used elsewhere in the codebase.
Check whether provided pointer is an actual MIDA data, by checking its signature header