diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2004-12-01 13:46:18 -0200 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2004-12-01 13:46:18 -0200 |
commit | 97e2dab1fb1b90f806eeb4da51bb74a2cdb6ca54 (patch) | |
tree | 197e23df4a3f31910b6269cf9cfd574caa2a318d | |
parent | 0ed85191270f8bbe3ef7c4f5f0466de89b00c9b5 (diff) | |
download | lua-97e2dab1fb1b90f806eeb4da51bb74a2cdb6ca54.tar.gz lua-97e2dab1fb1b90f806eeb4da51bb74a2cdb6ca54.tar.bz2 lua-97e2dab1fb1b90f806eeb4da51bb74a2cdb6ca54.zip |
better control of overflows in size computations
-rw-r--r-- | llex.c | 31 | ||||
-rw-r--r-- | lmem.c | 14 | ||||
-rw-r--r-- | lmem.h | 22 |
3 files changed, 35 insertions, 32 deletions
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: llex.c,v 2.4 2004/09/22 14:02:00 roberto Exp roberto $ | 2 | ** $Id: llex.c,v 2.5 2004/11/24 19:16:03 roberto Exp roberto $ |
3 | ** Lexical Analyzer | 3 | ** Lexical Analyzer |
4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
5 | */ | 5 | */ |
@@ -26,12 +26,6 @@ | |||
26 | #define next(ls) (ls->current = zgetc(ls->z)) | 26 | #define next(ls) (ls->current = zgetc(ls->z)) |
27 | 27 | ||
28 | 28 | ||
29 | #define save(ls,c) { \ | ||
30 | Mbuffer *b = ls->buff; \ | ||
31 | if (b->n + 1 > b->buffsize) \ | ||
32 | luaZ_resizebuffer(ls->L, b, ((b->buffsize*2) + LUA_MINBUFFER)); \ | ||
33 | b->buffer[b->n++] = cast(char, c); } | ||
34 | |||
35 | 29 | ||
36 | 30 | ||
37 | #define currIsNewline(ls) (ls->current == '\n' || ls->current == '\r') | 31 | #define currIsNewline(ls) (ls->current == '\n' || ls->current == '\r') |
@@ -48,6 +42,22 @@ static const char *const token2string [] = { | |||
48 | }; | 42 | }; |
49 | 43 | ||
50 | 44 | ||
45 | #define save_and_next(ls) (save(ls, ls->current), next(ls)) | ||
46 | |||
47 | |||
48 | static void save (LexState *ls, int c) { | ||
49 | Mbuffer *b = ls->buff; | ||
50 | if (b->n + 1 > b->buffsize) { | ||
51 | size_t newsize; | ||
52 | if (b->buffsize >= MAX_SIZET/2) | ||
53 | luaX_lexerror(ls, "lexical element too long", 0); | ||
54 | newsize = b->buffsize * 2; | ||
55 | luaZ_resizebuffer(ls->L, b, newsize); | ||
56 | } | ||
57 | b->buffer[b->n++] = cast(char, c); | ||
58 | } | ||
59 | |||
60 | |||
51 | void luaX_init (lua_State *L) { | 61 | void luaX_init (lua_State *L) { |
52 | int i; | 62 | int i; |
53 | for (i=0; i<NUM_RESERVED; i++) { | 63 | for (i=0; i<NUM_RESERVED; i++) { |
@@ -130,6 +140,7 @@ void luaX_setinput (lua_State *L, LexState *ls, ZIO *z, TString *source) { | |||
130 | ls->linenumber = 1; | 140 | ls->linenumber = 1; |
131 | ls->lastline = 1; | 141 | ls->lastline = 1; |
132 | ls->source = source; | 142 | ls->source = source; |
143 | luaZ_resizebuffer(ls->L, ls->buff, LUA_MINBUFFER); /* initialize buffer */ | ||
133 | next(ls); /* read first char */ | 144 | next(ls); /* read first char */ |
134 | } | 145 | } |
135 | 146 | ||
@@ -143,12 +154,6 @@ void luaX_setinput (lua_State *L, LexState *ls, ZIO *z, TString *source) { | |||
143 | 154 | ||
144 | 155 | ||
145 | 156 | ||
146 | static void save_and_next (LexState *ls) { | ||
147 | save(ls, ls->current); | ||
148 | next(ls); | ||
149 | } | ||
150 | |||
151 | |||
152 | 157 | ||
153 | /* LUA_NUMBER */ | 158 | /* LUA_NUMBER */ |
154 | static void read_numeral (LexState *ls, SemInfo *seminfo) { | 159 | static void read_numeral (LexState *ls, SemInfo *seminfo) { |
@@ -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 | ||
46 | void *luaM_growaux (lua_State *L, void *block, int *size, size_t size_elems, | 46 | void *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 | */ |
78 | void *luaM_realloc (lua_State *L, void *block, size_t osize, size_t nsize) { | 76 | void *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); |
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: lmem.h,v 1.27 2004/11/19 15:52:40 roberto Exp roberto $ | 2 | ** $Id: lmem.h,v 1.28 2004/11/24 19:20:21 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 | */ |
@@ -16,31 +16,31 @@ | |||
16 | #define MEMERRMSG "not enough memory" | 16 | #define MEMERRMSG "not enough memory" |
17 | 17 | ||
18 | 18 | ||
19 | void *luaM_realloc (lua_State *L, void *block, size_t oldsize, size_t size); | 19 | void *luaM_realloc_ (lua_State *L, void *block, size_t oldsize, size_t size); |
20 | 20 | ||
21 | void *luaM_toobig (lua_State *L); | 21 | void *luaM_toobig (lua_State *L); |
22 | 22 | ||
23 | #define luaM_reallocv(L,b,on,n,e) \ | 23 | #define luaM_reallocv(L,b,on,n,e) \ |
24 | ((cast(size_t, (n)+1) <= MAX_SIZET/(e)) ? /* +1 only to avoid warnings */ \ | 24 | ((cast(unsigned int, (n)+1) <= MAX_SIZET/(e)) ? /* +1 to avoid warnings */ \ |
25 | luaM_realloc(L, (b), (on)*(e), (n)*(e)) : \ | 25 | luaM_realloc_(L, (b), (on)*(e), (n)*(e)) : \ |
26 | luaM_toobig(L)) | 26 | luaM_toobig(L)) |
27 | 27 | ||
28 | 28 | ||
29 | void *luaM_growaux (lua_State *L, void *block, int *size, size_t size_elem, | 29 | void *luaM_growaux_ (lua_State *L, void *block, int *size, size_t size_elem, |
30 | int limit, const char *errormsg); | 30 | int limit, const char *errormsg); |
31 | 31 | ||
32 | #define luaM_freemem(L, b, s) luaM_realloc(L, (b), (s), 0) | 32 | #define luaM_freemem(L, b, s) luaM_realloc_(L, (b), (s), 0) |
33 | #define luaM_free(L, b) luaM_realloc(L, (b), sizeof(*(b)), 0) | 33 | #define luaM_free(L, b) luaM_realloc_(L, (b), sizeof(*(b)), 0) |
34 | #define luaM_freearray(L, b, n, t) luaM_reallocv(L, (b), n, 0, sizeof(t)) | 34 | #define luaM_freearray(L, b, n, t) luaM_reallocv(L, (b), n, 0, sizeof(t)) |
35 | 35 | ||
36 | #define luaM_malloc(L,t) luaM_realloc(L, NULL, 0, (t)) | 36 | #define luaM_malloc(L,t) luaM_realloc_(L, NULL, 0, (t)) |
37 | #define luaM_new(L,t) cast(t *, luaM_malloc(L, sizeof(t))) | 37 | #define luaM_new(L,t) cast(t *, luaM_malloc(L, sizeof(t))) |
38 | #define luaM_newvector(L,n,t) \ | 38 | #define luaM_newvector(L,n,t) \ |
39 | cast(t *, luaM_reallocv(L, NULL, 0, n, sizeof(t))) | 39 | cast(t *, luaM_reallocv(L, NULL, 0, n, sizeof(t))) |
40 | 40 | ||
41 | #define luaM_growvector(L,v,nelems,size,t,limit,e) \ | 41 | #define luaM_growvector(L,v,nelems,size,t,limit,e) \ |
42 | if (((nelems)+1) > (size)) \ | 42 | if ((nelems)+1 > (size)) \ |
43 | ((v)=cast(t *, luaM_growaux(L,v,&(size),sizeof(t),limit,e))) | 43 | ((v)=cast(t *, luaM_growaux_(L,v,&(size),sizeof(t),limit,e))) |
44 | 44 | ||
45 | #define luaM_reallocvector(L, v,oldn,n,t) \ | 45 | #define luaM_reallocvector(L, v,oldn,n,t) \ |
46 | ((v)=cast(t *, luaM_reallocv(L, v, oldn, n, sizeof(t)))) | 46 | ((v)=cast(t *, luaM_reallocv(L, v, oldn, n, sizeof(t)))) |