lua

A copy of the Lua development repository
Log | Files | Refs | README

commit f45177f2d325d76e0622109ddf4f534a05e721e8
parent d6f5fb2d2c5cfeab1e0aaddac7f121a909eb9408
Author: Roberto Ierusalimschy <roberto@inf.puc-rio.br>
Date:   Thu, 20 Jun 2013 14:37:06 -0300

In the table that hashes constants, use a light userdata as keys
to integer values to avoid collisions with floats with the same
numerical value

Diffstat:
Mlcode.c | 18+++++++++---------
1 file changed, 9 insertions(+), 9 deletions(-)

diff --git a/lcode.c b/lcode.c @@ -1,5 +1,5 @@ /* -** $Id: lcode.c,v 2.68 2013/05/02 12:37:24 roberto Exp roberto $ +** $Id: lcode.c,v 2.69 2013/05/06 17:22:16 roberto Exp roberto $ ** Code generator for Lua ** See Copyright Notice in lua.h */ @@ -333,9 +333,12 @@ int luaK_stringK (FuncState *fs, TString *s) { int luaK_intK (FuncState *fs, lua_Integer n) { - TValue o; + TValue k, o; + /* use userdata as key to avoid collision with float with same value; + conversion to 'void*' used only for hash, no "precision" problems */ + setpvalue(&k, cast(void*, cast(size_t, n))); setivalue(&o, n); - return addk(fs, &o, &o); + return addk(fs, &k, &o); } @@ -344,17 +347,14 @@ static int luaK_numberK (FuncState *fs, lua_Number r) { lua_State *L = fs->ls->L; TValue o; setnvalue(&o, r); - if (r == 0 || luai_numisnan(NULL, r)) { /* handle -0 and NaN */ + if (r != 0 && !luai_numisnan(NULL, r)) /* avoid -0 and NaN */ + n = addk(fs, &o, &o); /* regular case */ + else { /* handle -0 and NaN */ /* use raw representation as key to avoid numeric problems */ setsvalue(L, L->top++, luaS_newlstr(L, (char *)&r, sizeof(r))); n = addk(fs, L->top - 1, &o); L->top--; } - else { - TValue k; - setnvalue(&k, r + 0.5); /* ???? (avoid some collisions with ints) */ - n = addk(fs, &k, &o); /* regular case */ - } return n; }