Resync fmt_scaled. with OpenBSD.

Fixes underflow reported in bz#3401.
This commit is contained in:
Darren Tucker 2022-03-11 18:43:58 +11:00
parent 244f640711
commit 5880200867
1 changed files with 19 additions and 13 deletions

View File

@ -1,4 +1,4 @@
/* $OpenBSD: fmt_scaled.c,v 1.17 2018/05/14 04:39:04 djm Exp $ */
/* $OpenBSD: fmt_scaled.c,v 1.21 2022/03/11 07:29:53 dtucker Exp $ */
/*
* Copyright (c) 2001, 2002, 2003 Ian F. Darwin. All rights reserved.
@ -54,9 +54,9 @@ typedef enum {
} unit_type;
/* These three arrays MUST be in sync! XXX make a struct */
static unit_type units[] = { NONE, KILO, MEGA, GIGA, TERA, PETA, EXA };
static char scale_chars[] = "BKMGTPE";
static long long scale_factors[] = {
static const unit_type units[] = { NONE, KILO, MEGA, GIGA, TERA, PETA, EXA };
static const char scale_chars[] = "BKMGTPE";
static const long long scale_factors[] = {
1LL,
1024LL,
1024LL*1024,
@ -153,10 +153,8 @@ scan_scaled(char *scaled, long long *result)
}
}
if (sign) {
if (sign)
whole *= sign;
fpart *= sign;
}
/* If no scale factor given, we're done. fraction is discarded. */
if (!*p) {
@ -191,7 +189,8 @@ scan_scaled(char *scaled, long long *result)
/* truncate fpart so it doesn't overflow.
* then scale fractional part.
*/
while (fpart >= LLONG_MAX / scale_fact) {
while (fpart >= LLONG_MAX / scale_fact ||
fpart <= LLONG_MIN / scale_fact) {
fpart /= 10;
fract_digits--;
}
@ -200,7 +199,10 @@ scan_scaled(char *scaled, long long *result)
for (i = 0; i < fract_digits -1; i++)
fpart /= 10;
}
whole += fpart;
if (sign == -1)
whole -= fpart;
else
whole += fpart;
*result = whole;
return 0;
}
@ -222,12 +224,16 @@ fmt_scaled(long long number, char *result)
unsigned int i;
unit_type unit = NONE;
/* Not every negative long long has a positive representation. */
if (number == LLONG_MIN) {
errno = ERANGE;
return -1;
}
abval = llabs(number);
/* Not every negative long long has a positive representation.
* Also check for numbers that are just too darned big to format
*/
if (abval < 0 || abval / 1024 >= scale_factors[SCALE_LENGTH-1]) {
/* Also check for numbers that are just too darned big to format. */
if (abval / 1024 >= scale_factors[SCALE_LENGTH-1]) {
errno = ERANGE;
return -1;
}