Author: ed
Date: Tue Dec 20 07:50:49 2016
New Revision: 310320
URL: https://svnweb.freebsd.org/changeset/base/310320

Log:
  MFC r309650:
  
    Properly sign extend the result of jrand48() and mrand48().
  
    These functions are supposed to return a value between [-2^31, 2^31).
    This doesn't seem to work on 64-bit systems, where we return a value
    between [0, 3^32). Patch up the function to use proper casts to int32_t.
    While there, fix some other style bugs.

Modified:
  stable/10/lib/libc/gen/jrand48.c
  stable/10/lib/libc/gen/mrand48.c
Directory Properties:
  stable/10/   (props changed)

Modified: stable/10/lib/libc/gen/jrand48.c
==============================================================================
--- stable/10/lib/libc/gen/jrand48.c    Tue Dec 20 07:42:15 2016        
(r310319)
+++ stable/10/lib/libc/gen/jrand48.c    Tue Dec 20 07:50:49 2016        
(r310320)
@@ -14,11 +14,14 @@
 #include <sys/cdefs.h>
 __FBSDID("$FreeBSD$");
 
+#include <stdint.h>
+
 #include "rand48.h"
 
 long
 jrand48(unsigned short xseed[3])
 {
+
        _dorand48(xseed);
-       return ((long) xseed[2] << 16) + (long) xseed[1];
+       return ((int32_t)(((uint32_t)xseed[2] << 16) | (uint32_t)xseed[1]));
 }

Modified: stable/10/lib/libc/gen/mrand48.c
==============================================================================
--- stable/10/lib/libc/gen/mrand48.c    Tue Dec 20 07:42:15 2016        
(r310319)
+++ stable/10/lib/libc/gen/mrand48.c    Tue Dec 20 07:50:49 2016        
(r310320)
@@ -14,6 +14,8 @@
 #include <sys/cdefs.h>
 __FBSDID("$FreeBSD$");
 
+#include <stdint.h>
+
 #include "rand48.h"
 
 extern unsigned short _rand48_seed[3];
@@ -21,6 +23,8 @@ extern unsigned short _rand48_seed[3];
 long
 mrand48(void)
 {
+
        _dorand48(_rand48_seed);
-       return ((long) _rand48_seed[2] << 16) + (long) _rand48_seed[1];
+       return ((int32_t)(((uint32_t)_rand48_seed[2] << 16) |
+           (uint32_t)_rand48_seed[1]));
 }
_______________________________________________
svn-src-stable-10@freebsd.org mailing list
https://lists.freebsd.org/mailman/listinfo/svn-src-stable-10
To unsubscribe, send any mail to "svn-src-stable-10-unsubscr...@freebsd.org"

Reply via email to