From 1a97f941c3d8cd3f61e28180bc6dcce80d525250 Mon Sep 17 00:00:00 2001 From: "Jan (janneke) Nieuwenhuizen" Date: Fri, 1 Jan 2021 09:46:20 +0100 Subject: [PATCH] core: Avoid Floating point exception dividing negative numbers. * src/posix.c (seconds_and_nanoseconds_to_long): Use unsigned division. --- src/posix.c | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/posix.c b/src/posix.c index d59bfc90..51d71a67 100644 --- a/src/posix.c +++ b/src/posix.c @@ -1,6 +1,6 @@ /* -*-comment-start: "//";comment-end:""-*- * GNU Mes --- Maxwell Equations of Software - * Copyright © 2016,2017,2018,2019,2020 Jan (janneke) Nieuwenhuizen + * Copyright © 2016,2017,2018,2019,2020,2021 Jan (janneke) Nieuwenhuizen * * This file is part of GNU Mes. * @@ -411,7 +411,13 @@ gettimeofday_ () /*:((name . "gettimeofday")) */ long seconds_and_nanoseconds_to_long (long s, long ns) { - return s * TIME_UNITS_PER_SECOND + ns / (1000000000 / TIME_UNITS_PER_SECOND); + size_t uns = ns; + if (ns < 0) + { + uns = - ns; + return s * TIME_UNITS_PER_SECOND - uns / (1000000000 / TIME_UNITS_PER_SECOND); + } + return s * TIME_UNITS_PER_SECOND + uns / (1000000000 / TIME_UNITS_PER_SECOND); } struct scm *