From 130c0e40e01e24a79f7e416d1cc4187cd4a2a5fc Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Wed, 19 Jun 2013 11:27:00 -0300 Subject: [PATCH] new constant 'MAX_SIZE', distinct from 'MAX_SIZET', for sizes visible from Lua; these must fit in a lua_Integer --- llex.c | 4 ++-- llimits.h | 8 +++++++- lstring.c | 6 +++--- lvm.c | 4 ++-- 4 files changed, 14 insertions(+), 8 deletions(-) diff --git a/llex.c b/llex.c index b14c0257..920922eb 100644 --- a/llex.c +++ b/llex.c @@ -1,5 +1,5 @@ /* -** $Id: llex.c,v 2.65 2013/04/26 13:07:53 roberto Exp roberto $ +** $Id: llex.c,v 2.66 2013/05/14 15:59:04 roberto Exp roberto $ ** Lexical Analyzer ** See Copyright Notice in lua.h */ @@ -53,7 +53,7 @@ static void save (LexState *ls, int c) { Mbuffer *b = ls->buff; if (luaZ_bufflen(b) + 1 > luaZ_sizebuffer(b)) { size_t newsize; - if (luaZ_sizebuffer(b) >= MAX_SIZET/2) + if (luaZ_sizebuffer(b) >= MAX_SIZE/2) lexerror(ls, "lexical element too long", 0); newsize = luaZ_sizebuffer(b) * 2; luaZ_resizebuffer(ls->L, b, newsize); diff --git a/llimits.h b/llimits.h index 86af8cfb..4463e0c0 100644 --- a/llimits.h +++ b/llimits.h @@ -1,5 +1,5 @@ /* -** $Id: llimits.h,v 1.106 2013/05/27 12:43:37 roberto Exp roberto $ +** $Id: llimits.h,v 1.107 2013/05/29 14:04:15 roberto Exp roberto $ ** Limits, basic types, and some other `installation-dependent' definitions ** See Copyright Notice in lua.h */ @@ -27,8 +27,14 @@ typedef LUAI_MEM l_mem; typedef unsigned char lu_byte; +/* maximum value for size_t */ #define MAX_SIZET ((size_t)(~(size_t)0)-2) +/* maximum size visible for Lua (must be representable in a lua_Integer */ +#define MAX_SIZE (sizeof(size_t) <= sizeof(lua_Integer) ? MAX_SIZET \ + : (size_t)(~(lua_Unsigned)0)-2) + + #define MAX_LUMEM ((lu_mem)(~(lu_mem)0)-2) #define MAX_LMEM ((l_mem) ((MAX_LUMEM >> 1) - 2)) diff --git a/lstring.c b/lstring.c index 48fd1414..9809f006 100644 --- a/lstring.c +++ b/lstring.c @@ -1,5 +1,5 @@ /* -** $Id: lstring.c,v 2.25 2012/10/02 17:41:50 roberto Exp roberto $ +** $Id: lstring.c,v 2.26 2013/01/08 13:50:10 roberto Exp roberto $ ** String table (keeps all strings handled by Lua) ** See Copyright Notice in lua.h */ @@ -157,7 +157,7 @@ TString *luaS_newlstr (lua_State *L, const char *str, size_t l) { if (l <= LUAI_MAXSHORTLEN) /* short string? */ return internshrstr(L, str, l); else { - if (l + 1 > (MAX_SIZET - sizeof(TString))/sizeof(char)) + if (l + 1 > (MAX_SIZE - sizeof(TString))/sizeof(char)) luaM_toobig(L); return createstrobj(L, str, l, LUA_TLNGSTR, G(L)->seed, NULL); } @@ -174,7 +174,7 @@ TString *luaS_new (lua_State *L, const char *str) { Udata *luaS_newudata (lua_State *L, size_t s, Table *e) { Udata *u; - if (s > MAX_SIZET - sizeof(Udata)) + if (s > MAX_SIZE - sizeof(Udata)) luaM_toobig(L); u = &luaC_newobj(L, LUA_TUSERDATA, sizeof(Udata) + s, NULL, 0)->u; u->uv.len = s; diff --git a/lvm.c b/lvm.c index 5e01944c..d78fc039 100644 --- a/lvm.c +++ b/lvm.c @@ -1,5 +1,5 @@ /* -** $Id: lvm.c,v 2.172 2013/06/04 19:36:42 roberto Exp roberto $ +** $Id: lvm.c,v 2.173 2013/06/07 19:02:05 roberto Exp roberto $ ** Lua virtual machine ** See Copyright Notice in lua.h */ @@ -284,7 +284,7 @@ void luaV_concat (lua_State *L, int total) { /* collect total length */ for (i = 1; i < total && tostring(L, top-i-1); i++) { size_t l = tsvalue(top-i-1)->len; - if (l >= (MAX_SIZET/sizeof(char)) - tl) + if (l >= (MAX_SIZE/sizeof(char)) - tl) luaG_runerror(L, "string length overflow"); tl += l; }