]> git.ipfire.org Git - thirdparty/bind9.git/commitdiff
2633. [bug] Handle 15 bit rand() functions. [RT #19783]
authorMark Andrews <marka@isc.org>
Thu, 16 Jul 2009 05:58:45 +0000 (05:58 +0000)
committerMark Andrews <marka@isc.org>
Thu, 16 Jul 2009 05:58:45 +0000 (05:58 +0000)
CHANGES
lib/isc/random.c

diff --git a/CHANGES b/CHANGES
index ddabe3700294f5df27451868e502aaa9bc3cca88..6885521e08745d3516e527ccf4fdec6b3edfda15 100644 (file)
--- a/CHANGES
+++ b/CHANGES
@@ -1,3 +1,5 @@
+2633.  [bug]           Handle 15 bit rand() functions. [RT #19783]
+
 2632.  [func]          util/kit.sh: warn if documentation appears to be out of
                        date.  [RT #19922]
 
index 0329abde72b29bc4674b8eaa5a03c22ebdd061fe..5f541ca7416a90dc4f42269fa4cef4a251940d38 100644 (file)
@@ -15,7 +15,7 @@
  * PERFORMANCE OF THIS SOFTWARE.
  */
 
-/* $Id: random.c,v 1.25 2007/06/19 23:47:17 tbox Exp $ */
+/* $Id: random.c,v 1.25.332.1 2009/07/16 05:58:45 marka Exp $ */
 
 /*! \file */
 
@@ -84,7 +84,16 @@ isc_random_get(isc_uint32_t *val)
         * rand()'s lower bits are not random.
         * rand()'s upper bit is zero.
         */
+#if RAND_MAX >= 0xfffff
+       /* We have at least 20 bits.  Use lower 16 excluding lower most 4 */
        *val = ((rand() >> 4) & 0xffff) | ((rand() << 12) & 0xffff0000);
+#elif RAND_MAX >= 0x7fff
+       /* We have at least 15 bits.  Use lower 10/11 excluding lower most 4 */
+       *val = ((rand() >> 4) & 0x000007ff) | ((rand() << 7) & 0x003ff800) |
+               ((rand() << 18) & 0xffc00000);
+#else
+#error RAND_MAX is too small
+#endif
 #else
        *val = arc4random();
 #endif
@@ -92,13 +101,13 @@ isc_random_get(isc_uint32_t *val)
 
 isc_uint32_t
 isc_random_jitter(isc_uint32_t max, isc_uint32_t jitter) {
+       isc_uint32_t rnd;
+
        REQUIRE(jitter < max);
+
        if (jitter == 0)
                return (max);
-       else
-#ifndef HAVE_ARC4RANDOM
-               return (max - rand() % jitter);
-#else
-               return (max - arc4random() % jitter);
-#endif
+
+       isc_random_get(&rnd);
+       return (max - rnd % jitter);
 }