From owner-freebsd-current Sun Feb 2 6:16: 9 2003 Delivered-To: freebsd-current@freebsd.org Received: from mx1.FreeBSD.org (mx1.freebsd.org [216.136.204.125]) by hub.freebsd.org (Postfix) with ESMTP id A8BFC37B401; Sun, 2 Feb 2003 06:16:07 -0800 (PST) Received: from nagual.pp.ru (pobrecita.freebsd.ru [194.87.13.42]) by mx1.FreeBSD.org (Postfix) with ESMTP id 661BA43F3F; Sun, 2 Feb 2003 06:16:06 -0800 (PST) (envelope-from ache@pobrecita.freebsd.ru) Received: from pobrecita.freebsd.ru (ache@localhost [127.0.0.1]) by nagual.pp.ru (8.12.6/8.12.6) with ESMTP id h12EG5d6064181; Sun, 2 Feb 2003 17:16:05 +0300 (MSK) (envelope-from ache@pobrecita.freebsd.ru) Received: (from ache@localhost) by pobrecita.freebsd.ru (8.12.6/8.12.6/Submit) id h12EG5ro064180; Sun, 2 Feb 2003 17:16:05 +0300 (MSK) (envelope-from ache) Date: Sun, 2 Feb 2003 17:16:05 +0300 From: "Andrey A. Chernov" To: Tim Robbins Cc: Kris Kennaway , current@FreeBSD.ORG Subject: Re: rand() is broken Message-ID: <20030202141605.GA64157@nagual.pp.ru> References: <20030202070644.GA9987@rot13.obsecurity.org> <20030202090422.GA59750@nagual.pp.ru> <20030202091106.GA72723@rot13.obsecurity.org> <20030202102621.GA60900@nagual.pp.ru> <20030202123035.GB62977@nagual.pp.ru> <20030203001735.A30440@dilbert.robbins.dropbear.id.au> <20030202134225.GA63673@nagual.pp.ru> <20030202140223.GA63836@nagual.pp.ru> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20030202140223.GA63836@nagual.pp.ru> User-Agent: Mutt/1.5.1i Sender: owner-freebsd-current@FreeBSD.ORG Precedence: bulk List-ID: List-Archive: (Web Archive) List-Help: (List Instructions) List-Subscribe: List-Unsubscribe: X-Loop: FreeBSD.ORG On Sun, Feb 02, 2003 at 17:02:23 +0300, Andrey A. Chernov wrote: > > I'll produce and send it a bit later. Here it is. --- stdlib/rand.c.old Sat Jan 4 20:39:19 2003 +++ stdlib/rand.c Sun Feb 2 17:06:08 2003 @@ -72,10 +72,13 @@ */ long hi, lo, x; + /* Can't be initialized with 0, so use another value. */ + if (*ctx == 0) + *ctx = 123459876; hi = *ctx / 127773; lo = *ctx % 127773; x = 16807 * lo - 2836 * hi; - if (x <= 0) + if (x < 0) x += 0x7fffffff; return ((*ctx = x) % ((u_long)RAND_MAX + 1)); #endif /* !USE_WEAK_SEEDING */ @@ -86,8 +89,10 @@ rand_r(unsigned int *ctx) { u_long val = (u_long) *ctx; - *ctx = do_rand(&val); - return (int) *ctx; + int r = do_rand(&val); + + *ctx = (unsigned int) val; + return (r); } --- stdlib/random.c.old Sun Mar 24 23:42:48 2002 +++ stdlib/random.c Sun Feb 2 17:09:19 2003 @@ -236,10 +236,13 @@ */ long hi, lo; + /* Can't be initialized with 0, so use another value. */ + if (x == 0) + x = 123459876; hi = x / 127773; lo = x % 127773; x = 16807 * lo - 2836 * hi; - if (x <= 0) + if (x < 0) x += 0x7fffffff; return (x); #endif /* !USE_WEAK_SEEDING */ -- Andrey A. Chernov http://ache.pp.ru/ To Unsubscribe: send mail to majordomo@FreeBSD.org with "unsubscribe freebsd-current" in the body of the message