Merge pull request #2544 from signalwire/SWITCH_RAND_MAX

[Core] Introduce SWITCH_RAND_MAX to switch_rand()
This commit is contained in:
Andrey Volk 2024-07-29 22:52:09 +03:00 committed by GitHub
commit be3c0b3ef6
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 11 additions and 4 deletions

View File

@ -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

View File

@ -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