From mboxrd@z Thu Jan 1 00:00:00 1970 X-Msuck: nntp://news.gmane.org/gmane.linux.lib.musl.general/10786 Path: news.gmane.org!.POSTED!not-for-mail From: Ed Schouten Newsgroups: gmane.linux.lib.musl.general Subject: jrand48(): missing cast to int32_t? Date: Fri, 9 Dec 2016 07:16:42 +0100 Message-ID: Reply-To: musl@lists.openwall.com NNTP-Posting-Host: blaine.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 X-Trace: blaine.gmane.org 1481264272 7832 195.159.176.226 (9 Dec 2016 06:17:52 GMT) X-Complaints-To: usenet@blaine.gmane.org NNTP-Posting-Date: Fri, 9 Dec 2016 06:17:52 +0000 (UTC) To: musl@lists.openwall.com Original-X-From: musl-return-10799-gllmg-musl=m.gmane.org@lists.openwall.com Fri Dec 09 07:17:48 2016 Return-path: Envelope-to: gllmg-musl@m.gmane.org Original-Received: from mother.openwall.net ([195.42.179.200]) by blaine.gmane.org with smtp (Exim 4.84_2) (envelope-from ) id 1cFEV8-00013b-QI for gllmg-musl@m.gmane.org; Fri, 09 Dec 2016 07:17:47 +0100 Original-Received: (qmail 32204 invoked by uid 550); 9 Dec 2016 06:17:49 -0000 Mailing-List: contact musl-help@lists.openwall.com; run by ezmlm Precedence: bulk List-Post: List-Help: List-Unsubscribe: List-Subscribe: List-ID: Original-Received: (qmail 32115 invoked from network); 9 Dec 2016 06:17:24 -0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=nuxi-nl.20150623.gappssmtp.com; s=20150623; h=mime-version:from:date:message-id:subject:to; bh=hXwM1fAoJAVoPO5JBcfNTRBqrppxItrhAMqqxYu7kRE=; b=BWPtBU7EVdeqN6hpFYihsCts8H5wrYuk/3fV4HWRJymGtRiA5Gta/RUGo+iZzUJpDU CBq1Mljwr2sCaeTDrgo7CZnkkmYv2Bxih1q7nOiZl2PKxCrywlp74/UzEMul5D/+Y53g gbCjARsfQTqBiUJK+nPRVbzYwy18rqf+qt+Y9JLcDNLAJ7lvyITEJukqHgaJpLlb090b Xd641GogyzvpG/2aZgbfJihOvWeIQ3XIShS7O4VaB1lb8fYQERcHDbKOcUMxetbOvo/7 oqnVbkRz62Yd2erDnp4/Qb2zgUkS60DPkC/zqKqL+c3iYa9+ZdxXmM5V9LYkQ8B8DYRO vr7g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:from:date:message-id:subject:to; bh=hXwM1fAoJAVoPO5JBcfNTRBqrppxItrhAMqqxYu7kRE=; b=mvxhUKwgGTKsdXYrY2C8HI2kSm7THzA9W8kE+mA6oPK0IyohNjKPBvtU4WbID56PCN KLrksfVqHzxCoYHfNxlz5XUOZpdpA3rgSzH1dd8kxAYaeIceihcsnJvna74Ta1N735EJ EfGNba1Jvw/0gCp4j69eH0nXPI1lF+CGHPoCU3Z3QRZlsNdXwKLM45QFWNiser5418zj j9wrawk48ZAwhh7LXcc0o8S9/8j34cfH+BViEo0vuN8blN3hVQhdiTSO8yuWhP7nuDwN E1s11D2IKUttrkL5PTgoKSUVG8lbyZ8Gg3IRIgzjddiw0FRyymyDAmGJVONruiJN0Lxt PDTw== X-Gm-Message-State: AKaTC03XHaDsdjA9149LotMzjJwE4olp+DpMo3wizA8u/b/qYJjEvZIeVLfYIt8iAc79rx7ZTs8mhx37bEPlUQ== X-Received: by 10.37.210.197 with SMTP id j188mr42688446ybg.168.1481264232408; Thu, 08 Dec 2016 22:17:12 -0800 (PST) Xref: news.gmane.org gmane.linux.lib.musl.general:10786 Archived-At: 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 Nuxi, 's-Hertogenbosch, the Netherlands KvK-nr.: 62051717