Skip to content

fix: avoid dangling reference in optional/variant of reference_wrapper - #6232

Open
sayed0200 wants to merge 1 commit into
pybind:masterfrom
sayed0200:stl-optional-variant-refwrapper-uaf
Open

sayed0200 wants to merge 1 commit into
pybind:masterfrom
sayed0200:stl-optional-variant-refwrapper-uaf

Conversation

@sayed0200

@sayed0200 sayed0200 commented Oct 31, 2026 •

Copy link
Copy Markdown
Contributor

Description

Closes #5637. Tests only, rebased on master.

#6283 fixed the underlying lifetime problem, and std::optional/std::variant of std::reference_wrapper is fixed on master as a side effect (the reference_wrapper caster propagates refers_to_caster). #6283 doesn't reference #5637 and its element-caster coverage uses const char * for the optional/variant cases and std::vector<std::reference_wrapper<const std::string>> for the reference_wrapper case, so the exact shapes from #5637 aren't exercised. This adds them to test_element_casters_kept_alive rather than as separate tests: load_optional_str_ref and load_variant_str_ref round-trip the existing heap-allocated string through std::optional<std::reference_wrapper<const std::string>> and std::variant<int, std::reference_wrapper<const std::string>>, plus a static_assert that cast_refers_to_caster<std::reference_wrapper<const std::string>> is true (next to the existing one asserting it is false for std::reference_wrapper<UserType>, which is the interesting contrast).

Both assertions fail on the commit before #6283 and pass on master. Building the #5637 reproducer against e770e5ed^ returns a 200-character string with freed contents from both entry points; against master both round-trip intact.

@rwgk

rwgk commented Nov 1, 2026

Copy link
Copy Markdown
Collaborator

xref: #6217 (comment)

@sayed0200 could you please comment there?

@sayed0200

Copy link
Copy Markdown
Contributor Author

Replied on #6217: #6217 (comment). Short version: #6283 (merged yesterday) already fixes the optional/variant reference_wrapper cases from #5637, so the header change here is redundant. I've offered there to trim this PR to the two regression tests or close it, whichever you prefer.

@henryiii

Copy link
Copy Markdown
Collaborator

You can try the two regression tests if you think they add coverage.

@sayed0200
sayed0200 force-pushed the stl-optional-variant-refwrapper-uaf branch from 2fbc637 to 05a4673 Compare December 1, 2026 09:04
@sayed0200

Copy link
Copy Markdown
Contributor Author

Trimmed to tests only and rebased on master, header change dropped.

I folded them into test_element_casters_kept_alive instead of adding standalone tests, since it's the same concern: load_optional_str_ref and load_variant_str_ref reuse the heap-allocated string already in that test, and there's a static_assert that cast_refers_to_caster<std::reference_wrapper<const std::string>> is true, next to the existing one that it's false for std::reference_wrapper<UserType>.

On the coverage question: both new assertions fail on e770e5ed^ and pass on master, so they do bite. #6283's optional/variant cases use const char * and its reference_wrapper case is inside a vector, so the two shapes from #5637 weren't covered. Happy to drop it if you'd rather not grow that test.

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.

[BUG]: function with argument type std::optional<std::reference_wrapper<Eigen::...>> gives segmentation fault

3 participants