Subject: lib/29937: [PATCH]: Second argument of humanize_number(3) shouldn't change function behaviour.
To: None <lib-bug-people@netbsd.org, gnats-admin@netbsd.org,>
From: None <dunstan@FreeBSD.czest.pl>
List: netbsd-bugs
Date: 04/10/2005 20:14:01
>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;)