avoid subtle possibility of arithmetic overflow

This commit is contained in:
Roberto Ierusalimschy 2015-07-04 13:31:42 -03:00
parent 319ccfefbc
commit 8217e0d4fe
1 changed files with 5 additions and 5 deletions

View File

@ -1,5 +1,5 @@
/* /*
** $Id: ltablib.c,v 1.79 2014/11/02 19:19:04 roberto Exp roberto $ ** $Id: ltablib.c,v 1.80 2015/01/13 16:27:29 roberto Exp roberto $
** Library for Table Manipulation ** Library for Table Manipulation
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -215,10 +215,10 @@ static int unpack (lua_State *L) {
n = (lua_Unsigned)e - i; /* number of elements minus 1 (avoid overflows) */ n = (lua_Unsigned)e - i; /* number of elements minus 1 (avoid overflows) */
if (n >= (unsigned int)INT_MAX || !lua_checkstack(L, (int)(++n))) if (n >= (unsigned int)INT_MAX || !lua_checkstack(L, (int)(++n)))
return luaL_error(L, "too many results to unpack"); return luaL_error(L, "too many results to unpack");
do { /* must have at least one element */ for (; i < e; i++) { /* push arg[i..e - 1] (to avoid overflows) */
(*ta.geti)(L, 1, i); /* push arg[i..e] */ (*ta.geti)(L, 1, i);
} while (i++ < e); }
(*ta.geti)(L, 1, e); /* push last element */
return (int)n; return (int)n;
} }