Bug: luaL_tolstring may get confused with negative index

When object has a '__name' metafield, 'luaL_tolstring' used the
received index after pushing a string on the stack.
This commit is contained in:
Roberto Ierusalimschy
2021-07-22 13:48:43 -03:00
parent 62fb934427
commit 439e45a2f6
3 changed files with 20 additions and 0 deletions

View File

@@ -881,6 +881,7 @@ LUALIB_API lua_Integer luaL_len (lua_State *L, int idx) {
LUALIB_API const char *luaL_tolstring (lua_State *L, int idx, size_t *len) {
idx = lua_absindex(L,idx);
if (luaL_callmeta(L, idx, "__tostring")) { /* metafield? */
if (!lua_isstring(L, -1))
luaL_error(L, "'__tostring' must return a string");