NetBSD Problem Report #29937

From www@netbsd.org  Sun Apr 10 20:13:12 2005
Return-Path: <www@netbsd.org>
Received: by narn.netbsd.org (Postfix, from userid 31301)
	id 6842863B116; Sun, 10 Apr 2005 20:13:12 +0000 (UTC)
Message-Id: <20050410201312.6842863B116@narn.netbsd.org>
Date: Sun, 10 Apr 2005 20:13:12 +0000 (UTC)
From: dunstan@FreeBSD.czest.pl
Reply-To: dunstan@FreeBSD.czest.pl
To: gnats-bugs@netbsd.org
Subject: [PATCH]: Second argument of humanize_number(3) shouldn't change function behaviour.
X-Send-Pr-Version: www-1.0

>Number:         29937
>Category:       lib
>Synopsis:       [PATCH]: Second argument of humanize_number(3) shouldn't change function behaviour.
>Confidential:   no
>Severity:       non-critical
>Priority:       low
>Responsible:    lib-bug-people
>State:          open
>Class:          sw-bug
>Submitter-Id:   net
>Arrival-Date:   Sun Apr 10 20:14:00 +0000 2005
>Originator:     Wojciech A. Koszek
>Release:        -CURRENT
>Organization:
>Environment:
>Description:
Second argument of humanize_number(3) is size of buffer, and as it is
written in manual page, it must by at least 4 + suffix length.
Putting here for for example sizeof(buf) where buf is declared as:
        char buf[16];
causes problems, because code responsible for dividing "bytes" in order to make it smaller depends on "len" argument, which is equal to len - baselen:
[..]
for (max = 100, i = len - baselen; i-- > 0;)
     max *= 10;
for (i = 0; bytes >= max && i < maxscale; i++)
     bytes /= divisor;
[..]

Result of humanize_number isn't proper if len > 7:
http://FreeBSD.czest.pl/dunstan/hum.c

dunstan@dunstan:(/tmp/f/n)$ ./hum
[1] humanized 524288 -> '512K'
[2] humanized 524288 -> '524288B'


>How-To-Repeat:
Fetch:

http://FreeBSD.czest.pl/dunstan/hum.c

Compile and run:

./hum
[1] humanized 524288 -> '512K'
[2] humanized 524288 -> '524288B'

>Fix:
humanize_number(3) could be rewritten, but this patch corrects described problem.

Index: humanize_number.c
===================================================================
RCS file: /cvsroot/src/lib/libc/gen/humanize_number.c,v
retrieving revision 1.9
diff -r1.9 humanize_number.c
54a55,56
> #define MAX(a, b) (a) > (b) ? (a) : (b)
> 
122c124
< for (max = 100, i = len - baselen; i-- > 0;)
---
> for (max = 100, i = MAX(len - baselen, 7); i-- > 0;)

NetBSD Home
NetBSD PR Database Search

(Contact us) $NetBSD: query-full-pr,v 1.39 2013/11/01 18:47:49 spz Exp $
$NetBSD: gnats_config.sh,v 1.8 2006/05/07 09:23:38 tsutsui Exp $
Copyright © 1994-2007 The NetBSD Foundation, Inc. ALL RIGHTS RESERVED.