From 1f2b82bf25e8893add740a0b4cdb9c54fc9f6053 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Mon, 6 May 2013 14:22:16 -0300 Subject: correct way to avoid compile-time errors in integer divisions --- lcode.c | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) (limited to 'lcode.c') diff --git a/lcode.c b/lcode.c index ded49b0e..ffb263f7 100644 --- a/lcode.c +++ b/lcode.c @@ -1,5 +1,5 @@ /* -** $Id: lcode.c,v 2.67 2013/04/29 16:57:48 roberto Exp roberto $ +** $Id: lcode.c,v 2.68 2013/05/02 12:37:24 roberto Exp roberto $ ** Code generator for Lua ** See Copyright Notice in lua.h */ @@ -741,11 +741,14 @@ void luaK_indexed (FuncState *fs, expdesc *t, expdesc *k) { static int constfolding (OpCode op, expdesc *e1, expdesc *e2) { TValue v1, v2, res; - lua_Integer i2; + lua_Integer i; if (!tonumeral(e1, &v1) || !tonumeral(e2, &v2)) return 0; - if ((op == OP_IDIV || op == OP_MOD) && tointeger(&v2, &i2) && i2 == 0) - return 0; /* avoid division by 0 at compile time */ + if (op == OP_IDIV && + (!tointeger(&v1, &i) || !tointeger(&v2, &i) || i == 0)) + return 0; /* avoid division by 0 and conversion errors */ + if (op == OP_MOD && ttisinteger(&v1) && ttisinteger(&v2) && ivalue(&v2) == 0) + return 0; /* avoid module by 0 at compile time */ luaO_arith(NULL, op - OP_ADD + LUA_OPADD, &v1, &v2, &res); if (ttisinteger(&res)) { e1->k = VKINT; -- cgit v1.2.3-55-g6feb