From khera@kcilink.com  Thu May 19 18:42:11 2005
Return-Path: <khera@kcilink.com>
Received: from mx1.FreeBSD.org (mx1.freebsd.org [216.136.204.125])
	by hub.freebsd.org (Postfix) with ESMTP id ADE7616A4CE
	for <FreeBSD-gnats-submit@freebsd.org>; Thu, 19 May 2005 18:42:11 +0000 (GMT)
Received: from yertle.kcilink.com (yertle.kcilink.com [65.205.34.180])
	by mx1.FreeBSD.org (Postfix) with ESMTP id 3D5EB43DCC
	for <FreeBSD-gnats-submit@freebsd.org>; Thu, 19 May 2005 18:42:11 +0000 (GMT)
	(envelope-from khera@kcilink.com)
Received: by yertle.kcilink.com (Postfix, from userid 1002)
	id 34A5EB864; Thu, 19 May 2005 14:42:10 -0400 (EDT)
Message-Id: <20050519184210.34A5EB864@yertle.kcilink.com>
Date: Thu, 19 May 2005 14:42:10 -0400 (EDT)
From: Vivek Khera <vivek@khera.org>
Reply-To: Vivek Khera <vivek@khera.org>
To: FreeBSD-gnats-submit@freebsd.org
Cc:
Subject: /usr/games/random returns every line
X-Send-Pr-Version: 3.113
X-GNATS-Notify:

>Number:         81279
>Category:       amd64
>Synopsis:       /usr/games/random returns every line
>Confidential:   no
>Severity:       serious
>Priority:       medium
>Responsible:    freebsd-amd64
>State:          closed
>Quarter:        
>Keywords:       
>Date-Required:  
>Class:          sw-bug
>Submitter-Id:   current-users
>Arrival-Date:   Thu May 19 18:50:01 GMT 2005
>Closed-Date:    Wed Jun 29 15:20:47 GMT 2005
>Last-Modified:  Wed Jun 29 15:20:47 GMT 2005
>Originator:     Vivek Khera
>Release:        FreeBSD 5.3-STABLE amd64
>Organization:
>Environment:
System: FreeBSD d01 5.4-RELEASE-p1 FreeBSD 5.4-RELEASE-p1 #1: Wed May 18 12:29:27 EDT 2005     vivek@d01:/var/usr.obj/n/lorax1/usr5/src/sys/D03  amd64

	
>Description:
	

Running /usr/games/random to select lines from a file fails to eliminate any
lines -- ie, it functions just like "cat".

>How-To-Repeat:
	

make a file with 10000 lines for easy viewing:

perl -e 'print "$_\n" for (1..10000)' > list

now run

/usr/games/random 6 < list > out

On an amd64 system, list == out.  On an i386 system the file out has
approximately 1600 lines.

>Fix:

	

no idea.  must be some 64-bit over/under flow issue...

>Release-Note:
>Audit-Trail:

From: Adriaan de Groot <groot@kde.org>
To: bug-followup@freebsd.org, vivek@khera.org
Cc:  
Subject: Re: amd64/81279: /usr/games/random returns every line
Date: Thu, 19 May 2005 23:26:08 +0200

 Problem is lines like
 
         selected = (int)(denom * random() / LONG_MAX) == 0;
 
 which cause the overflow. Replacing LONG_MAX with INT_MAX here fixes the bug. 
 (The same replacement should probably be done in the other two places that 
 LONG_MAX occurs, since the -e flag is similarly broken.)
 
 -- 
 These are your friends - Adem
     GPG: FEA2 A3FE Adriaan de Groot

From: Vivek Khera <vivek@khera.org>
To: bug-followup@FreeBSD.org
Cc:  
Subject: Re: amd64/81279: /usr/games/random returns every line
Date: Fri, 20 May 2005 15:36:55 -0400

 --Apple-Mail-6-871661888
 Content-Transfer-Encoding: 7bit
 Content-Type: text/plain;
 	charset=US-ASCII;
 	delsp=yes;
 	format=flowed
 
 Here's a patch for /usr/src/games/random/random.c which seems to  
 solve the problem, based on Adriaan's message.
 
 
 
 --Apple-Mail-6-871661888
 Content-Transfer-Encoding: 7bit
 Content-Type: application/octet-stream;
 	x-unix-mode=0644;
 	name="random.c.patch"
 Content-Disposition: attachment;
 	filename=random.c.patch
 
 --- #random.c~	Fri May 20 15:26:50 2005
 +++ random.c	Fri May 20 15:26:50 2005
 @@ -159,7 +159,7 @@
  
  	/* Compute a random exit status between 0 and denom - 1. */
  	if (random_exit)
 -		return (int)((denom * random()) / LONG_MAX);
 +		return (int)((denom * random()) / INT_MAX);
  
  	/*
  	 * Select whether to print the first line.  (Prime the pump.)
 @@ -167,7 +167,7 @@
  	 * 0 (which has a 1 / denom chance of being true), we select the
  	 * line.
  	 */
 -	selected = (int)(denom * random() / LONG_MAX) == 0;
 +	selected = (int)(denom * random() / INT_MAX) == 0;
  	while ((ch = getchar()) != EOF) {
  		if (selected)
  			(void)putchar(ch);
 @@ -177,7 +177,7 @@
  				err(2, "stdout");
  
  			/* Now see if the next line is to be printed. */
 -			selected = (int)(denom * random() / LONG_MAX) == 0;
 +			selected = (int)(denom * random() / INT_MAX) == 0;
  		}
  	}
  	if (ferror(stdin))
 
 --Apple-Mail-6-871661888--
State-Changed-From-To: open->patched 
State-Changed-By: jhb 
State-Changed-When: Fri May 27 15:29:13 GMT 2005 
State-Changed-Why:  
Fix committed to HEAD.  Will MFC after a week. 

http://www.freebsd.org/cgi/query-pr.cgi?pr=81279 
State-Changed-From-To: patched->closed 
State-Changed-By: jhb 
State-Changed-When: Wed Jun 29 15:20:28 GMT 2005 
State-Changed-Why:  
Fix committed to RELENG_5. 

http://www.freebsd.org/cgi/query-pr.cgi?pr=81279 
>Unformatted:
