diff --git a/src/include/switch_types.h b/src/include/switch_types.h index f8ae00790d..c4c9131bd6 100644 --- a/src/include/switch_types.h +++ b/src/include/switch_types.h @@ -599,6 +599,13 @@ SWITCH_DECLARE_DATA extern switch_filenames SWITCH_GLOBAL_filenames; #define SWITCH_ACCEPTABLE_INTERVAL(_i) (_i && _i <= SWITCH_MAX_INTERVAL && (_i % 10) == 0) +/* Check if RAND_MAX is a power of 2 minus 1 or in other words all bits set */ +#if ((RAND_MAX) & ((RAND_MAX) + 1)) == 0 && (RAND_MAX) != 0 +#define SWITCH_RAND_MAX RAND_MAX +#else +#define SWITCH_RAND_MAX 0x7fff +#endif + typedef enum { SWITCH_RW_READ, SWITCH_RW_WRITE diff --git a/src/switch_utils.c b/src/switch_utils.c index 64577d3997..90c5de059a 100644 --- a/src/switch_utils.c +++ b/src/switch_utils.c @@ -4835,8 +4835,8 @@ SWITCH_DECLARE(int) switch_rand(void) BCryptCloseAlgorithmProvider(hAlgorithm, 0); - /* Make sure we return from 0 to RAND_MAX */ - return (random_number & 0x7FFF); + /* Make sure we return from 0 to SWITCH_RAND_MAX */ + return (random_number & (SWITCH_RAND_MAX)); #elif defined(__unix__) || defined(__APPLE__) int random_fd = open("/dev/urandom", O_RDONLY); ssize_t result; @@ -4865,8 +4865,8 @@ SWITCH_DECLARE(int) switch_rand(void) close(random_fd); - /* Make sure we return from 0 to RAND_MAX */ - return (random_number & 0x7FFF); + /* Make sure we return from 0 to SWITCH_RAND_MAX */ + return (random_number & (SWITCH_RAND_MAX)); #else return rand(); #endif