mirror of https://github.com/rusefi/lua.git
avoid overflow when doubling size
This commit is contained in:
parent
8b97b072cd
commit
da61624756
19
lmem.c
19
lmem.c
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
** $Id: lmem.c,v 1.61 2002/12/04 17:38:31 roberto Exp roberto $
|
** $Id: lmem.c,v 1.62 2003/10/02 20:31:17 roberto Exp roberto $
|
||||||
** Interface to Memory Manager
|
** Interface to Memory Manager
|
||||||
** See Copyright Notice in lua.h
|
** See Copyright Notice in lua.h
|
||||||
*/
|
*/
|
||||||
|
@ -45,13 +45,16 @@
|
||||||
void *luaM_growaux (lua_State *L, void *block, int *size, int size_elems,
|
void *luaM_growaux (lua_State *L, void *block, int *size, int size_elems,
|
||||||
int limit, const char *errormsg) {
|
int limit, const char *errormsg) {
|
||||||
void *newblock;
|
void *newblock;
|
||||||
int newsize = (*size)*2;
|
int newsize;
|
||||||
if (newsize < MINSIZEARRAY)
|
if (*size >= limit/2) { /* cannot double it? */
|
||||||
newsize = MINSIZEARRAY; /* minimum size */
|
if (*size >= limit - MINSIZEARRAY) /* try something smaller... */
|
||||||
else if (*size >= limit/2) { /* cannot double it? */
|
luaG_runerror(L, errormsg);
|
||||||
if (*size < limit - MINSIZEARRAY) /* try something smaller... */
|
newsize = limit; /* still have at least MINSIZEARRAY free places */
|
||||||
newsize = limit; /* still have at least MINSIZEARRAY free places */
|
}
|
||||||
else luaG_runerror(L, errormsg);
|
else {
|
||||||
|
newsize = (*size)*2;
|
||||||
|
if (newsize < MINSIZEARRAY)
|
||||||
|
newsize = MINSIZEARRAY; /* minimum size */
|
||||||
}
|
}
|
||||||
newblock = luaM_realloc(L, block,
|
newblock = luaM_realloc(L, block,
|
||||||
cast(lu_mem, *size)*cast(lu_mem, size_elems),
|
cast(lu_mem, *size)*cast(lu_mem, size_elems),
|
||||||
|
|
Loading…
Reference in New Issue