summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorbluhm <bluhm@openbsd.org>2019-01-14 23:52:06 +0000
committerbluhm <bluhm@openbsd.org>2019-01-14 23:52:06 +0000
commit7ab3d2e1688fb57720ab5ba0a670f046f1b04c4c (patch)
tree1f498ff42698cc9ffc2604dc33a4385d455d5b6c
parentAdd support for building kernels with clang. Clang does not support (diff)
downloadwireguard-openbsd-7ab3d2e1688fb57720ab5ba0a670f046f1b04c4c.tar.xz
wireguard-openbsd-7ab3d2e1688fb57720ab5ba0a670f046f1b04c4c.zip
Calling llabs(LLONG_MIN) is undefined behavior, llvm 7.0.1 does not
work with our old code. In fmt_scaled() move the check before calling llabs(). found by regress/lib/libutil/fmt_scaled; OK deraadt@ millert@ tedu@
-rw-r--r--lib/libutil/fmt_scaled.c14
1 files changed, 9 insertions, 5 deletions
diff --git a/lib/libutil/fmt_scaled.c b/lib/libutil/fmt_scaled.c
index f9c8f65c865..f9f644d53e5 100644
--- a/lib/libutil/fmt_scaled.c
+++ b/lib/libutil/fmt_scaled.c
@@ -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.18 2019/01/14 23:52:06 bluhm Exp $ */
/*
* Copyright (c) 2001, 2002, 2003 Ian F. Darwin. All rights reserved.
@@ -218,12 +218,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;
}