浏览代码

correct way to avoid compile-time errors in integer divisions

Roberto Ierusalimschy 12 年之前
父节点
当前提交
1f2b82bf25
共有 1 个文件被更改,包括 7 次插入4 次删除
  1. 7 4
      lcode.c

+ 7 - 4
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
 ** Code generator for Lua
 ** See Copyright Notice in lua.h
 ** 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) {
 static int constfolding (OpCode op, expdesc *e1, expdesc *e2) {
   TValue v1, v2, res;
   TValue v1, v2, res;
-  lua_Integer i2;
+  lua_Integer i;
   if (!tonumeral(e1, &v1) || !tonumeral(e2, &v2))
   if (!tonumeral(e1, &v1) || !tonumeral(e2, &v2))
     return 0;
     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);
   luaO_arith(NULL, op - OP_ADD + LUA_OPADD, &v1, &v2, &res);
   if (ttisinteger(&res)) {
   if (ttisinteger(&res)) {
     e1->k = VKINT;
     e1->k = VKINT;