]> Savannah Git Hosting - gnulib.git/commitdiff
rand: New module.
authorBruno Haible <bruno@clisp.org>
Thu, 9 Nov 2023 15:00:28 +0000 (16:00 +0100)
committerBruno Haible <bruno@clisp.org>
Thu, 9 Nov 2023 15:00:28 +0000 (16:00 +0100)
* 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.

ChangeLog
doc/posix-functions/rand.texi
lib/rand.c [new file with mode: 0644]
m4/rand.m4 [new file with mode: 0644]
modules/rand [new file with mode: 0644]

index 35a043ce7d8890c97116c11b860f684d8558239c..a6e6a8045afeb99732fa24aa59a6d40e43fd2e13 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,11 @@
+2023-11-09  Bruno Haible  <bruno@clisp.org>
+
+       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  <bruno@clisp.org>
 
        random: Fix multithread-safety bug on CheriBSD.
index 3d23c8d60916fef3cc0843ce2361f9193216ca54..2f36a548a77e52697a83514a5ddcf2ad650d711f 100644 (file)
@@ -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 (file)
index 0000000..86f76cb
--- /dev/null
@@ -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 <https://www.gnu.org/licenses/>.  */
+
+#include <config.h>
+
+/* Specification.  */
+#include <stdlib.h>
+
+#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 (file)
index 0000000..50b7fa3
--- /dev/null
@@ -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 (file)
index 0000000..7cd6ca7
--- /dev/null
@@ -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:
+<stdlib.h>
+
+License:
+LGPL
+
+Maintainer:
+glibc