From 523a7e9b3e849312bce9dda2ce1a20ef1edcd2d4 Mon Sep 17 00:00:00 2001
From: Mark Lakes <mlakes@signalsciences.com>
Date: Wed, 7 Mar 2018 15:38:13 -0800
Subject: [PATCH] MINOR: lua: allow socket api settimeout to accept integers,
 float, and doubles

---
 src/hlua.c | 14 +++++++++++++-
 1 file changed, 13 insertions(+), 1 deletion(-)

diff --git a/src/hlua.c b/src/hlua.c
index 82924f5c..57ba4aa4 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>
@@ -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;
+	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)

