lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


While upgrading my codebase and patch files to the latest Lua 5.3 working version I encountered a number of 'implicit conversion loses integer precision' warnings. I am running OSX 10.9.2 and am only targeting 64-bit versions of OSX. I detail the warnings I encountered, how I fixed the warning, and have attached patch files for these fixes.



The first set of warnings are in the 'tconcat' (line 100) and 'unpack' (line 140) functions in 'ltablib.c', as well as the 'gctm' (line 276) function in 'loadlib.c'.

luaL_len() returns lua_Integer which is being assigned to an int variable in both functions. Additionally, the 'aux_getn' macro also calls luaL_len() and assigns the result to an int everywhere it is used.

My solution for this issue was to create a new macro called luaL_lenint() which is the same as the other '*int' macros, it casts the result of luaL_len() to int from lua_Integer. I then updated the 'aux_getn' macro and the three functions to use this new macro.

The patch file for this fix is attached and is titled 'fix-64bit-lenint.patch'.

Attachment: fix-64bit-lenint.patch
Description: Binary data


The second set of warnings are due to some variables not being assigned the correct type.

In the 'iter_aux' (line 190) function in 'lutf8lib.c' the variable 'n' is assigned the int type. However, throughout the function 'n' is being compared with 'lua_Integer'. My solution for this issue is to define 'n' as 'lua_Integer' instead of 'int'.

In the 'checkliteral' (line 206) function in 'lundump.c' the variable 'len' is assigned the int type. However, the value of 'len' should be 'size_t' as that is what all functions 'len' is being passed to expect. My solution for this issue is to define 'len' as 'size_t' instead of 'int'.

The patch file for this fix is attached and is titled 'fix-64bit-vartype.patch'.

Attachment: fix-64bit-vartype.patch
Description: Binary data


The last set of warnings are easily fixed with some explicit casts.

In the 'addk' (line 320) function in 'lcode.c' the variable 'k', an int, is being assigned the result of ivalue(), which is lua_Integer. As 'k' is the constant number it can not exceed the size of Ax, which has a 32-bit max size, my solution was to cast the result of ivalue() to 'int'.

In the 'DumpString' (line 76) function in 'ldump.c' the DumpByte() function is called passing in 'size', defined as 'size_t' which can be 64-bit. The compiler produces a warning when we pass 'size' to DumpByte() on a 64-bit platform, therefore I simply added a cast to 'int' to suppress the warning.

In the 'math_randomseed' (line 248) function in 'lmathlib.c' a warning is generated because lua_Integer is being passed to srand() which expects a smaller data type. I added a cast to 'unsigned int' to suppress the warning.

In the 'luaH_newkey' (lines 446, 457, 459) in 'ltable.c' assignments are being made using the gnext() macro. The assignments are the results of pointer math, which on a 64-bit platform will produce the 'implicit conversion' warning. I added three specific 'int' casts to suppress these warnings.

The patch file for this fix is attached and is titled 'fix-64bit-casts.patch'.

Attachment: fix-64bit-casts.patch
Description: Binary data


I hope these patches are useful, especially for those using 64-bit systems. Please let me know if I have made any mistakes, or if another way to correct the warnings is used! :)

~Paige