Browse Source

Fix overly-conservative overflow checks on mulitplications and add checks

on additions.  This allows scan_scaled to work up to +/-LLONG_MAX (LLONG_MIN
will still be flagged as a range error).  ok millert@
OPENBSD_6_1
dtucker 7 years ago
parent
commit
b249d7ea63
1 changed files with 11 additions and 3 deletions
  1. +11
    -3
      src/lib/libutil/fmt_scaled.c

+ 11
- 3
src/lib/libutil/fmt_scaled.c View File

@ -1,4 +1,4 @@
/* $OpenBSD: fmt_scaled.c,v 1.15 2017/03/15 05:25:56 dtucker Exp $ */
/* $OpenBSD: fmt_scaled.c,v 1.16 2017/03/16 02:40:46 dtucker Exp $ */
/* /*
* Copyright (c) 2001, 2002, 2003 Ian F. Darwin. All rights reserved. * Copyright (c) 2001, 2002, 2003 Ian F. Darwin. All rights reserved.
@ -121,22 +121,30 @@ scan_scaled(char *scaled, long long *result)
/* ignore extra fractional digits */ /* ignore extra fractional digits */
continue; continue;
fract_digits++; /* for later scaling */ fract_digits++; /* for later scaling */
if (fpart >= LLONG_MAX / 10) {
if (fpart > LLONG_MAX / 10) {
errno = ERANGE; errno = ERANGE;
return -1; return -1;
} }
fpart *= 10; fpart *= 10;
if (i > LLONG_MAX - fpart) {
errno = ERANGE;
return -1;
}
fpart += i; fpart += i;
} else { /* normal digit */ } else { /* normal digit */
if (++ndigits >= MAX_DIGITS) { if (++ndigits >= MAX_DIGITS) {
errno = ERANGE; errno = ERANGE;
return -1; return -1;
} }
if (whole >= LLONG_MAX / 10) {
if (whole > LLONG_MAX / 10) {
errno = ERANGE; errno = ERANGE;
return -1; return -1;
} }
whole *= 10; whole *= 10;
if (i > LLONG_MAX - whole) {
errno = ERANGE;
return -1;
}
whole += i; whole += i;
} }
} }


Loading…
Cancel
Save