Skip to content

fix(stl): determine the sequence length only once in array_caster - #6278

Open
henryiii wants to merge 2 commits into
pybind:masterfrom
henryiii:fix/array-caster-bounds
Open

henryiii wants to merge 2 commits into
pybind:masterfrom
henryiii:fix/array-caster-bounds

Conversation

@henryiii

Copy link
Copy Markdown
Collaborator

🤖 AI text below 🤖

Description

The element loops of array_caster asked the sequence for its length more than one time: once to size the storage, and again for the end of the iteration. A sequence that reports different lengths could cause out-of-bounds accesses: a write for std::valarray, a read for std::array. A list that another thread resizes in a free-threaded build is one example.

The loops now read the length one time and index up to it.

This is split out of the array_caster rework for #5765, which it does not depend on.

Suggested changelog entry:

  • Fixed possible out-of-bounds accesses when loading a std::array or std::valarray from a sequence whose length changes during the conversion.

The element loops of `array_caster` asked the sequence for its length
more than once: one time to size the storage, and again for the end of
the iteration. A sequence that reports different lengths could cause
out-of-bounds accesses (a write for `std::valarray`, a read for
`std::array`).

The loops now read the length one time and index up to it.

Assisted-by: ClaudeCode:claude-fable-5-2
This keeps the order that the loops had when they iterated over the
sequence.

Assisted-by: ClaudeCode:claude-fable-5-2

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