Ensure the value returned from ast_random is between 0 and RAND_MAX on 64-bit platforms.
[asterisk/asterisk.git] / main / utils.c
index d40a749..5d64661 100644 (file)
@@ -1139,8 +1139,10 @@ long int ast_random(void)
        if (dev_urandom_fd >= 0) {
                int read_res = read(dev_urandom_fd, &res, sizeof(res));
                if (read_res > 0) {
+                       long int rm = RAND_MAX;
                        res = res < 0 ? ~res : res;
-                       return res;
+                       rm++;
+                       return res % rm;
                }
        }
 #endif