array_t::at() returns a misaligned reference when the stride is not a multiple of alignof(T)

Đang mở
#6,309 0 bình luận 0 reaction 0 người được giao Xem trên GitHub

Maintainer thường phản hồi trong vòng 3 ngày

@henryiii đang làm issue này rồi.

Từ ngày 25/11/2026.

  • #6311 của @henryiii — đang mở

Đánh giá

Độ khó
4/5
Thời gian dự kiến
3-5 ngày
Mức phù hợp với người mới
35/100
Loại issue
Lỗi
Độ rõ ràng
Khá rõ ràng
Mức độ hoạt động
Sôi nổi
Công nghệ
cpp, python
Lĩnh vực
api, data

Hướng nghiên cứu

The misaligned reference comes from array_t::at() and mutable_at() in include/pybind11/numpy.h, around line 1526. Start with the element address calculation there, then run test_at_stride_not_multiple_of_itemsize under the UBSan CI job, which currently deselects it. Done means the test passes under UBSan without the deselect, and either a throw for misaligned elements or an alignment check at array creation has been chosen and implemented.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

🤖 AI text below 🤖

Since #6241, array_t<T>::at() and mutable_at() support strides that are not a multiple of sizeof(T). They return a T & to the element. If the stride is not a multiple of alignof(T), that reference is misaligned. This is undefined behavior. It works on x86 and arm64, but it can fail on platforms that require aligned access, and UBSan reports it:

include/pybind11/numpy.h:1526: runtime error: reference binding to misaligned address 0x00000a5cf433 for type 'const short unsigned int', which requires 2 byte alignment

Reproducer (from test_at_stride_not_multiple_of_itemsize):

packed = np.zeros(3, dtype=[("a", np.uint16), ("b", np.uint8)])
m.at_t(packed["a"], 1)  # stride 3, element 1 is at an odd address

A reference cannot point to a misaligned T. Possible fixes:

  • Throw when the element address is not aligned for T.
  • Throw when the array is not aligned (NPY_ARRAY_ALIGNED), possibly as a check when the array_t is created.

The UBSan CI job deselects this test until this is fixed.

Ngôn ngữ chính
C++
Star
18k
Fork
2.3k
Merge trung bình
5 ngày 7 giờ
Pull request đã merge (30 ngày)
53

Chuẩn bị môi trường

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của pybind/pybind11

Tất cả issue của pybind/pybind11

Issue tương tự

Thêm issue về C++

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.