diff options
author | kyren <kerriganw@gmail.com> | 2017-08-02 10:42:18 -0400 |
---|---|---|
committer | kyren <kerriganw@gmail.com> | 2017-08-02 10:42:18 -0400 |
commit | f9f3d0580458e8af7051095eec16022a5fa1bbba (patch) | |
tree | 8acd9f91c18eb35aabb25b8b686dd0d3c92e9245 /src/util.rs | |
parent | f6cefca9169d8dcbb0d7e22c209bb6b5d7cc5794 (diff) | |
download | mlua-f9f3d0580458e8af7051095eec16022a5fa1bbba.zip |
Fix argument bugs with pcall / xpcall, add tests for it
Diffstat (limited to 'src/util.rs')
-rw-r--r-- | src/util.rs | 14 |
1 files changed, 12 insertions, 2 deletions
diff --git a/src/util.rs b/src/util.rs index 2cc56ed..1cd2a26 100644 --- a/src/util.rs +++ b/src/util.rs @@ -432,7 +432,11 @@ pub unsafe fn resume_with_traceback( // A variant of pcall that does not allow lua to catch panic errors from callback_error pub unsafe extern "C" fn safe_pcall(state: *mut ffi::lua_State) -> c_int { - if ffi::lua_pcall(state, ffi::lua_gettop(state) - 1, ffi::LUA_MULTRET, 0) != ffi::LUA_OK { + let top = ffi::lua_gettop(state); + if top == 0 { + push_string(state, "not enough arguments to pcall"); + ffi::lua_error(state); + } else if ffi::lua_pcall(state, top - 1, ffi::LUA_MULTRET, 0) != ffi::LUA_OK { if is_wrapped_panic(state, -1) { ffi::lua_error(state); } @@ -459,10 +463,16 @@ pub unsafe extern "C" fn safe_xpcall(state: *mut ffi::lua_State) -> c_int { } } + let top = ffi::lua_gettop(state); + if top < 2 { + push_string(state, "not enough arguments to xpcall"); + ffi::lua_error(state); + } + ffi::lua_pushvalue(state, 2); ffi::lua_pushcclosure(state, xpcall_msgh, 1); ffi::lua_copy(state, 1, 2); - ffi::lua_insert(state, 1); + ffi::lua_replace(state, 1); let res = ffi::lua_pcall(state, ffi::lua_gettop(state) - 2, ffi::LUA_MULTRET, 1); if res != ffi::LUA_OK { |