From: dtucker Date: Fri, 11 Mar 2022 07:29:53 +0000 (+0000) Subject: Check for underflow as well as overflow when scaling negative numbers. X-Git-Url: http://artulab.com/gitweb/?a=commitdiff_plain;h=39af7dcca31cab3059a865d66e9ca17a9a2563f7;p=openbsd Check for underflow as well as overflow when scaling negative numbers. ok millert@ --- diff --git a/lib/libutil/fmt_scaled.c b/lib/libutil/fmt_scaled.c index f06c727be51..0b443d4d83a 100644 --- a/lib/libutil/fmt_scaled.c +++ b/lib/libutil/fmt_scaled.c @@ -1,4 +1,4 @@ -/* $OpenBSD: fmt_scaled.c,v 1.20 2021/06/20 14:08:42 tb 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. @@ -185,7 +185,8 @@ scan_scaled(char *scaled, long long *result) /* truncate fpart so it does'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--; } diff --git a/regress/lib/libutil/fmt_scaled/fmt_test.c b/regress/lib/libutil/fmt_scaled/fmt_test.c index f35c4451407..308c2ce01fc 100644 --- a/regress/lib/libutil/fmt_scaled/fmt_test.c +++ b/regress/lib/libutil/fmt_scaled/fmt_test.c @@ -1,4 +1,4 @@ -/* $OpenBSD: fmt_test.c,v 1.17 2021/06/20 14:09:59 tb Exp $ */ +/* $OpenBSD: fmt_test.c,v 1.18 2022/03/11 07:29:53 dtucker Exp $ */ /* * Combined tests for fmt_scaled and scan_scaled. @@ -188,6 +188,9 @@ struct { /* the test cases */ { "", 0, 0 }, /* boundary */ { "--1", -1, EINVAL }, { "++42", -1, EINVAL }, + { "-.060000000000000000E", -69175290276410818, 0 }, + { "-.600000000000000000E", -691752902764108185, 0 }, + { "-60000000000000000E", 0, ERANGE }, { "SCALE_OVERFLOW", 0, ERANGE }, { "SCALE_UNDERFLOW", 0, ERANGE }, { "LLONG_MAX_K", (LLONG_MAX / 1024) * 1024, 0 },