aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>2022-09-23 11:08:10 -0300
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>2022-09-23 11:08:10 -0300
commit26be27459b11feabed52cf40aaa76f86c7edc977 (patch)
tree9f3cd8be898991a52f6b5f8b140a12b8b9acb51d
parentcfbe378f906061ee56f91acfbdf569d0d3fb9556 (diff)
downloadlua-26be27459b11feabed52cf40aaa76f86c7edc977.tar.gz
lua-26be27459b11feabed52cf40aaa76f86c7edc977.tar.bz2
lua-26be27459b11feabed52cf40aaa76f86c7edc977.zip
Negation in constant folding of '>>' may overflow
-rw-r--r--lobject.c2
-rw-r--r--lvm.c4
-rw-r--r--lvm.h5
-rw-r--r--testes/bitwise.lua12
4 files changed, 19 insertions, 4 deletions
diff --git a/lobject.c b/lobject.c
index a2c00609..03e2798c 100644
--- a/lobject.c
+++ b/lobject.c
@@ -62,7 +62,7 @@ static lua_Integer intarith (lua_State *L, int op, lua_Integer v1,
62 case LUA_OPBOR: return intop(|, v1, v2); 62 case LUA_OPBOR: return intop(|, v1, v2);
63 case LUA_OPBXOR: return intop(^, v1, v2); 63 case LUA_OPBXOR: return intop(^, v1, v2);
64 case LUA_OPSHL: return luaV_shiftl(v1, v2); 64 case LUA_OPSHL: return luaV_shiftl(v1, v2);
65 case LUA_OPSHR: return luaV_shiftl(v1, -v2); 65 case LUA_OPSHR: return luaV_shiftr(v1, v2);
66 case LUA_OPUNM: return intop(-, 0, v1); 66 case LUA_OPUNM: return intop(-, 0, v1);
67 case LUA_OPBNOT: return intop(^, ~l_castS2U(0), v1); 67 case LUA_OPBNOT: return intop(^, ~l_castS2U(0), v1);
68 default: lua_assert(0); return 0; 68 default: lua_assert(0); return 0;
diff --git a/lvm.c b/lvm.c
index 614df055..73a19ba9 100644
--- a/lvm.c
+++ b/lvm.c
@@ -765,12 +765,10 @@ lua_Number luaV_modf (lua_State *L, lua_Number m, lua_Number n) {
765/* number of bits in an integer */ 765/* number of bits in an integer */
766#define NBITS cast_int(sizeof(lua_Integer) * CHAR_BIT) 766#define NBITS cast_int(sizeof(lua_Integer) * CHAR_BIT)
767 767
768
768/* 769/*
769** Shift left operation. (Shift right just negates 'y'.) 770** Shift left operation. (Shift right just negates 'y'.)
770*/ 771*/
771#define luaV_shiftr(x,y) luaV_shiftl(x,intop(-, 0, y))
772
773
774lua_Integer luaV_shiftl (lua_Integer x, lua_Integer y) { 772lua_Integer luaV_shiftl (lua_Integer x, lua_Integer y) {
775 if (y < 0) { /* shift right? */ 773 if (y < 0) { /* shift right? */
776 if (y <= -NBITS) return 0; 774 if (y <= -NBITS) return 0;
diff --git a/lvm.h b/lvm.h
index 1bc16f3a..dba1ad27 100644
--- a/lvm.h
+++ b/lvm.h
@@ -110,6 +110,11 @@ typedef enum {
110 luaC_barrierback(L, gcvalue(t), v); } 110 luaC_barrierback(L, gcvalue(t), v); }
111 111
112 112
113/*
114** Shift right is the same as shift left with a negative 'y'
115*/
116#define luaV_shiftr(x,y) luaV_shiftl(x,intop(-, 0, y))
117
113 118
114 119
115LUAI_FUNC int luaV_equalobj (lua_State *L, const TValue *t1, const TValue *t2); 120LUAI_FUNC int luaV_equalobj (lua_State *L, const TValue *t1, const TValue *t2);
diff --git a/testes/bitwise.lua b/testes/bitwise.lua
index 9509f7f0..dd0a1a9a 100644
--- a/testes/bitwise.lua
+++ b/testes/bitwise.lua
@@ -38,6 +38,18 @@ d = d << 32
38assert(a | b ~ c & d == 0xF4000000 << 32) 38assert(a | b ~ c & d == 0xF4000000 << 32)
39assert(~~a == a and ~a == -1 ~ a and -d == ~d + 1) 39assert(~~a == a and ~a == -1 ~ a and -d == ~d + 1)
40 40
41
42do -- constant folding
43 local code = string.format("return -1 >> %d", math.maxinteger)
44 assert(load(code)() == 0)
45 local code = string.format("return -1 >> %d", math.mininteger)
46 assert(load(code)() == 0)
47 local code = string.format("return -1 << %d", math.maxinteger)
48 assert(load(code)() == 0)
49 local code = string.format("return -1 << %d", math.mininteger)
50 assert(load(code)() == 0)
51end
52
41assert(-1 >> 1 == (1 << (numbits - 1)) - 1 and 1 << 31 == 0x80000000) 53assert(-1 >> 1 == (1 << (numbits - 1)) - 1 and 1 << 31 == 0x80000000)
42assert(-1 >> (numbits - 1) == 1) 54assert(-1 >> (numbits - 1) == 1)
43assert(-1 >> numbits == 0 and 55assert(-1 >> numbits == 0 and