Fixed issue #40

Using absolute index could be error-prone, updated code to use relative indices as it commonly done with Lua
This commit is contained in:
Mikhail Galanin
2019-05-21 08:49:02 +01:00
committed by Antony Dovgal
parent 84c831e4cb
commit 24fd1baf19
2 changed files with 23 additions and 1 deletions

5
lua.c
View File

@@ -321,8 +321,11 @@ zval *php_lua_get_zval_from_lua(lua_State *L, int index, zval *lua_obj, zval *rv
break;
case LUA_TTABLE:
array_init(rv);
lua_pushvalue(L, index); /* stack now contains: -1 => table */
lua_pushnil(L); /* first key */
while (lua_next(L, index-1) != 0) {
/* stack now contains: -1 => nil; -2 => table */
while (lua_next(L, -2) != 0) {
zval key, val;
/* uses 'key' (at index -2) and 'value' (at index -1) */

19
tests/issue040.phpt Normal file
View File

@@ -0,0 +1,19 @@
--TEST--
ISSUE #040 (segment fault)
--SKIPIF--
<?php
if (!extension_loaded("lua")) print "skip lua extension missing";
?>
--FILE--
<?php
$lua = new Lua();
$lua->eval(<<<CODE
local a = {}
print(a)
CODE
);
?>
--EXPECT--
Array
(
)