From ca92cfd3622c52ab1b76ff780a66a169a74dc99c Mon Sep 17 00:00:00 2001 From: Javier Peletier Date: Tue, 15 Jan 2019 17:06:11 +0100 Subject: [PATCH] fix number2integer conversion for floating-point builds (#2605) --- components/lua/luaconf.h | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/components/lua/luaconf.h b/components/lua/luaconf.h index 67ac3503..e5cdf041 100644 --- a/components/lua/luaconf.h +++ b/components/lua/luaconf.h @@ -702,8 +702,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