From: Bruno Haible Date: Thu, 9 Nov 2023 15:00:28 +0000 (+0100) Subject: rand: New module. X-Git-Tag: v1.0~617 X-Git-Url: https://gitweb.git.savannah.gnu.org/gitweb/?a=commitdiff_plain;h=9d7322634e8e64aed2d53a67ab221b16f83aaabd;p=gnulib.git rand: New module. * lib/rand.c: New file, based on glibc/stdlib/rand.c. * m4/rand.m4: New file. * modules/rand: New file. * doc/posix-functions/rand.texi: Mention the new module. --- diff --git a/ChangeLog b/ChangeLog index 35a043ce7d..a6e6a8045a 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,11 @@ +2023-11-09 Bruno Haible + + rand: New module. + * lib/rand.c: New file, based on glibc/stdlib/rand.c. + * m4/rand.m4: New file. + * modules/rand: New file. + * doc/posix-functions/rand.texi: Mention the new module. + 2023-11-09 Bruno Haible random: Fix multithread-safety bug on CheriBSD. diff --git a/doc/posix-functions/rand.texi b/doc/posix-functions/rand.texi index 3d23c8d609..2f36a548a7 100644 --- a/doc/posix-functions/rand.texi +++ b/doc/posix-functions/rand.texi @@ -4,10 +4,13 @@ POSIX specification:@* @url{https://pubs.opengroup.org/onlinepubs/9699919799/functions/rand.html} -Gnulib module: --- +Gnulib module: rand Portability problems fixed by Gnulib: @itemize +@item +This function crashes when used in multithreaded programs on some platforms: +CheriBSD. @end itemize Portability problems not fixed by Gnulib: diff --git a/lib/rand.c b/lib/rand.c new file mode 100644 index 0000000000..86f76cb422 --- /dev/null +++ b/lib/rand.c @@ -0,0 +1,30 @@ +/* Copyright (C) 1991-2023 Free Software Foundation, Inc. + + This file is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as + published by the Free Software Foundation, either version 3 of the + License, or (at your option) any later version. + + This file is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with this program. If not, see . */ + +#include + +/* Specification. */ +#include + +#ifndef _LIBC +# define __random random +#endif + +/* Return a random integer between 0 and RAND_MAX. */ +int +rand (void) +{ + return (int) __random (); +} diff --git a/m4/rand.m4 b/m4/rand.m4 new file mode 100644 index 0000000000..50b7fa3333 --- /dev/null +++ b/m4/rand.m4 @@ -0,0 +1,17 @@ +# rand.m4 serial 1 +dnl Copyright (C) 2023 Free Software Foundation, Inc. +dnl This file is free software; the Free Software Foundation +dnl gives unlimited permission to copy and/or distribute it, +dnl with or without modifications, as long as this notice is preserved. + +AC_DEFUN([gl_FUNC_RAND], +[ + AC_REQUIRE([gl_STDLIB_H_DEFAULTS]) + AC_REQUIRE([AC_CANONICAL_HOST]) + + dnl On CheriBSD, rand() lacks locking, leading to an out-of-bounds read + dnl inside random_r. + case "$host" in + aarch64c-*-freebsd*) REPLACE_RAND=1 ;; + esac +]) diff --git a/modules/rand b/modules/rand new file mode 100644 index 0000000000..7cd6ca71c6 --- /dev/null +++ b/modules/rand @@ -0,0 +1,29 @@ +Description: +global random number generator + +Files: +lib/rand.c +m4/rand.m4 + +Depends-on: +stdlib +random [test $REPLACE_RAND = 1] + +configure.ac: +gl_FUNC_RAND +gl_CONDITIONAL([GL_COND_OBJ_RAND], [test $REPLACE_RAND = 1]) +gl_STDLIB_MODULE_INDICATOR([rand]) + +Makefile.am: +if GL_COND_OBJ_RAND +lib_SOURCES += rand.c +endif + +Include: + + +License: +LGPL + +Maintainer: +glibc