From kcwu@kcwu.csie.org  Thu May 19 11:21:26 2011
Return-Path: <kcwu@kcwu.csie.org>
Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34])
	by hub.freebsd.org (Postfix) with ESMTP id 050DA106564A
	for <FreeBSD-gnats-submit@freebsd.org>; Thu, 19 May 2011 11:21:26 +0000 (UTC)
	(envelope-from kcwu@kcwu.csie.org)
Received: from kcwu.csie.org (kcwu.csie.org [114.32.97.66])
	by mx1.freebsd.org (Postfix) with ESMTP id A00DC8FC20
	for <FreeBSD-gnats-submit@freebsd.org>; Thu, 19 May 2011 11:21:25 +0000 (UTC)
Received: from kcwu.csie.org (fakeidkcwu@kcwu.csie.org [127.0.0.1] (may be forged))
	by kcwu.csie.org (8.14.4/8.14.4) with ESMTP id p4JB0rdC089403
	for <FreeBSD-gnats-submit@freebsd.org>; Thu, 19 May 2011 19:00:53 +0800 (CST)
	(envelope-from kcwu@kcwu.csie.org)
Received: (from kcwu@localhost)
	by kcwu.csie.org (8.14.4/8.14.4/Submit) id p4JB0qHY089201;
	Thu, 19 May 2011 19:00:52 +0800 (CST)
	(envelope-from kcwu)
Message-Id: <201105191100.p4JB0qHY089201@kcwu.csie.org>
Date: Thu, 19 May 2011 19:00:52 +0800 (CST)
From: Kuang-che Wu <kcwu@kcwu.csie.org>
Reply-To: Kuang-che Wu <kcwu@kcwu.csie.org>
To: FreeBSD-gnats-submit@freebsd.org
Cc:
Subject: primes(1) prints non-prime for numbers > 2^32
X-Send-Pr-Version: 3.113
X-GNATS-Notify:

>Number:         157177
>Category:       bin
>Synopsis:       primes(1) prints non-prime for numbers > 2^32
>Confidential:   no
>Severity:       non-critical
>Priority:       medium
>Responsible:    freebsd-bugs
>State:          open
>Quarter:        
>Keywords:       
>Date-Required:  
>Class:          sw-bug
>Submitter-Id:   current-users
>Arrival-Date:   Thu May 19 11:30:11 UTC 2011
>Closed-Date:    
>Last-Modified:  Tue Jun 14 04:00:21 UTC 2011
>Originator:     Kuang-che Wu
>Release:        FreeBSD 9.0-CURRENT amd64
>Organization:
>Environment:
System: FreeBSD kcwu.csie.org 9.0-CURRENT FreeBSD 9.0-CURRENT #0: Sun Oct 17 05:01:33 CST 2010 root@kcwu.csie.org:/usr/obj/amd64.amd64/usr/src/sys/DESKTOP amd64


	
>Description:
	
	primes(1) can generate prime numbers.
	However, its prime table (/usr/src/games/primes/pr_tbl.c) contains
	primes less than 2^16. And the program didn't generate more prime in
        runtime. So it can only determine prime less than 2^32.
	For numbers larger than 2^32, it may incorrect print non-prime numbers.

>How-To-Repeat:
	
	$ primes `echo 2^32 | bc -q` | xargs -n 1 factor  | grep '[0-9] '
	4295360521: 65539 65539
	4295622677: 65539 65543
	...

>Fix:

	


>Release-Note:
>Audit-Trail:

From: Robert Lorentz <robert.lorentz@gmail.com>
To: bug-followup@FreeBSD.org,
 kcwu@kcwu.csie.org
Cc:  
Subject: Re: bin/157177: primes(1) prints non-prime for numbers &gt; 2^32
Date: Mon, 13 Jun 2011 23:29:33 -0400

 I regression tested this on FreeBSD 8.2-RELEASE against FreeBSD =
 9.0-CURRENT r221981, using primegen-0.97 on amd64 platform and came up =
 with some interesting results.
 
 First off, from primes(1) man page, syntax is:
 
 primes [ low [high] ]
 
 So your bc line effectively says:
 
 primes 4294967296=20
 
 Where low =3D 4294967296 and high is not explicitly stated.  According =
 to the man page high defaults to 1000000000 where the last prime =
 possible is 999999937.  Since 1,000,000,000 < 4,294,967,296, the correct =
 output of "primes 4294967296" should be nothing.
 
 In FreeBSD 8.2-RELEASE, primes correctly does output nothing.
 
 However on FreeBSD 9.0-CURRENT, primes incorrectly prints primes =
 starting at 4,294,967,296 and seems to go forever (not sure where it =
 will stop).  This is contrary to what the manual page says and is a =
 problem.
 
 Your original problem I did regression test and confirm to be working in =
 8.2-RELEASE and broken in 9.0-CURRENT.  I isolated one of your examples =
 using:
 
 primes 4295360520 4295360522 | xargs -n 1 factor
 
 On FreeBSD 9.0-CURRENT: 4295360521: 65539 65539
 On FreeBSD 8.2-RELEASE: No output=20
 
 
 On FreeBSD 9.0-CURRENT I debugged the source in =
 /usr/ports/math/primegen/work/primegen-0.97 a bit and realized that if I =
 ran the compiled version in =
 /usr/ports/math/primegen/work/primegen-0.97/primes I got the correct =
 expected results.  However, if I run the installed version in =
 /usr/games/primes, I get the incorrect results.  The binaries in those =
 two places aren't the same (verified using md5). =20
 
 This appears to be an issue with the port building, probably building in =
 32 bit.  If the inputs to primes are interpreted as 32bit then a "low" =
 of (2^32 + 1) is interpreted as 1, therefore being less than 1000000000, =
 therefore the code would continue to generate primes, and if this is the =
 case then I wouldn't be surprised that the prime generation code also =
 would misbehave.
 
>Unformatted:
