aboutsummaryrefslogtreecommitdiff
path: root/lmem.c
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>2004-12-01 13:46:18 -0200
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>2004-12-01 13:46:18 -0200
commit97e2dab1fb1b90f806eeb4da51bb74a2cdb6ca54 (patch)
tree197e23df4a3f31910b6269cf9cfd574caa2a318d /lmem.c
parent0ed85191270f8bbe3ef7c4f5f0466de89b00c9b5 (diff)
downloadlua-97e2dab1fb1b90f806eeb4da51bb74a2cdb6ca54.tar.gz
lua-97e2dab1fb1b90f806eeb4da51bb74a2cdb6ca54.tar.bz2
lua-97e2dab1fb1b90f806eeb4da51bb74a2cdb6ca54.zip
better control of overflows in size computations
Diffstat (limited to 'lmem.c')
-rw-r--r--lmem.c14
1 files changed, 6 insertions, 8 deletions
diff --git a/lmem.c b/lmem.c
index 29252abe..f2a70d0f 100644
--- a/lmem.c
+++ b/lmem.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lmem.c,v 1.65 2004/08/30 13:44:44 roberto Exp roberto $ 2** $Id: lmem.c,v 1.66 2004/11/19 15:52:40 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,16 +43,14 @@
43#define MINSIZEARRAY 4 43#define MINSIZEARRAY 4
44 44
45 45
46void *luaM_growaux (lua_State *L, void *block, int *size, size_t 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);
52 if (*size >= limit/2) { /* cannot double it? */ 50 if (*size >= limit/2) { /* cannot double it? */
53 if (*size >= limit - MINSIZEARRAY) /* try something smaller... */ 51 if (*size >= limit) /* cannot grow even a little? */
54 luaG_runerror(L, errormsg); 52 luaG_runerror(L, errormsg);
55 newsize = limit; /* still have at least MINSIZEARRAY free places */ 53 newsize = limit; /* still have at least one free place */
56 } 54 }
57 else { 55 else {
58 newsize = (*size)*2; 56 newsize = (*size)*2;
@@ -75,7 +73,7 @@ void *luaM_toobig (lua_State *L) {
75/* 73/*
76** generic allocation routine. 74** generic allocation routine.
77*/ 75*/
78void *luaM_realloc (lua_State *L, void *block, size_t osize, size_t nsize) { 76void *luaM_realloc_ (lua_State *L, void *block, size_t osize, size_t nsize) {
79 global_State *g = G(L); 77 global_State *g = G(L);
80 lua_assert((osize == 0) == (block == NULL)); 78 lua_assert((osize == 0) == (block == NULL));
81 block = (*g->realloc)(g->ud, block, osize, nsize); 79 block = (*g->realloc)(g->ud, block, osize, nsize);