Skip to content

Commit 8efe9f2

Browse files
committed
BUG: compiler can optimize away overflow check in 'table.unpack'
1 parent a293779 commit 8efe9f2

File tree

1 file changed

+6
-4
lines changed

1 file changed

+6
-4
lines changed

ltablib.c

+6-4
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
/*
2-
** $Id: ltablib.c,v 1.65 2013/03/07 18:17:24 roberto Exp $
2+
** $Id: ltablib.c,v 1.65.1.1 2013/04/12 18:48:47 roberto Exp roberto $
33
** Library for Table Manipulation
44
** See Copyright Notice in lua.h
55
*/
66

77

8+
#include <limits.h>
89
#include <stddef.h>
910

1011
#define ltablib_c
@@ -134,13 +135,14 @@ static int pack (lua_State *L) {
134135

135136

136137
static int unpack (lua_State *L) {
137-
int i, e, n;
138+
int i, e;
139+
unsigned int n;
138140
luaL_checktype(L, 1, LUA_TTABLE);
139141
i = luaL_optint(L, 2, 1);
140142
e = luaL_opt(L, luaL_checkint, 3, luaL_len(L, 1));
141143
if (i > e) return 0; /* empty range */
142-
n = e - i + 1; /* number of elements */
143-
if (n <= 0 || !lua_checkstack(L, n)) /* n <= 0 means arith. overflow */
144+
n = (unsigned int)e - (unsigned int)i; /* number of elements minus 1 */
145+
if (n > (INT_MAX - 10) || !lua_checkstack(L, ++n))
144146
return luaL_error(L, "too many results to unpack");
145147
lua_rawgeti(L, 1, i); /* push arg[i] (avoiding overflow problems) */
146148
while (i++ < e) /* push arg[i + 1...e] */

0 commit comments

Comments
 (0)