Skip to content

Conversation

@lcsmuller
Copy link
Owner

Check whether provided pointer is an actual MIDA data, by checking its signature header

Check whether provided pointer is an actual MIDA data, by checking its
signature header
@lcsmuller lcsmuller requested a review from Copilot October 13, 2025 19:22
@lcsmuller lcsmuller self-assigned this Oct 13, 2025
@lcsmuller lcsmuller marked this pull request as draft October 13, 2025 19:22
Copy link

Copilot AI left a 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.

Comment on lines +63 to +68
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)
{
Copy link

Copilot AI Oct 13, 2025

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.

Copilot uses AI. Check for mistakes.
Comment on lines +77 to +81
for (size_t i = 0; i < (size_t)MIDA_SIGNATURE_SIZE; ++i) {
if (buffer[i] != sig[i]) {
return 0;
}
}
Copy link

Copilot AI Oct 13, 2025

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.

Copilot uses AI. Check for mistakes.
Comment on lines +327 to +329
if (MIDA_PREFIX_SIZE) {
__mida_write_signature(buffer);
}
Copy link

Copilot AI Oct 13, 2025

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.

Copilot uses AI. Check for mistakes.
Comment on lines +345 to +347
if (MIDA_PREFIX_SIZE) {
__mida_write_signature(buffer);
}
Copy link

Copilot AI Oct 13, 2025

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.

Copilot uses AI. Check for mistakes.
Comment on lines +371 to +373
if (MIDA_PREFIX_SIZE) {
__mida_write_signature(buffer);
}
Copy link

Copilot AI Oct 13, 2025

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.

Suggested change
if (MIDA_PREFIX_SIZE) {
__mida_write_signature(buffer);
}
#if MIDA_PREFIX_SIZE
__mida_write_signature(buffer);
#endif

Copilot uses AI. Check for mistakes.
Comment on lines +386 to +390
if (MIDA_PREFIX_SIZE) {
/* container points to the beginning of the container region; header is
* just before */
__mida_write_signature(container - MIDA_PREFIX_SIZE);
}
Copy link

Copilot AI Oct 13, 2025

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.

Copilot uses AI. Check for mistakes.
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