aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>2014-04-03 10:29:24 -0300
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>2014-04-03 10:29:24 -0300
commit48c745a611888ab1e163cf184fbf5ae2ae743dad (patch)
tree0568adcb6febce673ed80450409361b6c0402999
parent3a044de5a1df82ed5d76f2c5afdf79677c92800f (diff)
downloadlua-48c745a611888ab1e163cf184fbf5ae2ae743dad.tar.gz
lua-48c745a611888ab1e163cf184fbf5ae2ae743dad.tar.bz2
lua-48c745a611888ab1e163cf184fbf5ae2ae743dad.zip
avoid constant overflow when shifting left signed integers until
their last bit
-rw-r--r--lstrlib.c10
1 files changed, 5 insertions, 5 deletions
diff --git a/lstrlib.c b/lstrlib.c
index 880c4519..a1b71c36 100644
--- a/lstrlib.c
+++ b/lstrlib.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lstrlib.c,v 1.190 2014/03/27 15:58:05 roberto Exp roberto $ 2** $Id: lstrlib.c,v 1.191 2014/03/31 18:38:26 roberto Exp roberto $
3** Standard library for string operations and pattern-matching 3** Standard library for string operations and pattern-matching
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -954,10 +954,10 @@ static int str_format (lua_State *L) {
954#define NB CHAR_BIT 954#define NB CHAR_BIT
955 955
956/* mask for one character (NB ones) */ 956/* mask for one character (NB ones) */
957#define MC (((lua_Integer)1 << NB) - 1) 957#define MC ((1 << NB) - 1)
958 958
959/* mask for one character without sign ((NB - 1) ones) */ 959/* mask for one character without sign ((NB - 1) ones) */
960#define SM (((lua_Integer)1 << (NB - 1)) - 1) 960#define SM ((1 << (NB - 1)) - 1)
961 961
962 962
963#define SZINT ((int)sizeof(lua_Integer)) 963#define SZINT ((int)sizeof(lua_Integer))
@@ -1007,7 +1007,7 @@ static int packint (char *buff, lua_Integer n, int littleendian, int size) {
1007 /* test for overflow: OK if there are only zeros left in higher bytes, 1007 /* test for overflow: OK if there are only zeros left in higher bytes,
1008 or if there are only ones left and packed number is negative (signal 1008 or if there are only ones left and packed number is negative (signal
1009 bit, the higher bit in last byte, is one) */ 1009 bit, the higher bit in last byte, is one) */
1010 return ((n & ~MC) == 0 || (n | SM) == ~(lua_Integer)0); 1010 return ((n & ~(lua_Integer)MC) == 0 || (n | SM) == ~(lua_Integer)0);
1011} 1011}
1012 1012
1013 1013
@@ -1025,7 +1025,7 @@ static int packint_l (lua_State *L) {
1025 1025
1026 1026
1027/* mask to check higher-order byte in a Lua integer */ 1027/* mask to check higher-order byte in a Lua integer */
1028#define HIGHERBYTE (MC << (NB * (SZINT - 1))) 1028#define HIGHERBYTE ((lua_Unsigned)MC << (NB * (SZINT - 1)))
1029 1029
1030/* mask to check higher-order byte + signal bit of next (lower) byte */ 1030/* mask to check higher-order byte + signal bit of next (lower) byte */
1031#define HIGHERBYTE1 (HIGHERBYTE | (HIGHERBYTE >> 1)) 1031#define HIGHERBYTE1 (HIGHERBYTE | (HIGHERBYTE >> 1))