diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2015-07-13 10:30:03 -0300 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2015-07-13 10:30:03 -0300 |
commit | 2b61360d826355e996ec4b9b3af7778af9d84ada (patch) | |
tree | 200e66c4a412a7e414b5aed38a0dbc11f75f9c78 | |
parent | 5b6ac971f9223be8cbe44036fdd7c5e46389412d (diff) | |
download | lua-2b61360d826355e996ec4b9b3af7778af9d84ada.tar.gz lua-2b61360d826355e996ec4b9b3af7778af9d84ada.tar.bz2 lua-2b61360d826355e996ec4b9b3af7778af9d84ada.zip |
avoid overflows (detected with 'clang -ftrapv')
-rw-r--r-- | lgc.c | 11 | ||||
-rw-r--r-- | lstate.c | 10 |
2 files changed, 14 insertions, 7 deletions
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: lgc.c,v 2.204 2015/03/04 13:51:55 roberto Exp roberto $ | 2 | ** $Id: lgc.c,v 2.205 2015/03/25 13:42:19 roberto Exp roberto $ |
3 | ** Garbage Collector | 3 | ** Garbage Collector |
4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
5 | */ | 5 | */ |
@@ -1114,9 +1114,12 @@ void luaC_runtilstate (lua_State *L, int statesmask) { | |||
1114 | static l_mem getdebt (global_State *g) { | 1114 | static l_mem getdebt (global_State *g) { |
1115 | l_mem debt = g->GCdebt; | 1115 | l_mem debt = g->GCdebt; |
1116 | int stepmul = g->gcstepmul; | 1116 | int stepmul = g->gcstepmul; |
1117 | debt = (debt / STEPMULADJ) + 1; | 1117 | if (debt <= 0) return 0; /* minimal debt */ |
1118 | debt = (debt < MAX_LMEM / stepmul) ? debt * stepmul : MAX_LMEM; | 1118 | else { |
1119 | return debt; | 1119 | debt = (debt / STEPMULADJ) + 1; |
1120 | debt = (debt < MAX_LMEM / stepmul) ? debt * stepmul : MAX_LMEM; | ||
1121 | return debt; | ||
1122 | } | ||
1120 | } | 1123 | } |
1121 | 1124 | ||
1122 | /* | 1125 | /* |
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: lstate.c,v 2.127 2014/11/02 19:33:33 roberto Exp roberto $ | 2 | ** $Id: lstate.c,v 2.128 2015/03/04 13:31:21 roberto Exp roberto $ |
3 | ** Global State | 3 | ** Global State |
4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
5 | */ | 5 | */ |
@@ -93,10 +93,14 @@ static unsigned int makeseed (lua_State *L) { | |||
93 | 93 | ||
94 | /* | 94 | /* |
95 | ** set GCdebt to a new value keeping the value (totalbytes + GCdebt) | 95 | ** set GCdebt to a new value keeping the value (totalbytes + GCdebt) |
96 | ** invariant | 96 | ** invariant (and avoiding underflows in 'totalbytes') |
97 | */ | 97 | */ |
98 | void luaE_setdebt (global_State *g, l_mem debt) { | 98 | void luaE_setdebt (global_State *g, l_mem debt) { |
99 | g->totalbytes -= (debt - g->GCdebt); | 99 | l_mem tb = gettotalbytes(g); |
100 | lua_assert(tb > 0); | ||
101 | if (debt < tb - MAX_LMEM) | ||
102 | debt = tb - MAX_LMEM; /* will make 'totalbytes == MAX_LMEM' */ | ||
103 | g->totalbytes = tb - debt; | ||
100 | g->GCdebt = debt; | 104 | g->GCdebt = debt; |
101 | } | 105 | } |
102 | 106 | ||