aboutsummaryrefslogtreecommitdiff
path: root/lmathlib.c
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>2025-03-25 11:43:03 -0300
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>2025-03-25 11:43:03 -0300
commitb0f3df16a495745cf16657a48dde6845ec85c732 (patch)
tree243436d6728e159af83ff376bda84eb065f4ed94 /lmathlib.c
parentcad5a4fdbb0f0843ec67596d1e472187decf1c88 (diff)
downloadlua-b0f3df16a495745cf16657a48dde6845ec85c732.tar.gz
lua-b0f3df16a495745cf16657a48dde6845ec85c732.tar.bz2
lua-b0f3df16a495745cf16657a48dde6845ec85c732.zip
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.
Diffstat (limited to 'lmathlib.c')
-rw-r--r--lmathlib.c4
1 files changed, 2 insertions, 2 deletions
diff --git a/lmathlib.c b/lmathlib.c
index c7418e69..a0981772 100644
--- a/lmathlib.c
+++ b/lmathlib.c
@@ -593,8 +593,8 @@ static int math_random (lua_State *L) {
593 /* random integer in the interval [low, up] */ 593 /* random integer in the interval [low, up] */
594 luaL_argcheck(L, low <= up, 1, "interval is empty"); 594 luaL_argcheck(L, low <= up, 1, "interval is empty");
595 /* project random integer into the interval [0, up - low] */ 595 /* project random integer into the interval [0, up - low] */
596 p = project(I2UInt(rv), (lua_Unsigned)up - (lua_Unsigned)low, state); 596 p = project(I2UInt(rv), l_castS2U(up) - l_castS2U(low), state);
597 lua_pushinteger(L, l_castU2S(p) + low); 597 lua_pushinteger(L, l_castU2S(p + l_castS2U(low)));
598 return 1; 598 return 1;
599} 599}
600 600