aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>2003-11-27 16:18:37 -0200
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>2003-11-27 16:18:37 -0200
commitda61624756a9a64df4aa7e45e9f4013c1b76c293 (patch)
treeaa98e0f66cec95e3086d32e15f3143336d9fdfa0
parent8b97b072cd276ece00dc4a9957c0b07cf654fcdc (diff)
downloadlua-da61624756a9a64df4aa7e45e9f4013c1b76c293.tar.gz
lua-da61624756a9a64df4aa7e45e9f4013c1b76c293.tar.bz2
lua-da61624756a9a64df4aa7e45e9f4013c1b76c293.zip
avoid overflow when doubling size
-rw-r--r--lmem.c19
1 files changed, 11 insertions, 8 deletions
diff --git a/lmem.c b/lmem.c
index 39e19647..cd0766a6 100644
--- a/lmem.c
+++ b/lmem.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lmem.c,v 1.61 2002/12/04 17:38:31 roberto Exp roberto $ 2** $Id: lmem.c,v 1.62 2003/10/02 20:31:17 roberto Exp roberto $
3** Interface to Memory Manager 3** Interface to Memory Manager
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -45,13 +45,16 @@
45void *luaM_growaux (lua_State *L, void *block, int *size, int size_elems, 45void *luaM_growaux (lua_State *L, void *block, int *size, int size_elems,
46 int limit, const char *errormsg) { 46 int limit, const char *errormsg) {
47 void *newblock; 47 void *newblock;
48 int newsize = (*size)*2; 48 int newsize;
49 if (newsize < MINSIZEARRAY) 49 if (*size >= limit/2) { /* cannot double it? */
50 newsize = MINSIZEARRAY; /* minimum size */ 50 if (*size >= limit - MINSIZEARRAY) /* try something smaller... */
51 else if (*size >= limit/2) { /* cannot double it? */ 51 luaG_runerror(L, errormsg);
52 if (*size < limit - MINSIZEARRAY) /* try something smaller... */ 52 newsize = limit; /* still have at least MINSIZEARRAY free places */
53 newsize = limit; /* still have at least MINSIZEARRAY free places */ 53 }
54 else luaG_runerror(L, errormsg); 54 else {
55 newsize = (*size)*2;
56 if (newsize < MINSIZEARRAY)
57 newsize = MINSIZEARRAY; /* minimum size */
55 } 58 }
56 newblock = luaM_realloc(L, block, 59 newblock = luaM_realloc(L, block,
57 cast(lu_mem, *size)*cast(lu_mem, size_elems), 60 cast(lu_mem, *size)*cast(lu_mem, size_elems),