Skip to content

Commit

Permalink
Fixed issue laruence#40
Browse files Browse the repository at this point in the history
Using absolute index could be error-prone, updated code to use relative indices as it commonly done with Lua
  • Loading branch information
Mikhail Galanin authored and tony2001 committed May 22, 2019
1 parent 84c831e commit 2cce013
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
5 changes: 4 additions & 1 deletion lua.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) */
Expand Down
19 changes: 19 additions & 0 deletions tests/issue040.phpt
Original file line number Diff line number Diff line change
@@ -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
(
)

0 comments on commit 2cce013

Please sign in to comment.