Skip to content

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

Open
henryiii wants to merge 1 commit into
pybind:masterfrom
henryiii:henryiii/fix/tuple-caster-item-lifetime
Open

henryiii wants to merge 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

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