Skip to content

Commit 4bc33d6

Browse files
committed
avoid overflows in computation of step size
1 parent 8821746 commit 4bc33d6

2 files changed

Lines changed: 12 additions & 3 deletions

File tree

lgc.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
** $Id: lgc.c,v 2.228 2017/05/04 13:32:01 roberto Exp roberto $
2+
** $Id: lgc.c,v 2.229 2017/05/26 19:14:29 roberto Exp roberto $
33
** Garbage Collector
44
** See Copyright Notice in lua.h
55
*/
@@ -1486,7 +1486,9 @@ void luaC_runtilstate (lua_State *L, int statesmask) {
14861486
static void incstep (lua_State *L, global_State *g) {
14871487
int stepmul = (g->gcstepmul | 1); /* avoid division by 0 */
14881488
l_mem debt = (g->GCdebt / WORK2MEM) * stepmul;
1489-
l_mem stepsize = cast(l_mem, 1) << g->gcstepsize;
1489+
l_mem stepsize = (g->gcstepsize <= log2maxs(l_mem))
1490+
? cast(l_mem, 1) << g->gcstepsize
1491+
: MAX_LMEM;
14901492
stepsize = -((stepsize / WORK2MEM) * stepmul);
14911493
do { /* repeat until pause or enough "credit" (negative debt) */
14921494
lu_mem work = singlestep(L); /* perform one single step */

llimits.h

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
** $Id: llimits.h,v 1.141 2015/11/19 19:16:22 roberto Exp roberto $
2+
** $Id: llimits.h,v 1.142 2017/04/24 18:06:12 roberto Exp roberto $
33
** Limits, basic types, and some other 'installation-dependent' definitions
44
** See Copyright Notice in lua.h
55
*/
@@ -51,6 +51,13 @@ typedef unsigned char lu_byte;
5151
#define MAX_INT INT_MAX /* maximum value of an int */
5252

5353

54+
/*
55+
** floor of the log2 of the maximum signed value for integral type 't'.
56+
** (That is, maximum 'n' such that '2^n' fits in the given signed type.)
57+
*/
58+
#define log2maxs(t) (sizeof(t) * 8 - 2)
59+
60+
5461
/*
5562
** conversion of pointer to unsigned integer:
5663
** this is for hashing only; there is no problem if the integer

0 commit comments

Comments
 (0)