[PATCH] lua: work around 32-bit integers limitation in vlc.var

Pierre Ynard linkfanel at yahoo.fr
Sun Nov 27 00:56:44 CET 2016


From: Pierre Ynard <linkfanel at yahoo.fr>

Integer object variables use 64 bits. If lua integers are limited to 32
bits, use floats when necessary.

Ref #17285

diff --git a/modules/lua/libs/variables.c b/modules/lua/libs/variables.c
index 9957a68..17728f7 100644
--- a/modules/lua/libs/variables.c
+++ b/modules/lua/libs/variables.c
@@ -50,7 +50,18 @@ static int vlclua_pushvalue( lua_State *L, int i_type, vlc_value_t val )
             lua_pushboolean( L, val.b_bool );
             break;
         case VLC_VAR_INTEGER:
-            lua_pushinteger( L, val.i_int );
+            /* Lua may only support 32-bit integers. If so, and the
+             * value requires a higher range, push it as a float. We
+             * lose some precision, but object variables are not a
+             * recommended API for lua scripts: functionality requiring
+             * high precision should be provided with a dedicated lua
+             * binding instead of object variables.
+             */
+            if( sizeof( lua_Integer ) < sizeof( val.i_int ) &&
+                ( val.i_int < INT32_MIN || INT32_MAX < val.i_int ) )
+                lua_pushnumber( L, (lua_Number)val.i_int );
+            else
+                lua_pushinteger( L, val.i_int );
             break;
         case VLC_VAR_STRING:
             lua_pushstring( L, val.psz_string );
@@ -93,7 +104,21 @@ static int vlclua_tovalue( lua_State *L, int i_type, vlc_value_t *val )
             val->b_bool = luaL_checkboolean( L, -1 );
             break;
         case VLC_VAR_INTEGER:
-            val->i_int = luaL_checkinteger( L, -1 );
+            /* Lua may only support 32-bit integers. If so, we need to
+             * get the value as a float instead so we can even know if
+             * there would be an overflow.
+             */
+            if( sizeof( lua_Integer ) < sizeof( val->i_int ) )
+            {
+                lua_Number f = luaL_checknumber( L, -1 );
+                // Calling vlc.var.set() on integer object variables with
+                // an out-of-range float value is not handled.
+                val->i_int = (int64_t)( f >= 0 ? f + 0.5 : f - 0.5 );
+                if( INT32_MIN < val->i_int && val->i_int < INT32_MAX )
+                    val->i_int = luaL_checkinteger( L, -1 );
+            }
+            else
+                val->i_int = luaL_checkinteger( L, -1 );
             break;
         case VLC_VAR_STRING:
             val->psz_string = (char*)luaL_checkstring( L, -1 ); /* XXX: Beware, this only stays valid as long as (L,-1) stays in the stack */


-- 
Pierre Ynard
"Une âme dans un corps, c'est comme un dessin sur une feuille de papier."


More information about the vlc-devel mailing list