aboutsummaryrefslogtreecommitdiff
path: root/lmem.c
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>2004-11-19 13:52:40 -0200
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>2004-11-19 13:52:40 -0200
commit6f1ea817f5827523f8c7e429ab023e5984a84343 (patch)
tree778c20b8816afb4ee5cfc3b988ec1c31efbb7b75 /lmem.c
parentcdcb236747a728e3ef0855aa16a42b73e7a9a6c6 (diff)
downloadlua-6f1ea817f5827523f8c7e429ab023e5984a84343.tar.gz
lua-6f1ea817f5827523f8c7e429ab023e5984a84343.tar.bz2
lua-6f1ea817f5827523f8c7e429ab023e5984a84343.zip
better control over memory-size overflows
Diffstat (limited to 'lmem.c')
-rw-r--r--lmem.c21
1 files changed, 13 insertions, 8 deletions
diff --git a/lmem.c b/lmem.c
index 5873e8dc..29252abe 100644
--- a/lmem.c
+++ b/lmem.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lmem.c,v 1.64 2004/04/30 20:13:38 roberto Exp roberto $ 2** $Id: lmem.c,v 1.65 2004/08/30 13:44:44 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*/
@@ -43,10 +43,12 @@
43#define MINSIZEARRAY 4 43#define MINSIZEARRAY 4
44 44
45 45
46void *luaM_growaux (lua_State *L, void *block, int *size, int size_elems, 46void *luaM_growaux (lua_State *L, void *block, int *size, size_t size_elems,
47 int limit, const char *errormsg) { 47 int limit, const char *errormsg) {
48 void *newblock; 48 void *newblock;
49 int newsize; 49 int newsize;
50 if (cast(size_t, limit) > MAX_SIZET/size_elems)
51 limit = cast(int, MAX_SIZET/size_elems);
50 if (*size >= limit/2) { /* cannot double it? */ 52 if (*size >= limit/2) { /* cannot double it? */
51 if (*size >= limit - MINSIZEARRAY) /* try something smaller... */ 53 if (*size >= limit - MINSIZEARRAY) /* try something smaller... */
52 luaG_runerror(L, errormsg); 54 luaG_runerror(L, errormsg);
@@ -57,22 +59,25 @@ void *luaM_growaux (lua_State *L, void *block, int *size, int size_elems,
57 if (newsize < MINSIZEARRAY) 59 if (newsize < MINSIZEARRAY)
58 newsize = MINSIZEARRAY; /* minimum size */ 60 newsize = MINSIZEARRAY; /* minimum size */
59 } 61 }
60 newblock = luaM_realloc(L, block, 62 newblock = luaM_reallocv(L, block, *size, newsize, size_elems);
61 cast(lu_mem, *size)*cast(lu_mem, size_elems),
62 cast(lu_mem, newsize)*cast(lu_mem, size_elems));
63 *size = newsize; /* update only when everything else is OK */ 63 *size = newsize; /* update only when everything else is OK */
64 return newblock; 64 return newblock;
65} 65}
66 66
67 67
68void *luaM_toobig (lua_State *L) {
69 luaG_runerror(L, "memory allocation error: block too big");
70 return NULL; /* to avoid warnings */
71}
72
73
74
68/* 75/*
69** generic allocation routine. 76** generic allocation routine.
70*/ 77*/
71void *luaM_realloc (lua_State *L, void *block, lu_mem osize, lu_mem nsize) { 78void *luaM_realloc (lua_State *L, void *block, size_t osize, size_t nsize) {
72 global_State *g = G(L); 79 global_State *g = G(L);
73 lua_assert((osize == 0) == (block == NULL)); 80 lua_assert((osize == 0) == (block == NULL));
74 if (nsize >= MAX_SIZET)
75 luaG_runerror(L, "memory allocation error: block too big");
76 block = (*g->realloc)(g->ud, block, osize, nsize); 81 block = (*g->realloc)(g->ud, block, osize, nsize);
77 if (block == NULL && nsize > 0) 82 if (block == NULL && nsize > 0)
78 luaD_throw(L, LUA_ERRMEM); 83 luaD_throw(L, LUA_ERRMEM);