aboutsummaryrefslogtreecommitdiff
path: root/ltablib.c
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>2014-04-01 15:50:34 -0300
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>2014-04-01 15:50:34 -0300
commit315c5bf7b7ce65e8a12d1d6e9a5a027307a31b89 (patch)
treefd0bd8d6530df09354c5564b38f6a964612e6f55 /ltablib.c
parent607be77ec8d2b6062077772a55831a5aca16fb2d (diff)
downloadlua-315c5bf7b7ce65e8a12d1d6e9a5a027307a31b89.tar.gz
lua-315c5bf7b7ce65e8a12d1d6e9a5a027307a31b89.tar.bz2
lua-315c5bf7b7ce65e8a12d1d6e9a5a027307a31b89.zip
bug: compiler could optimize away overflow check (+ changing indices
from 'int' to 'lua_Integer')
Diffstat (limited to 'ltablib.c')
-rw-r--r--ltablib.c21
1 files changed, 12 insertions, 9 deletions
diff --git a/ltablib.c b/ltablib.c
index 09fe8491..9160b94d 100644
--- a/ltablib.c
+++ b/ltablib.c
@@ -1,10 +1,11 @@
1/* 1/*
2** $Id: ltablib.c,v 1.65 2013/03/07 18:17:24 roberto Exp roberto $ 2** $Id: ltablib.c,v 1.66 2014/03/21 13:52:33 roberto Exp roberto $
3** Library for Table Manipulation 3** Library for Table Manipulation
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
6 6
7 7
8#include <limits.h>
8#include <stddef.h> 9#include <stddef.h>
9 10
10#define ltablib_c 11#define ltablib_c
@@ -134,17 +135,19 @@ static int pack (lua_State *L) {
134 135
135 136
136static int unpack (lua_State *L) { 137static int unpack (lua_State *L) {
137 int i, e, n; 138 lua_Integer i, e;
139 lua_Unsigned n;
138 luaL_checktype(L, 1, LUA_TTABLE); 140 luaL_checktype(L, 1, LUA_TTABLE);
139 i = luaL_optint(L, 2, 1); 141 i = luaL_optinteger(L, 2, 1);
140 e = luaL_opt(L, luaL_checkint, 3, luaL_len(L, 1)); 142 e = luaL_opt(L, luaL_checkinteger, 3, luaL_len(L, 1));
141 if (i > e) return 0; /* empty range */ 143 if (i > e) return 0; /* empty range */
142 n = e - i + 1; /* number of elements */ 144 n = (lua_Unsigned)e - i; /* number of elements minus 1 (avoid overflows) */
143 if (n <= 0 || !lua_checkstack(L, n)) /* n <= 0 means arithmetic overflow */ 145 if (n >= (lua_Unsigned)INT_MAX || !lua_checkstack(L, ++n))
144 return luaL_error(L, "too many results to unpack"); 146 return luaL_error(L, "too many results to unpack");
145 lua_rawgeti(L, 1, i); /* push arg[i] (avoiding overflow problems) */ 147 do { /* must have at least one element */
146 while (i++ < e) /* push arg[i + 1...e] */ 148 lua_rawgeti(L, 1, i); /* push arg[i..e] */
147 lua_rawgeti(L, 1, i); 149 } while (i++ < e);
150
148 return n; 151 return n;
149} 152}
150 153