From turutani@scphys.kyoto-u.ac.jp  Wed Aug 23 08:43:14 2006
Return-Path: <turutani@scphys.kyoto-u.ac.jp>
Received: from mx1.FreeBSD.org (mx1.freebsd.org [216.136.204.125])
	by hub.freebsd.org (Postfix) with ESMTP id 14C4916A4DA
	for <FreeBSD-gnats-submit@freebsd.org>; Wed, 23 Aug 2006 08:43:14 +0000 (UTC)
	(envelope-from turutani@scphys.kyoto-u.ac.jp)
Received: from polymer3.scphys.kyoto-u.ac.jp (polymer3.scphys.kyoto-u.ac.jp [130.54.55.55])
	by mx1.FreeBSD.org (Postfix) with ESMTP id 6AE1843D45
	for <FreeBSD-gnats-submit@freebsd.org>; Wed, 23 Aug 2006 08:43:12 +0000 (GMT)
	(envelope-from turutani@scphys.kyoto-u.ac.jp)
Received: from h116.65.226.10.32118.vlan.kuins.net (h116.65.226.10.32118.vlan.kuins.net [10.226.65.116])
	by polymer3.scphys.kyoto-u.ac.jp (8.13.6/8.13.6/20060227-1) with ESMTP id k7N8h3Mb030637;
	Wed, 23 Aug 2006 17:43:04 +0900 (JST)
	(envelope-from turutani@scphys.kyoto-u.ac.jp)
Received: from h116.65.226.10.32118.vlan.kuins.net (localhost [127.0.0.1])
	by h116.65.226.10.32118.vlan.kuins.net (8.13.6/8.13.6) with ESMTP id k7N8h4Ok023261;
	Wed, 23 Aug 2006 17:43:04 +0900 (JST)
	(envelope-from turutani@h116.65.226.10.32118.vlan.kuins.net)
Received: (from turutani@localhost)
	by h116.65.226.10.32118.vlan.kuins.net (8.13.6/8.13.6/Submit) id k7N8h3wg023260;
	Wed, 23 Aug 2006 17:43:03 +0900 (JST)
	(envelope-from turutani)
Message-Id: <200608230843.k7N8h3wg023260@h116.65.226.10.32118.vlan.kuins.net>
Date: Wed, 23 Aug 2006 17:43:03 +0900 (JST)
From: Tsurutani Naoki <turutani@scphys.kyoto-u.ac.jp>
Reply-To: Tsurutani Naoki <turutani@scphys.kyoto-u.ac.jp>
To: FreeBSD-gnats-submit@freebsd.org
Cc:
Subject: printf(3) prints ill result.
X-Send-Pr-Version: 3.113
X-GNATS-Notify:

>Number:         102424
>Category:       kern
>Synopsis:       [libc] printf(3) prints ill result.
>Confidential:   no
>Severity:       serious
>Priority:       high
>Responsible:    freebsd-bugs
>State:          closed
>Quarter:        
>Keywords:       
>Date-Required:  
>Class:          sw-bug
>Submitter-Id:   current-users
>Arrival-Date:   Wed Aug 23 08:50:18 GMT 2006
>Closed-Date:    Fri Jul 25 06:33:39 UTC 2008
>Last-Modified:  Fri Jul 25 06:33:39 UTC 2008
>Originator:     Tsurutani Naoki
>Release:        FreeBSD 6.1-STABLE amd64
>Organization:
>Environment:
System: FreeBSD h116.65.226.10.32118.vlan.kuins.net 6.1-STABLE FreeBSD 6.1-STABLE #3: Fri Aug 11 12:02:50 JST 2006 turutani@h116.65.226.10.32118.vlan.kuins.net:/usr/local/work/usr/obj/usr/src/sys/POLYMER13 amd64


	
>Description:
	output via printf(3) is wrong on some special case.
	on FreeBSD/amd64 and compile for 32bit by gcc (or copied i386 binary),
	next code prints irregal output:
		#include <stdio.h>
		int main()
		{
			union {
				double a;
				int i[2];
			} b;
			b.i[1] = 0x3fb99999;
			b.i[0] = 0x99999999;
			fprintf(stdout, "%f, %.8x %.8x\n", b.a, b.i[1], b.i[0]);
			return 0;
		}
	expected output is:
		0.100000, 3fb99999 99999999
	but on above mensioned environment,
		0.0:0000, 3fb99999 99999999
	0x3fb9999999999999 is a little fewer value than 0.1 in decimal.
	this happens only on amd64, and do not happen with 64bit binary
	and on i386 machine.
	"10" is ('0'+1)//('0'+0), and "0:" is ('0'+0)//('0'+10)...
	character of each digit and its position are inconsequent.
	
>How-To-Repeat:
	everytime when compile with following way:
	% gcc -m32 -L/usr/lib32 -B/usr/lib32 foo.c -o foo
	/etc/make.conf has no CFLAGS.
	
