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

Ouverte
#6,309 0 commentaires 0 réactions 0 personnes assignées Voir sur GitHub

Les mainteneurs répondent en général sous 3 jours

@henryiii y travaille déjà.

Depuis le 25/11/2026.

  • #6311 par @henryiii — ouverte

Évaluation

Difficulté
4/5
Temps estimé
3-5 jours
Accessibilité débutants
35/100
Type d'issue
Bug
Clarté
Plutôt claire
Activité
Active
Stack technique
cpp, python
Domaine
api, data

Piste de recherche

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.

Rédigé par le modèle d'indexation à partir du texte de l'issue.

Description

🤖 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.

Langage dominant
C++
Étoiles
18k
Forks
2.3k
Merge moyen
5 j 7 h
PR mergées (30 j)
53

Préparer son environnement

Par où commencer

  1. Lisez l'issue en entier, puis le guide de contribution du projet.
  2. Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
  3. Forkez le dépôt et travaillez sur une branche.
  4. Ouvrez une pull request qui référence le numéro de l'issue.

Autres issues de pybind/pybind11

Toutes les issues de pybind/pybind11

Issues similaires

Plus d'issues C++

Recevez les nouvelles issues par e-mail

Un résumé court des issues GitHub adaptées aux débutants.