lua-users home
lua-l archive

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


 Hello,

I have a following bug report for you:

Consider a big number

a=math.pow(2, 32) + 14  --4294967310 in dec, 10000000e in hex

print(tonumber("4294967310") -> 4294967310
print(tonumber("10000000e", 16) -> 4294967295 (0xffffffff, that is LONG_MAX)

tonumber() doesn't convert numbers greater than 2**31-1 from bases different than default decimal.
On my platform (Solaris 5.10, 32-bit x86 arch) lua_Number is 'double', 
i.e. 64-bit. I expect tonumber(x, 16) to work for all possible inputs 
that would fit in this type.
The bug is that luaB_tonumber() uses strtoul(3), which handles only 
'unsigned long' i.e. 32-bit values.

I see three ways out of this situation:
(a) use strtoull(3) (disadvantage: AFAIK that is not in C89)

(b) check errno for ERANGE and make a run-time error when it's set. (advantage: simple; disadvantage: doesn't make my program work, only shows the bug clearly)
(c) make our own re-implementation of strtoull(3), which works for all 
possible lengths of lua_Number.
--
Tadeusz Andrzej Kadłubowski
Dział Rozwoju Technologii
Wirtualna Polska S.A.


"WIRTUALNA POLSKA" Spolka Akcyjna z siedziba w Gdansku przy ul.
Traugutta 115 C, wpisana do Krajowego Rejestru Sadowego - Rejestru
Przedsiebiorcow prowadzonego przez Sad Rejonowy Gdansk - Polnoc w
Gdansku pod numerem KRS 0000068548, o kapitale zakladowym
67.980.024,00  zlotych oplaconym w calosci oraz Numerze Identyfikacji
Podatkowej 957-07-51-216.