>Fix:
	After compile all the 32bit libraries with "CFLAGS= -O" instead of "-O2"
	and install them, no trouble with above code occurs.
	And compile with "-O2" and install them makes trouble again.
	As man page of make.conf(5) and /usr/share/mk/sys.mk say,
	"-O2" in CFLAGS is now a supported feature, so this is a bug.
	This trouble happend with direct using of __dtoa() defined in
	src/contrib/gdtoa/dtoa.c, with the same compile options, or use it
	in libc.
	Therefore this is thought to be a bug in __dtoa() or compiler.
	Note that this does not occurs under "-O" libraries even if compile
	directly with dtoa.c and -O2 options.
	the following is another code:
		#include <stdio.h>
		#include <stdlib.h>
		#define dtoa __dtoa
		char* __dtoa(double d, int mode, int ndigits, int *decpt, int *sign, char **rve);
		int main()
		{
			union {
				double d;
				int b[2];
			} a;
			int decpt, sign;
			char *ptr, *ret;
		
			a.b[1] = 0x3fb99999;
			a.b[0] = 0x99999999;
			ret = __dtoa(a.d, 3, 6, &decpt, &sign, &ptr);
			printf("decpt = %d\n", decpt);
			printf("returned string is \"%s\"\n", ret);
			return 0;
		}

	this prints
		decpt = 0
		returned string is "1"
	on normal case, and
		decpt = 1
		returned string is ":"
	on wrong case.
	


>Release-Note:
>Audit-Trail:

From: Tsurutani Naoki <turutani@scphys.kyoto-u.ac.jp>
To: bug-followup@FreeBSD.org
Cc:  
Subject: Re: bin/102424: printf(3) prints ill result.
Date: Sun, 17 Dec 2006 21:31:25 +0900

 I tested a little more.
 If compile the code in "Fix" section of original PR with dtoa.c,
 binary with "-O2" prints ill result.
 But among options on "-O2", removing -fgcse can fix this problem.
 I do not know about this option, but this can save me.
 Please help me.
 
 P.S. gdtoa in original site has been changed since imported into FreeBSD.
 Especially, mail addresss in source code are changed.

From: linimon@lonesome.com (Mark Linimon)
To: bug-followup@FreeBSD.org
Cc:  
Subject: [das@FreeBSD.ORG: Re: kern/102424: [libc] printf(3) prints ill result.]
Date: Tue, 1 Jul 2008 21:40:04 -0500

 ----- Forwarded message from David Schultz <das@FreeBSD.ORG> -----
 
 From: David Schultz <das@FreeBSD.ORG>
 To: Tsurutani Naoki <turutani@scphys.kyoto-u.ac.jp>
 Cc: freebsd-bugs@FreeBSD.ORG
 
 Is this still a problem with a more recent gcc, e.g. the one in
 7.0? I ran your test on a machine running HEAD and could not
 reproduce the bug.
 
 ----- End forwarded message -----
State-Changed-From-To: open->feedback 
State-Changed-By: linimon 
State-Changed-When: Wed Jul 2 02:52:24 UTC 2008 
State-Changed-Why:  
Note that submitter was asked for feedback. 

http://www.freebsd.org/cgi/query-pr.cgi?pr=102424 

From: Tsurutani Naoki <turutani@scphys.kyoto-u.ac.jp>
To: bug-followup@FreeBSD.org, linimon@lonesome.com (Mark Linimon),
        freebsd-bugs@FreeBSD.org
Cc:  
Subject: Re: kern/102424: [libc] printf(3) prints ill result.
Date: Thu, 24 Jul 2008 20:38:42 +0900

 Hi,
 
 Now, I can ready to use 7-STABLE.
 % uname -a
 FreeBSD h116.65.226.10.32118.vlan.kuins.net 7.0-STABLE FreeBSD 7.0-STABLE #16: Sun Jul 20 
 13:56:49 JST 2008     turutani@h116.65.226.10.32118.vlan.kuins.net:/usr/obj/usr/src/sys/POLYMER13  amd64
 
 I tested my old binary compiled on 6-STABLE:
 % ./printftest
 0.0:0000, 3fb99999 99999999
 I recompiled it:
 % gcc -O2 -m32 -L/usr/lib32 -B/usr/lib32 printftest.c -o printftest
 % ./printftest
 0.100000, 3fb99999 99999999
 I recompiled it and linked with libc.so.6:
 % gcc -O2 -m32 -L/usr/lib32 -B/usr/lib32 printftest.c /usr/local/lib32/compat/libc.so.6 -o 
 printftest
 % ./printftest
 0.0:0000, 3fb99999 99999999
 
 Now, it seems to be fixed, but I do not know how this trouble is fixed.
 
 Sincerely.
 
 ---
 Tsurutani Naoki (turutani@scphys.kyoto-u.ac.jp)
State-Changed-From-To: feedback->closed  
State-Changed-By: brucec 
State-Changed-When: Fri Jul 25 06:30:13 UTC 2008 
State-Changed-Why:  
Submitter reports that the problem has been fixed. 

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