mirror of https://github.com/rusefi/lua.git
correct way to avoid compile-time errors in integer divisions
This commit is contained in:
parent
f2043b7a58
commit
1f2b82bf25
11
lcode.c
11
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)
|
if (op == OP_IDIV &&
|
||||||
return 0; /* avoid division by 0 at compile time */
|
(!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;
|
||||||
|
|
Loading…
Reference in New Issue