Skip site navigation (1)Skip section navigation (2)
Date:      Sun,  6 Jul 2008 19:30:27 +0200 (CEST)
From:      Jilles Tjoelker <jilles@stack.nl>
To:        FreeBSD-gnats-submit@FreeBSD.org
Subject:   kern/125338: [PATCH] expand_number(3) silently truncates numeric part on i386
Message-ID:  <20080706173027.62ABE33C7F@turtle.stack.nl>
Resent-Message-ID: <200807061740.m66He2BC011425@freefall.freebsd.org>

next in thread | raw e-mail | index | archive | help

>Number:         125338
>Category:       kern
>Synopsis:       [PATCH] expand_number(3) silently truncates numeric part on i386
>Confidential:   no
>Severity:       serious
>Priority:       medium
>Responsible:    freebsd-bugs
>State:          open
>Quarter:        
>Keywords:       
>Date-Required:
>Class:          sw-bug
>Submitter-Id:   current-users
>Arrival-Date:   Sun Jul 06 17:40:02 UTC 2008
>Closed-Date:
>Last-Modified:
>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:
>Unformatted:



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20080706173027.62ABE33C7F>