kern/125338: [PATCH] expand_number(3) silently truncates numeric part on i386

Jilles Tjoelker jilles at stack.nl
Sun Jul 6 17:40:02 UTC 2008


>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 at 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:


More information about the freebsd-bugs mailing list