From c6653b592154423ad2482ef961495fb673bcd314 Mon Sep 17 00:00:00 2001 From: Javier Peletier Date: Tue, 15 Jan 2019 14:00:37 +0100 Subject: [PATCH] fix number2integer conversion in ESP8266 for floating-point builds (#2609) --- app/lua/luaconf.h | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/app/lua/luaconf.h b/app/lua/luaconf.h index 7ee4e41a..2c81c2ef 100644 --- a/app/lua/luaconf.h +++ b/app/lua/luaconf.h @@ -717,8 +717,18 @@ union luai_Cast { double l_d; long l_l; }; /* this option always works, but may be slow */ #else -#define lua_number2int(i,d) ((i)=(int)(d)) -#define lua_number2integer(i,d) ((i)=(lua_Integer)(d)) + +#ifdef LUA_NUMBER_INTEGRAL + +#define lua_number2int(i, d) ((i) = (int)(d)) +#define lua_number2integer(i, d) ((i) = (lua_Integer)(d)) + +#else // for floating-point builds, cast to a larger integer first to avoid undefined behavior on overflows. + +#define lua_number2int(i, d) ((i) = (int)(long long)(d)) +#define lua_number2integer(i, d) ((i) = (lua_Integer)(long long)(d)) + +#endif // LUA_NUMBER_INTEGRAL #endif @@ -900,4 +910,4 @@ union luai_Cast { double l_d; long l_l; }; #error "Pipes not supported in aggresive optimization mode (LUA_OPTIMIZE_MEMORY=2)" #endif -#endif +#endif \ No newline at end of file