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 /llex.c | |
parent | 0ed85191270f8bbe3ef7c4f5f0466de89b00c9b5 (diff) | |
download | lua-97e2dab1fb1b90f806eeb4da51bb74a2cdb6ca54.tar.gz lua-97e2dab1fb1b90f806eeb4da51bb74a2cdb6ca54.tar.bz2 lua-97e2dab1fb1b90f806eeb4da51bb74a2cdb6ca54.zip |
better control of overflows in size computations
Diffstat (limited to 'llex.c')
-rw-r--r-- | llex.c | 31 |
1 files changed, 18 insertions, 13 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) { |