Skip to content

Fix with_std_vector() element cast collision across types under Clang - #317

Merged
joergbrech merged 1 commit into
mainfrom
fix-with-std-vector-clang-lambda-collision
Nov 16, 2026
Merged

joergbrech merged 1 commit into
mainfrom
fix-with-std-vector-clang-lambda-collision

Conversation

@joergbrech

Copy link
Copy Markdown
Collaborator

sol2 names a pushed functor's metatable after a PRETTY_FUNCTION-derived type name, and Clang spells a lambda there as "(lambda at file:line:col)" without the enclosing template arguments. The captureless cast lambda inside with_std_vector() therefore got the same metatable name for every instantiation, so the second type to call with_std_vector() in one lua_State silently reused the first type's __call: B.as_vec(...) invoked A's cast, corrupting the heap with the unchecked getter or rejecting every argument with the checked one. GCC and MSVC produce distinct names, which is why only Emscripten and macOS builds ever showed it.

Push a named function template (details::identity_cast) instead - a C closure, no functor metatable, and a name that carries its template argument on every compiler. Adds a regression test with two with_std_vector() types in one state, the shape none of the existing tests covered.

Fixes #300

sol2 names a pushed functor's metatable after a __PRETTY_FUNCTION__-derived
type name, and Clang spells a lambda there as "(lambda at file:line:col)"
without the enclosing template arguments. The captureless cast lambda inside
with_std_vector<VecElement>() therefore got the same metatable name for every
instantiation, so the second type to call with_std_vector() in one lua_State
silently reused the first type's __call: B.as_vec(...) invoked A's cast,
corrupting the heap with the unchecked getter or rejecting every argument
with the checked one. GCC and MSVC produce distinct names, which is why only
Emscripten and macOS builds ever showed it.

Push a named function template (details::identity_cast<VecElement>) instead -
a C closure, no functor metatable, and a name that carries its template
argument on every compiler. Adds a regression test with two with_std_vector()
types in one state, the shape none of the existing tests covered.

Fixes #300

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
@joergbrech
joergbrech merged commit 6b08b0e into main Nov 16, 2026
14 checks passed
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.

with_std_vector(): heap corruption (a B userdata is copied as an A) when two types are used through as_vec - reproduced in WebAssembly

1 participant