diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2013-05-06 14:22:16 -0300 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2013-05-06 14:22:16 -0300 |
commit | 1f2b82bf25e8893add740a0b4cdb9c54fc9f6053 (patch) | |
tree | 8a8c18f1180f90fd840107b3c0c77a2d6b852b57 /lcode.c | |
parent | f2043b7a589448990199f246a362c3df648157c7 (diff) | |
download | lua-1f2b82bf25e8893add740a0b4cdb9c54fc9f6053.tar.gz lua-1f2b82bf25e8893add740a0b4cdb9c54fc9f6053.tar.bz2 lua-1f2b82bf25e8893add740a0b4cdb9c54fc9f6053.zip |
correct way to avoid compile-time errors in integer divisions
Diffstat (limited to 'lcode.c')
-rw-r--r-- | lcode.c | 11 |
1 files changed, 7 insertions, 4 deletions
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: lcode.c,v 2.67 2013/04/29 16:57:48 roberto Exp roberto $ | 2 | ** $Id: lcode.c,v 2.68 2013/05/02 12:37:24 roberto Exp roberto $ |
3 | ** Code generator for Lua | 3 | ** Code generator for Lua |
4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
5 | */ | 5 | */ |
@@ -741,11 +741,14 @@ void luaK_indexed (FuncState *fs, expdesc *t, expdesc *k) { | |||
741 | 741 | ||
742 | static int constfolding (OpCode op, expdesc *e1, expdesc *e2) { | 742 | static int constfolding (OpCode op, expdesc *e1, expdesc *e2) { |
743 | TValue v1, v2, res; | 743 | TValue v1, v2, res; |
744 | lua_Integer i2; | 744 | lua_Integer i; |
745 | if (!tonumeral(e1, &v1) || !tonumeral(e2, &v2)) | 745 | if (!tonumeral(e1, &v1) || !tonumeral(e2, &v2)) |
746 | return 0; | 746 | return 0; |
747 | if ((op == OP_IDIV || op == OP_MOD) && tointeger(&v2, &i2) && i2 == 0) | 747 | if (op == OP_IDIV && |
748 | return 0; /* avoid division by 0 at compile time */ | 748 | (!tointeger(&v1, &i) || !tointeger(&v2, &i) || i == 0)) |
749 | return 0; /* avoid division by 0 and conversion errors */ | ||
750 | if (op == OP_MOD && ttisinteger(&v1) && ttisinteger(&v2) && ivalue(&v2) == 0) | ||
751 | return 0; /* avoid module by 0 at compile time */ | ||
749 | luaO_arith(NULL, op - OP_ADD + LUA_OPADD, &v1, &v2, &res); | 752 | luaO_arith(NULL, op - OP_ADD + LUA_OPADD, &v1, &v2, &res); |
750 | if (ttisinteger(&res)) { | 753 | if (ttisinteger(&res)) { |
751 | e1->k = VKINT; | 754 | e1->k = VKINT; |