From 8217e0d4fe4cf80c52328230614ab41cf32f4afe Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Sat, 4 Jul 2015 13:31:42 -0300 Subject: [PATCH] avoid subtle possibility of arithmetic overflow --- ltablib.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/ltablib.c b/ltablib.c index 5bf2806d..af9bd9c6 100644 --- a/ltablib.c +++ b/ltablib.c @@ -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 ** 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) */ if (n >= (unsigned int)INT_MAX || !lua_checkstack(L, (int)(++n))) return luaL_error(L, "too many results to unpack"); - do { /* must have at least one element */ - (*ta.geti)(L, 1, i); /* push arg[i..e] */ - } while (i++ < e); - + for (; i < e; i++) { /* push arg[i..e - 1] (to avoid overflows) */ + (*ta.geti)(L, 1, i); + } + (*ta.geti)(L, 1, e); /* push last element */ return (int)n; }