From 2e207e895f35aebb7afeb4d9bb0f3ad3a279dc56 Mon Sep 17 00:00:00 2001
From: Mark Lakes <mlakes@signalsciences.com>
Date: Mon, 29 Jan 2018 14:38:40 -0800
Subject: [PATCH] MINOR: lua: allow socket api settimeout to accept integers,
 float, and doubles

---
 src/hlua.c | 20 ++++++++++++++++----
 1 file changed, 16 insertions(+), 4 deletions(-)

diff --git a/src/hlua.c b/src/hlua.c
index 82924f5c..bcb2beb4 100644
--- a/src/hlua.c
+++ b/src/hlua.c
@@ -11,6 +11,7 @@
  */
 
 #include <ctype.h>
+#include <limits.h>
 #include <setjmp.h>
 
 #include <lauxlib.h>
@@ -277,7 +278,7 @@ __LJMP static inline void check_args(lua_State *L, int nb, char *fcn)
 	WILL_LJMP(luaL_error(L, "'%s' needs %d arguments", fcn, nb));
 }
 
-/* This fucntion push an error string prefixed by the file name
+/* This function pushes an error string prefixed by the file name
  * and the line number where the error is encountered.
  */
 static int hlua_pusherror(lua_State *L, const char *fmt, ...)
@@ -1843,7 +1844,7 @@ __LJMP static int hlua_socket_receive(struct lua_State *L)
 }
 
 /* Write the Lua input string in the output buffer.
- * This fucntion returns a yield if no space are available.
+ * This function returns a yield if no space is available.
  */
 static int hlua_socket_write_yield(struct lua_State *L,int status, lua_KContext ctx)
 {
@@ -2245,15 +2246,26 @@ __LJMP static int hlua_socket_setoption(struct lua_State *L)
 	return 0;
 }
 
+/* support timeout numbers of type integer, float and double */
 __LJMP static int hlua_socket_settimeout(struct lua_State *L)
 {
 	struct hlua_socket *socket;
-	int tmout;
+	int    tmout;
+	double dtmout;
 
 	MAY_LJMP(check_args(L, 2, "settimeout"));
 
 	socket = MAY_LJMP(hlua_checksocket(L, 1));
-	tmout = MAY_LJMP(luaL_checkinteger(L, 2)) * 1000;
+
+	/* round up for inputs that may be fractions and convert to millis */
+	dtmout = (0.5 + MAY_LJMP(luaL_checknumber(L, 2))) * 1000;
+	if (dtmout > INT_MAX) /* overflow check */
+		return 1;
+	tmout = MS_TO_TICKS((int)dtmout);
+	/* sanity check millisecs - cli_parse_set_timeout doesnt allow less than
+	   1 second so we wont here either */
+	if (tmout < 1000)
+		return 1;
 
 	socket->s->req.rto = tmout;
 	socket->s->req.wto = tmout;
-- 
2.14.3 (Apple Git-98)

