Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cleanup: rand(), lrand48() and random() are POSIX 2001 #1423

Merged
merged 2 commits into from
Oct 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ AX_TYPE_SOCKLEN_T
AX_CREATE_STDINT_H([eggint.h])

# Checks for functions and their arguments.
AC_CHECK_FUNCS([dprintf explicit_bzero memset_explicit explicit_memset getrandom inet_aton isascii memset_s random rand lrand48 snprintf strlcpy vsnprintf])
AC_CHECK_FUNCS([dprintf explicit_bzero memset_explicit explicit_memset getrandom inet_aton isascii memset_s snprintf strlcpy vsnprintf])
AC_FUNC_SELECT_ARGTYPES
EGG_FUNC_B64_NTOP
AC_FUNC_MMAP
Expand Down
54 changes: 10 additions & 44 deletions src/eggdrop.h
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@

/* Yikes...who would have thought finding a usable random() would be so much
* trouble?
* Note: random(), rand(), and lrand48() are *not* thread safe.
* Note: random() is *not* thread safe.
*
* QNX doesn't include random() and srandom() in libc.so, only in libc.a
* So we can only use these functions in static builds on QNX.
Expand All @@ -213,49 +213,15 @@
# undef HAVE_SRANDOM
#endif

#ifdef HAVE_RANDOM
/* On systems with random(), RANDOM_MAX may or may not be defined.
*
* If RANDOM_MAX isn't defined, we use 0x7FFFFFFF (2^31-1), or 2147483647
* since this follows the 4.3BSD and POSIX.1-2001 standards. This of course
* assumes random() uses a 32 bit long int type per the standards.
*/
# ifndef RANDOM_MAX
# define RANDOM_MAX 0x7FFFFFFF /* random() -- 2^31-1 */
# endif
#else /* !HAVE_RANDOM */
/* This shouldn't exist in this case, but just to be safe... */
# ifdef RANDOM_MAX
# undef RANDOM_MAX
# endif
/* If we don't have random() it's safe to assume we also don't have
* srandom(), and we need both.
*/
# ifdef HAVE_RAND
# define random() rand()
# define srandom(x) srand(x)
/* Depending on the system int size, RAND_MAX can be either 0x7FFFFFFF
* (2^31-1), or 2147483647 for a 32 bit int, or 0x7FFF (2^15-1), or
* 32767 for a 16 bit int. The standards only state that RAND_MAX must
* be _at least_ 32767 but some systems with 16 bit int define it as
* 32767. See: SVr4, 4.3BSD, C89, C99, POSIX.1-2001.
*/
# define RANDOM_MAX RAND_MAX /* rand() -- 2^31-1 or 2^15-1 */
# else /* !HAVE_RAND */
# ifdef HAVE_LRAND48
# define random() lrand48()
# define srandom(x) srand48(x)
/* For lrand48() we define RANDOM_MAX as 0x7FFFFFFF (2^31-1), or
* 2147483647 since this is what the SVr4 and POSIX.1-2001 standards
* call for. Note: SVID 3 declares these functions as obsolete and
* states rand() should be used instead.
*/
# define RANDOM_MAX 0x7FFFFFFF /* lrand48() -- 2^31-1 */
# else /* !HAVE_LRAND48 */
# include "Error: Must define one of HAVE_RANDOM, HAVE_RAND, or HAVE_LRAND48"
# endif /* HAVE_LRAND48 */
# endif /* HAVE_RAND */
#endif /* HAVE_RANDOM */
/* On systems with random(), RANDOM_MAX may or may not be defined.
*
* If RANDOM_MAX isn't defined, we use 0x7FFFFFFF (2^31-1), or 2147483647
* since this follows the 4.3BSD and POSIX.1-2001 standards. This of course
* assumes random() uses a 32 bit long int type per the standards.
*/
#ifndef RANDOM_MAX
# define RANDOM_MAX 0x7FFFFFFF /* random() -- 2^31-1 */
#endif


/* Use high-order bits for getting the random integer. With a modern
Expand Down