diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2017-05-19 09:57:10 -0300 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2017-05-19 09:57:10 -0300 |
commit | e3d52da144dcb1179988ba315a6cb6703fe9a4f8 (patch) | |
tree | 61007e08765c1e69d055702804c8ab2e75249f07 /ltable.c | |
parent | e39ee2cc5841eff37f728ceeffd90cd748180f57 (diff) | |
download | lua-github-e3d52da144dcb1179988ba315a6cb6703fe9a4f8.tar.gz |
BUG: in 'computesizes', 'twotoi' overflows when a sequence has
more than 2^30 elements.
Diffstat (limited to 'ltable.c')
-rw-r--r-- | ltable.c | 9 |
1 files changed, 6 insertions, 3 deletions
@@ -1,5 +1,5 @@ /* -** $Id: ltable.c,v 2.120 2017/05/16 19:07:08 roberto Exp roberto $ +** $Id: ltable.c,v 2.121 2017/05/19 12:47:00 roberto Exp roberto $ ** Lua tables (hash) ** See Copyright Notice in lua.h */ @@ -214,7 +214,8 @@ int luaH_next (lua_State *L, Table *t, StkId key) { ** "count array" where 'nums[i]' is the number of integers in the table ** between 2^(i - 1) + 1 and 2^i. 'pna' enters with the total number of ** integer keys in the table and leaves with the number of keys that -** will go to the array part; return the optimal size. +** will go to the array part; return the optimal size. (The condition +** 'twotoi > 0' in the for loop stops the loop if 'twotoi' overflows.) */ static unsigned int computesizes (unsigned int nums[], unsigned int *pna) { int i; @@ -223,7 +224,9 @@ static unsigned int computesizes (unsigned int nums[], unsigned int *pna) { unsigned int na = 0; /* number of elements to go to array part */ unsigned int optimal = 0; /* optimal size for array part */ /* loop while keys can fill more than half of total size */ - for (i = 0, twotoi = 1; *pna > twotoi / 2; i++, twotoi *= 2) { + for (i = 0, twotoi = 1; + twotoi > 0 && *pna > twotoi / 2; + i++, twotoi *= 2) { a += nums[i]; if (a > twotoi/2) { /* more than half elements present? */ optimal = twotoi; /* optimal size (till now) */ |