diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2025-03-31 13:44:41 -0300 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2025-03-31 13:44:41 -0300 |
commit | f4123b2fc2a662c08e3d7edc721241c251a22c4b (patch) | |
tree | f856da11d3134e1755bdb11192394dfd5c3595cd | |
parent | 37a1b72706b6e55e60b8d73bcbe269921976825e (diff) | |
download | lua-f4123b2fc2a662c08e3d7edc721241c251a22c4b.tar.gz lua-f4123b2fc2a662c08e3d7edc721241c251a22c4b.tar.bz2 lua-f4123b2fc2a662c08e3d7edc721241c251a22c4b.zip |
Growth factor of 1.5 for stack and lexical buffer
-rw-r--r-- | lauxlib.c | 14 | ||||
-rw-r--r-- | ldo.c | 2 | ||||
-rw-r--r-- | llex.c | 6 |
3 files changed, 11 insertions, 11 deletions
@@ -541,17 +541,17 @@ static void newbox (lua_State *L) { | |||
541 | 541 | ||
542 | /* | 542 | /* |
543 | ** Compute new size for buffer 'B', enough to accommodate extra 'sz' | 543 | ** Compute new size for buffer 'B', enough to accommodate extra 'sz' |
544 | ** bytes plus one for a terminating zero. (The test for "not big enough" | 544 | ** bytes plus one for a terminating zero. |
545 | ** also gets the case when the computation of 'newsize' overflows.) | ||
546 | */ | 545 | */ |
547 | static size_t newbuffsize (luaL_Buffer *B, size_t sz) { | 546 | static size_t newbuffsize (luaL_Buffer *B, size_t sz) { |
548 | size_t newsize = (B->size / 2) * 3; /* buffer size * 1.5 */ | 547 | size_t newsize = B->size; |
549 | if (l_unlikely(sz > MAX_SIZE - B->n - 1)) | 548 | if (l_unlikely(sz >= MAX_SIZE - B->n)) |
550 | return cast_sizet(luaL_error(B->L, "resulting string too large")); | 549 | return cast_sizet(luaL_error(B->L, "resulting string too large")); |
551 | if (newsize < B->n + sz + 1 || newsize > MAX_SIZE) { | 550 | /* else B->n + sz + 1 <= MAX_SIZE */ |
552 | /* newsize was not big enough or too big */ | 551 | if (newsize <= MAX_SIZE/3 * 2) /* no overflow? */ |
552 | newsize += (newsize >> 1); /* new size *= 1.5 */ | ||
553 | if (newsize < B->n + sz + 1) /* not big enough? */ | ||
553 | newsize = B->n + sz + 1; | 554 | newsize = B->n + sz + 1; |
554 | } | ||
555 | return newsize; | 555 | return newsize; |
556 | } | 556 | } |
557 | 557 | ||
@@ -319,7 +319,7 @@ int luaD_growstack (lua_State *L, int n, int raiseerror) { | |||
319 | return 0; /* if not 'raiseerror', just signal it */ | 319 | return 0; /* if not 'raiseerror', just signal it */ |
320 | } | 320 | } |
321 | else if (n < MAXSTACK) { /* avoids arithmetic overflows */ | 321 | else if (n < MAXSTACK) { /* avoids arithmetic overflows */ |
322 | int newsize = 2 * size; /* tentative new size */ | 322 | int newsize = size + (size >> 1); /* tentative new size (size * 1.5) */ |
323 | int needed = cast_int(L->top.p - L->stack.p) + n; | 323 | int needed = cast_int(L->top.p - L->stack.p) + n; |
324 | if (newsize > MAXSTACK) /* cannot cross the limit */ | 324 | if (newsize > MAXSTACK) /* cannot cross the limit */ |
325 | newsize = MAXSTACK; | 325 | newsize = MAXSTACK; |
@@ -62,10 +62,10 @@ static l_noret lexerror (LexState *ls, const char *msg, int token); | |||
62 | static void save (LexState *ls, int c) { | 62 | static void save (LexState *ls, int c) { |
63 | Mbuffer *b = ls->buff; | 63 | Mbuffer *b = ls->buff; |
64 | if (luaZ_bufflen(b) + 1 > luaZ_sizebuffer(b)) { | 64 | if (luaZ_bufflen(b) + 1 > luaZ_sizebuffer(b)) { |
65 | size_t newsize; | 65 | size_t newsize = luaZ_sizebuffer(b); /* get old size */; |
66 | if (luaZ_sizebuffer(b) >= MAX_SIZE/2) | 66 | if (newsize >= (MAX_SIZE/3 * 2)) /* larger than MAX_SIZE/1.5 ? */ |
67 | lexerror(ls, "lexical element too long", 0); | 67 | lexerror(ls, "lexical element too long", 0); |
68 | newsize = luaZ_sizebuffer(b) * 2; | 68 | newsize += (newsize >> 1); /* new size is 1.5 times the old one */ |
69 | luaZ_resizebuffer(ls->L, b, newsize); | 69 | luaZ_resizebuffer(ls->L, b, newsize); |
70 | } | 70 | } |
71 | b->buffer[luaZ_bufflen(b)++] = cast_char(c); | 71 | b->buffer[luaZ_bufflen(b)++] = cast_char(c); |