Return nil instead of an empty string when indexing beyond the string length

Fixes #5893
This commit is contained in:
Celtic Minstrel 2021-06-29 09:33:37 -04:00
parent 6f889d98ba
commit dd94e03a84

View file

@ -72,6 +72,14 @@ static int impl_str_index(lua_State* L)
lua_gettable(L, -2);
return 1;
} else if(lua_type(L, 2) == LUA_TNUMBER) {
// get the string length and the index
int len = lua_rawlen(L, 1);
int i = luaL_checkinteger(L, 2);
// In order to not break ipairs, an out-of-bounds access needs to return nil
if(i == 0 || abs(i) > len) {
lua_pushnil(L);
return 1;
}
// return string.sub(str, key, key)
luaW_getglobal(L, "string", "sub");
lua_pushvalue(L, 1);