diff --git a/.gitignore b/.gitignore index 2e94319..9c67af3 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ +*.o *.lo *.la .*.swp diff --git a/lua.c b/lua.c index c532e52..0a75b7f 100755 --- a/lua.c +++ b/lua.c @@ -321,15 +321,18 @@ 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_pushnil(L); /* first key */ - while (lua_next(L, index-1) != 0) { + lua_pushvalue(L, index); // table + lua_pushnil(L); // first key + while (lua_next(L, -2) != 0) { zval key, val; - /* uses 'key' (at index -2) and 'value' (at index -1) */ - if (!php_lua_get_zval_from_lua(L, -2, lua_obj, &key)) { + lua_pushvalue(L, -2); + + /* uses 'key' (at index -1) and 'value' (at index -2) */ + if (!php_lua_get_zval_from_lua(L, -1, lua_obj, &key)) { break; } - if (!php_lua_get_zval_from_lua(L, -1, lua_obj, &val)) { + if (!php_lua_get_zval_from_lua(L, -2, lua_obj, &val)) { zval_ptr_dtor(&key); /* there is a warning already in php_lua_get_zval_from_lua */ break; @@ -349,17 +352,13 @@ zval *php_lua_get_zval_from_lua(lua_State *L, int index, zval *lua_obj, zval *rv default: break; } - lua_pop(L, 1); + lua_pop(L, 2); } + lua_pop(L, 1); break; case LUA_TFUNCTION: { long ref_id = 0; - if (!lua_obj) { - php_error_docref(NULL, E_WARNING, "corrupted Lua object"); - break; - } - lua_pushvalue(L, index); ref_id = luaL_ref(L, LUA_REGISTRYINDEX); diff --git a/lua_closure.c b/lua_closure.c index b9789e2..50ef039 100644 --- a/lua_closure.c +++ b/lua_closure.c @@ -53,7 +53,9 @@ zval* php_lua_closure_instance(zval *instance, long ref_id, zval *lua_obj) { object_init_ex(instance, lua_closure_ce); objval = php_lua_closure_object_from_zend_object(Z_OBJ_P(instance)); objval->closure = ref_id; - ZVAL_ZVAL(&(objval->lua), lua_obj, 1, 0); + if (lua_obj) { + ZVAL_ZVAL(&(objval->lua), lua_obj, 1, 0); + } return instance; } @@ -75,7 +77,7 @@ PHP_METHOD(lua_closure, invoke) { zval rv; if (ZEND_NUM_ARGS()) { - arguments = emalloc(sizeof(zval*) * ZEND_NUM_ARGS()); + arguments = emalloc(sizeof(zval) * ZEND_NUM_ARGS()); if (zend_get_parameters_array_ex(ZEND_NUM_ARGS(), arguments) == FAILURE) { efree(arguments); zend_throw_exception_ex(NULL, 0, "cannot get arguments for calling closure"); diff --git a/tests/issue040.phpt b/tests/issue040.phpt new file mode 100644 index 0000000..e2f4738 --- /dev/null +++ b/tests/issue040.phpt @@ -0,0 +1,19 @@ +--TEST-- +ISSUE #040 (segmentation fault) +--SKIPIF-- + +--FILE-- +eval(<< +--EXPECT-- +Array + ( + ) diff --git a/tests/print.phpt b/tests/print.phpt new file mode 100644 index 0000000..600fb31 Binary files /dev/null and b/tests/print.phpt differ diff --git a/tests/print_tables-1.phpt b/tests/print_tables-1.phpt new file mode 100644 index 0000000..2795f70 --- /dev/null +++ b/tests/print_tables-1.phpt @@ -0,0 +1,28 @@ +--TEST-- +print with tables - 1 +--SKIPIF-- + +--FILE-- +eval(<< +--EXPECT-- +Array + ( + [1] => Array + ( + [phone] => 123456 + ) + + ) diff --git a/tests/print_tables-2.phpt b/tests/print_tables-2.phpt new file mode 100644 index 0000000..a0e1b6c --- /dev/null +++ b/tests/print_tables-2.phpt @@ -0,0 +1,22 @@ +--TEST-- +print with tables - 2 +--SKIPIF-- + +--FILE-- +eval(<< +--EXPECT-- +Array + ( + [LEFT] => 1 + )