You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The read method of Value specialization where type = const char* returns the result of a luaL_checklstring call and subsequently pops the string from the stack. The returned memory may unexpectedly be GC'd during another Lua operation.
This is contrary to the typical usage pattern of lua_tolstring where the returned memory would be used or copied before the value is popped.
constchar* value = luaL_checklstring(state, -1, &length);
The same problem exists to some degree in the type = std::string specialization, where the value is popped from the stack before a std::string is constructed using the pointer. In practice, the memory won't be GC'd in this instance but Lua makes no guarantee of this behavior and it may change in a future release.
I'd suggest either removing the const char* specialization or changing it so that it copies the string to a newly allocated location. The std::string specialization can be fixed by moving the construction of the std::string to before the pop call.
The text was updated successfully, but these errors were encountered:
snake-4
changed the title
Possibly dangerous string lifetime assumption
Dangerous string lifetime assumption
Nov 12, 2024
luwra/lib/luwra/values.hpp
Line 211 in e0e4b6b
The
read
method ofValue
specialization wheretype = const char*
returns the result of aluaL_checklstring
call and subsequently pops the string from the stack. The returned memory may unexpectedly be GC'd during another Lua operation.This is contrary to the typical usage pattern of
lua_tolstring
where the returned memory would be used or copied before the value is popped.luwra/lib/luwra/values.hpp
Line 230 in e0e4b6b
The same problem exists to some degree in the
type = std::string
specialization, where the value is popped from the stack before astd::string
is constructed using the pointer. In practice, the memory won't be GC'd in this instance but Lua makes no guarantee of this behavior and it may change in a future release.I'd suggest either removing the
const char*
specialization or changing it so that it copies the string to a newly allocated location. Thestd::string
specialization can be fixed by moving the construction of thestd::string
to before the pop call.The text was updated successfully, but these errors were encountered: