mirror of
https://github.com/lua/lua
synced 2026-09-27 00:28:25 +03:00
Addition in math.random can overflow
To avoid complains from some tools, the addition when computing math.random(n,m), which is computed as n + random(0, m - n), should use unsigned integers.
This commit is contained in:
parent
cad5a4fdbb
commit
b0f3df16a4
1 changed files with 2 additions and 2 deletions
|
|
@ -593,8 +593,8 @@ static int math_random (lua_State *L) {
|
|||
/* random integer in the interval [low, up] */
|
||||
luaL_argcheck(L, low <= up, 1, "interval is empty");
|
||||
/* project random integer into the interval [0, up - low] */
|
||||
p = project(I2UInt(rv), (lua_Unsigned)up - (lua_Unsigned)low, state);
|
||||
lua_pushinteger(L, l_castU2S(p) + low);
|
||||
p = project(I2UInt(rv), l_castS2U(up) - l_castS2U(low), state);
|
||||
lua_pushinteger(L, l_castU2S(p + l_castS2U(low)));
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue