aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorLibravatarLibravatar william <william@25tandclement.com> 2014-03-27 22:29:31 -0700
committerLibravatarLibravatar william <william@25tandclement.com> 2014-03-27 22:29:31 -0700
commitf61a39615ebffb61e8cca28d5dfafb6363da54ec (patch)
tree5f095744050c72227a1a28e362056b1d2c85e36a /src
parentb2ca9062bc95ff7bc822a330f1d9f597ec95b770 (diff)
downloadluaossl-f61a39615ebffb61e8cca28d5dfafb6363da54ec.tar.gz
luaossl-f61a39615ebffb61e8cca28d5dfafb6363da54ec.tar.bz2
luaossl-f61a39615ebffb61e8cca28d5dfafb6363da54ec.zip
support Lua 5.3 64-bit integer type in rand.uniform
Diffstat (limited to 'src')
-rw-r--r--src/openssl.c27
1 files changed, 18 insertions, 9 deletions
diff --git a/src/openssl.c b/src/openssl.c
index acd2454..7a03c97 100644
--- a/src/openssl.c
+++ b/src/openssl.c
@@ -4017,15 +4017,18 @@ static unsigned long long rand_llu(lua_State *L) {
* (https://groups.google.com/forum/message/raw?msg=sci.crypt/DMslf6tSrD8/rv9rk6oP3r4J)
*/
static int rand_uniform(lua_State *L) {
- if (lua_isnoneornil(L, 1)) {
- unsigned long long r = rand_llu(L);
-
- lua_pushnumber(L, r);
+ unsigned long long r;
- return 1;
+ if (lua_isnoneornil(L, 1)) {
+ r = rand_llu(L);
} else {
- unsigned long long N = luaL_checknumber(L, 1);
- unsigned long long r, m;
+ unsigned long long N, m;
+
+ if (sizeof (lua_Unsigned) >= sizeof r) {
+ N = luaL_checkunsigned(L, 1);
+ } else {
+ N = luaL_checknumber(L, 1);
+ }
luaL_argcheck(L, N > 1, 1, lua_pushfstring(L, "[0, %d): interval is empty", (int)N));
@@ -4035,10 +4038,16 @@ static int rand_uniform(lua_State *L) {
r = rand_llu(L);
} while (r < m);
- lua_pushnumber(L, (r % N));
+ r = r % N;
+ }
- return 1;
+ if (sizeof (lua_Unsigned) >= sizeof r) {
+ lua_pushunsigned(L, r);
+ } else {
+ lua_pushnumber(L, r);
}
+
+ return 1;
} /* rand_uniform() */