* jrand48(): missing cast to int32_t?
@ 2016-12-09 6:16 Ed Schouten
2016-12-09 17:05 ` Szabolcs Nagy
0 siblings, 1 reply; 2+ messages in thread
From: Ed Schouten @ 2016-12-09 6:16 UTC (permalink / raw)
To: musl
Hi there,
According to POSIX, jrand48() is supposed to return a value between
[-2^31, 2^31). Musl's version of jrand48() is implemented as follows:
long jrand48(unsigned short s[3])
{
return __rand48_step(s, __seed48+3) >> 16;
}
Notice that the function only uses simple bit shifting and its return
type is long, meaning that on 64-bit systems, this implementation may
actually return a value between [0, 2^32).
For some reason, FreeBSD had the same bug. By adding a cast to
int32_t, its implementation now returns the same sequence of
pseudo-random numbers as macOS, Solaris, glibc, etc.
https://svnweb.freebsd.org/base?view=revision&revision=309650
Musl's version should likely be changed to this:
long jrand48(unsigned short s[3])
{
return (int32_t)(__rand48_step(s, __seed48+3) >> 16);
}
Best regards,
--
Ed Schouten <ed@nuxi.nl>
Nuxi, 's-Hertogenbosch, the Netherlands
KvK-nr.: 62051717
^ permalink raw reply [flat|nested] 2+ messages in thread
* Re: jrand48(): missing cast to int32_t?
2016-12-09 6:16 jrand48(): missing cast to int32_t? Ed Schouten
@ 2016-12-09 17:05 ` Szabolcs Nagy
0 siblings, 0 replies; 2+ messages in thread
From: Szabolcs Nagy @ 2016-12-09 17:05 UTC (permalink / raw)
To: Ed Schouten; +Cc: musl
* Ed Schouten <ed@nuxi.nl> [2016-12-09 07:16:42 +0100]:
> According to POSIX, jrand48() is supposed to return a value between
> [-2^31, 2^31). Musl's version of jrand48() is implemented as follows:
>
> long jrand48(unsigned short s[3])
> {
> return __rand48_step(s, __seed48+3) >> 16;
> }
nice catch, note that posix does not guarantee that
long can represent -2^31 so this requirement is not
possible to implement portably (indeed your proposed
cast has implementation defined effect, but in musl
we already rely on it to work as expected).
i submitted a posix bug report against limits.h
http://austingroupbugs.net/view.php?id=1108
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2016-12-09 17:05 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2016-12-09 6:16 jrand48(): missing cast to int32_t? Ed Schouten
2016-12-09 17:05 ` Szabolcs Nagy
Code repositories for project(s) associated with this public inbox
https://git.vuxu.org/mirror/musl/
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).