From 843d53aabb1aa64ac55f95c1533ff29095ab585f Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Thu, 14 Feb 2008 14:03:27 -0200 Subject: [PATCH] 'table.concat' may get confused with too large limits --- ltablib.c | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/ltablib.c b/ltablib.c index 437270ad..e98188c4 100644 --- a/ltablib.c +++ b/ltablib.c @@ -1,5 +1,5 @@ /* -** $Id: ltablib.c,v 1.41 2007/09/12 20:53:24 roberto Exp roberto $ +** $Id: ltablib.c,v 1.42 2007/11/26 16:57:33 roberto Exp roberto $ ** Library for Table Manipulation ** See Copyright Notice in lua.h */ @@ -123,6 +123,15 @@ static int tremove (lua_State *L) { } +static void addfield (lua_State *L, luaL_Buffer *b, int i) { + lua_rawgeti(L, 1, i); + if (!lua_isstring(L, -1)) + luaL_error(L, "invalid value (%s) at index %d in table for " + LUA_QL("concat"), luaL_typename(L, -1), i); + luaL_addvalue(b); +} + + static int tconcat (lua_State *L) { luaL_Buffer b; size_t lsep; @@ -132,15 +141,12 @@ static int tconcat (lua_State *L) { i = luaL_optint(L, 3, 1); last = luaL_opt(L, luaL_checkint, 4, (int)lua_objlen(L, 1)); luaL_buffinit(L, &b); - for (; i <= last; i++) { - lua_rawgeti(L, 1, i); - if (!lua_isstring(L, -1)) - return luaL_error(L, "invalid value (%s) at index %d in table for " - LUA_QL("concat"), luaL_typename(L, -1), i); - luaL_addvalue(&b); - if (i != last) - luaL_addlstring(&b, sep, lsep); + for (; i < last; i++) { + addfield(L, &b, i); + luaL_addlstring(&b, sep, lsep); } + if (i == last) /* add last value (if interval was not empty) */ + addfield(L, &b, i); luaL_pushresult(&b); return 1; }