Skip to content

Allow direct assignment of struct/union fields#17

Merged
lacraig2 merged 3 commits intomainfrom
directassign
Mar 12, 2026
Merged

Allow direct assignment of struct/union fields#17
lacraig2 merged 3 commits intomainfrom
directassign

Conversation

@lacraig2
Copy link
Contributor

This pull request adds support for direct assignment of struct and union fields in the DFFI (DWARF FFI) system, allowing users to assign one struct instance to another, or to assign raw bytes to struct/union fields, with robust safety checks. It includes comprehensive tests to verify correct behavior, memory safety, and error handling for these assignments.

Feature: Direct struct/union assignment and byte-level assignment

  • Enhanced the _write_data method in src/dwarffi/instances.py to allow direct assignment of a BoundTypeInstance (struct/union) or a byte-like object to struct/union fields, with strict size checks to prevent buffer overflows. Assignments with incorrect types or sizes now raise clear errors.

Testing: Robustness and safety of struct/union assignment

  • Added test_struct_direct_assignment to tests/test_struct_array_assignment.py to verify correct memory copying, cache invalidation, and error handling for struct-to-struct assignments, including size and type mismatches.
  • Introduced a new test suite in tests/test_struct_assignment_offset_safety.py covering:
    • Safe assignment to struct fields at non-zero offsets, ensuring no buffer overflows.
    • Nested struct assignment and cache invalidation.
    • Union assignment and correct handling of overlapping fields.
    • Size mismatch and type safety errors for assignments.
    • Assignment of raw bytes to struct fields and validation of correct memory updates.
    • Rejection of invalid assignment types and incorrect byte sizes.

@lacraig2 lacraig2 merged commit cae0093 into main Mar 12, 2026
3 checks passed
@lacraig2 lacraig2 deleted the directassign branch March 12, 2026 23:28
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.

1 participant