From jilles@stack.nl  Sun Jul  6 17:30:28 2008
Return-Path: <jilles@stack.nl>
Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34])
	by hub.freebsd.org (Postfix) with ESMTP id 9D3641065688
	for <FreeBSD-gnats-submit@freebsd.org>; Sun,  6 Jul 2008 17:30:28 +0000 (UTC)
	(envelope-from jilles@stack.nl)
Received: from mx1.stack.nl (meestal-mk5.stack.nl [IPv6:2001:610:1108:5010::149])
	by mx1.freebsd.org (Postfix) with ESMTP id 5F9E18FC1E
	for <FreeBSD-gnats-submit@freebsd.org>; Sun,  6 Jul 2008 17:30:28 +0000 (UTC)
	(envelope-from jilles@stack.nl)
Received: from turtle.stack.nl (turtle.stack.nl [IPv6:2001:610:1108:5010::132])
	by mx1.stack.nl (Postfix) with ESMTP id 6B2E43F7FC
	for <FreeBSD-gnats-submit@freebsd.org>; Sun,  6 Jul 2008 19:30:27 +0200 (CEST)
Received: by turtle.stack.nl (Postfix, from userid 1677)
	id 62ABE33C7F; Sun,  6 Jul 2008 19:30:27 +0200 (CEST)
Message-Id: <20080706173027.62ABE33C7F@turtle.stack.nl>
Date: Sun,  6 Jul 2008 19:30:27 +0200 (CEST)
From: Jilles Tjoelker <jilles@stack.nl>
Reply-To: Jilles Tjoelker <jilles@stack.nl>
To: FreeBSD-gnats-submit@freebsd.org
Cc:
Subject: [PATCH] expand_number(3) silently truncates numeric part on i386
X-Send-Pr-Version: 3.113
X-GNATS-Notify:

>Number:         125338
>Category:       kern
>Synopsis:       [libutil] [patch] expand_number(3) silently truncates numeric part on i386
>Confidential:   no
>Severity:       serious
>Priority:       medium
>Responsible:    freebsd-bugs
>State:          closed
>Quarter:        
>Keywords:       
>Date-Required:  
>Class:          sw-bug
>Submitter-Id:   current-users
>Arrival-Date:   Sun Jul 06 17:40:02 UTC 2008
>Closed-Date:    Sun Aug 03 16:09:12 UTC 2008
>Last-Modified:  Sun Aug 03 16:09:12 UTC 2008
>Originator:     Jilles Tjoelker
>Release:        FreeBSD 7.0-STABLE i386
>Organization:
MCGV Stack
>Environment:
System: FreeBSD turtle.stack.nl 7.0-STABLE FreeBSD 7.0-STABLE #0: Tue May 20 22:43:27 CEST 2008 root@snail.stack.nl:/sabretooth.mnt/sources/6.x/i386/obj/sabretooth.mnt/sources/7.x/src/sys/STACK-SMP i386
problem present in -CURRENT sources as well
>Description:
On i386, if the numeric part of the string passed to expand_number(3) does
not fit in 32 bits, e.g. "5368709120k" it is truncated.
>How-To-Repeat:
Detailed how-to-repeat from Alexandre Sunny Kovalenko:

sunny:RabbitsDen>./expand_number 5368709120k
Result is 1099511627776
sunny:RabbitsDen>./expand_number 5120G
Result is 5497558138880
sunny:RabbitsDen>

The expected result is 5497558138880 for both.

test program (needs to be linked with -lutil)

#include <ctype.h>
#include <sys/types.h>
#include <inttypes.h>
#include <errno.h>
#include <libutil.h>
#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[])
{
  if(argc != 2)
  {
    fprintf(stderr, "Usage: %s <number>\n", argv[0]);
    exit(1);
  }

  errno = 0;
  intmax_t result;
  if(expand_number(argv[1], &result) || errno)
  {
    perror("Expand number");
    exit(1);
  }

  printf("Result is %jd\n", result);
  exit(0);
}

>Fix:

The problem occurs because src/lib/libutil/expand_number.c does not include
the necessary header <inttypes.h> for calling strtoimax(3). The file is
compiled without compiler warnings, so the bug shows up as wrong
behaviour.

Adding #include <inttypes.h> fixes it.

The file is slightly changed in CURRENT but the same patch should apply.

--- expand_number.patch begins here ---
--- src/lib/libutil/expand_number.c.orig	2007-09-05 16:27:13.000000000 +0200
+++ src/lib/libutil/expand_number.c	2008-07-06 13:11:02.766238000 +0200
@@ -33,6 +33,7 @@
 #include <errno.h>
 #include <libutil.h>
 #include <stdint.h>
+#include <inttypes.h>
 
 /*
  * Convert an expression of the following forms to a int64_t.
--- expand_number.patch ends here ---


>Release-Note:
>Audit-Trail:
State-Changed-From-To: open->closed 
State-Changed-By: kib 
State-Changed-When: Sun Aug 3 16:08:53 UTC 2008 
State-Changed-Why:  
Committed, thanks ! 

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