From df4fda17615d7e1420e15ab547ca763758d3abb4 Mon Sep 17 00:00:00 2001 From: bluhm <> Date: Mon, 14 Jan 2019 23:52:06 +0000 Subject: [PATCH] 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@ --- src/lib/libutil/fmt_scaled.c | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/src/lib/libutil/fmt_scaled.c b/src/lib/libutil/fmt_scaled.c index f9c8f65c..f9f644d5 100644 --- a/src/lib/libutil/fmt_scaled.c +++ b/src/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; }