Skip to content

fix: keep tuple_caster items alive until the subcasters are used - #6277

Merged
rwgk merged 1 commit into
pybind:masterfrom
henryiii:henryiii/fix/tuple-caster-item-lifetime
Nov 21, 2026
Merged

rwgk merged 1 commit into
pybind:masterfrom
henryiii:henryiii/fix/tuple-caster-item-lifetime

Conversation

@henryiii

Copy link
Copy Markdown
Collaborator

🤖 AI text below 🤖

Description

tuple_caster::load loaded each subcaster from a temporary seq[Is], which released the item when load_impl returned. The subcasters are used later, in the conversion operator. A subcaster can refer to its item (for example, the caster of a py::class_ type holds a pointer into the instance). If the sequence creates its items on access and does not keep them, the conversion read a destroyed object.

The caster now holds the items in a std::array<object, size> member. A tuple or list argument was not affected in normal use, because the container keeps its items alive.

This is the same item-lifetime problem that #6276 fixes for array_caster.

The new test fails on master.

Suggested changelog entry:

  • Fixed a use-after-free in the std::pair / std::tuple caster for sequences that create their items on access.

tuple_caster loaded each subcaster from a temporary item, but uses the
subcasters later. A subcaster can refer to its item, so a sequence that
creates items on access caused a use-after-free.

Assisted-by: ClaudeCode:claude-fable-5-2
@rwgk
rwgk merged commit 3c97f2b into pybind:master Nov 21, 2026
96 checks passed
@rwgk
rwgk deleted the henryiii/fix/tuple-caster-item-lifetime branch November 21, 2026 18:33
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.

2 participants