1
0
mirror of https://github.com/nmap/nmap.git synced 2025-12-06 12:41:29 +00:00

Fix crashes when assigning timeouts as floats. New NSE utility function nseU_checkinteger

This commit is contained in:
dmiller
2016-07-17 04:56:29 +00:00
parent 62c4985536
commit dbc26606ca
3 changed files with 22 additions and 4 deletions

View File

@@ -44,7 +44,7 @@ extern NmapOps o;
typedef struct nse_nsock_udata typedef struct nse_nsock_udata
{ {
nsock_iod nsiod; nsock_iod nsiod;
unsigned timeout; int timeout;
lua_State *thread; lua_State *thread;
@@ -739,9 +739,10 @@ static int l_get_info (lua_State *L)
static int l_set_timeout (lua_State *L) static int l_set_timeout (lua_State *L)
{ {
nse_nsock_udata *nu = check_nsock_udata(L, 1, false); nse_nsock_udata *nu = check_nsock_udata(L, 1, false);
nu->timeout = luaL_checkinteger(L, 2); int timeout = nseU_checkinteger(L, 2);
if ((int) nu->timeout < -1) /* -1 is no timeout */ if (timeout < -1) /* -1 is no timeout */
return luaL_error(L, "Negative timeout: %d", nu->timeout); return luaL_error(L, "Negative timeout: %f", timeout);
nu->timeout = timeout;
return nseU_success(L); return nseU_success(L);
} }

View File

@@ -1,5 +1,6 @@
#include <stdlib.h> #include <stdlib.h>
#include <stdarg.h> #include <stdarg.h>
#include <math.h>
#include "Target.h" #include "Target.h"
#include "portlist.h" #include "portlist.h"
@@ -7,6 +8,16 @@
#include "nse_main.h" #include "nse_main.h"
#include "nse_utility.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) int nseU_traceback (lua_State *L)
{ {
if (lua_isstring(L, 1)) if (lua_isstring(L, 1))

View File

@@ -8,6 +8,12 @@ class Target;
#include <stdint.h> #include <stdint.h>
#endif #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) /* int nseU_traceback (lua_State *L)
* *
* Traceback C Lua function. * Traceback C Lua function.