mailing list of musl libc
 help / color / mirror / code / Atom feed
From: Ed Schouten <ed@nuxi.nl>
To: musl@lists.openwall.com
Subject: jrand48(): missing cast to int32_t?
Date: Fri, 9 Dec 2016 07:16:42 +0100	[thread overview]
Message-ID: <CABh_MKm9w3uXtQx4zHS1mOLLFK7=RViX86+DNtYmrNFr_8C+qQ@mail.gmail.com> (raw)

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


             reply	other threads:[~2016-12-09  6:16 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-12-09  6:16 Ed Schouten [this message]
2016-12-09 17:05 ` Szabolcs Nagy

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='CABh_MKm9w3uXtQx4zHS1mOLLFK7=RViX86+DNtYmrNFr_8C+qQ@mail.gmail.com' \
    --to=ed@nuxi.nl \
    --cc=musl@lists.openwall.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).