diff --git a/nse_nsock.cc b/nse_nsock.cc index e9beebef5..70c60a066 100644 --- a/nse_nsock.cc +++ b/nse_nsock.cc @@ -44,7 +44,7 @@ extern NmapOps o; typedef struct nse_nsock_udata { nsock_iod nsiod; - unsigned timeout; + int timeout; lua_State *thread; @@ -739,9 +739,10 @@ static int l_get_info (lua_State *L) static int l_set_timeout (lua_State *L) { nse_nsock_udata *nu = check_nsock_udata(L, 1, false); - nu->timeout = luaL_checkinteger(L, 2); - if ((int) nu->timeout < -1) /* -1 is no timeout */ - return luaL_error(L, "Negative timeout: %d", nu->timeout); + int timeout = nseU_checkinteger(L, 2); + if (timeout < -1) /* -1 is no timeout */ + return luaL_error(L, "Negative timeout: %f", timeout); + nu->timeout = timeout; return nseU_success(L); } diff --git a/nse_utility.cc b/nse_utility.cc index 0bc5108ad..72e53c9b9 100644 --- a/nse_utility.cc +++ b/nse_utility.cc @@ -1,5 +1,6 @@ #include #include +#include #include "Target.h" #include "portlist.h" @@ -7,6 +8,16 @@ #include "nse_main.h" #include "nse_utility.h" +int nseU_checkinteger (lua_State *L, int arg) +{ + lua_Number n = luaL_checknumber(L, arg); + int i; + if (!lua_numbertointeger(floor(n), &i)) { + return luaL_error(L, "Number cannot be converted to an integer"); + } + return i; +} + int nseU_traceback (lua_State *L) { if (lua_isstring(L, 1)) diff --git a/nse_utility.h b/nse_utility.h index 2e6573c80..b4fdb5d72 100644 --- a/nse_utility.h +++ b/nse_utility.h @@ -8,6 +8,12 @@ class Target; #include #endif +/* int nseU_checkinteger (lua_State *L, int arg) + * + * Replacement for luaL_checkinteger that does a floor operation first + */ +int nseU_checkinteger (lua_State *L, int arg); + /* int nseU_traceback (lua_State *L) * * Traceback C Lua function.