Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dangerous string lifetime assumption #20

Open
snake-4 opened this issue Nov 12, 2024 · 0 comments
Open

Dangerous string lifetime assumption #20

snake-4 opened this issue Nov 12, 2024 · 0 comments
Labels

Comments

@snake-4
Copy link

snake-4 commented Nov 12, 2024

const char* ret = luaL_checkstring(state, -1);

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.

const char* 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.

@snake-4 snake-4 changed the title Possibly dangerous string lifetime assumption Dangerous string lifetime assumption Nov 12, 2024
@vapourismo vapourismo added the bug label Nov 15, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants