Skip to content

fix(numpy): check typed data pointer alignment in debug builds - #6323

Open
rwgk wants to merge 1 commit into
pybind:masterfrom
rwgk:rwgk/numpy-pointer-alignment-debug
Open

rwgk wants to merge 1 commit into
pybind:masterfrom
rwgk:rwgk/numpy-pointer-alignment-debug

Conversation

@rwgk

@rwgk rwgk commented Nov 27, 2026

Copy link
Copy Markdown
Collaborator

— codex on behalf of Ralf —

Description

Follow-up to #6311. Callers of array_t<T>::data() and mutable_data() must ensure that the element address is aligned for T. Builds without NDEBUG now check the raw address before casting to the typed pointer and throw py::value_error on misalignment. The NumPy guide documents the precondition and the need for consistent NDEBUG settings across an extension.

Validation

  • Both debug regression cases failed on the pre-fix headers and passed with the change. The test exercises the const and mutable accessors without dereferencing the pointer.
  • The full Catch2 and pytest suites passed on regular and free-threaded CPython 3.14.10. The optimized NumPy array suites passed with the two expected debug-only skips.
  • GCC generated identical optimized release assembly for representative calls to both accessors before and after the change. Pre-commit checks passed.

Suggested changelog entry:

  • In debug builds, array_t<T>::data() and mutable_data() now report misaligned element addresses with ValueError.

Check the raw address before converting it to a typed pointer when NDEBUG is undefined. Document the alignment precondition and exercise both accessors without dereferencing the returned pointer.

This branch has not been deployed

No deployments
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