From 4e7848787c730f3fbde9cadcfb0b18f147734082 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Petr=20P=C3=ADsa=C5=99?= Date: Wed, 8 Feb 2017 18:02:12 +0100 Subject: [PATCH] Fix signed integer wrap in time2sub() MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit There is a signed integer wrap that has an undefined behevior and GCC 7 knows about it that the compiled code just crashes. Upstream changed time_t to int64_t in a next release, thus here I will use a different fix than plain INT64_MAX and INT64_MIN. Fix can be verified by building with -fsanitize=undefined option. Signed-off-by: Petr Písař --- src/bson/bson-timegm.c | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/bson/bson-timegm.c b/src/bson/bson-timegm.c index 1cb4990..1cea127 100644 --- a/src/bson/bson-timegm.c +++ b/src/bson/bson-timegm.c @@ -636,10 +636,8 @@ time2sub(struct tm *const tmp, lo = 0; hi = lo - 1; } else { - lo = 1; - for (i = 0; i < (int) TYPE_BIT(time_t) - 1; ++i) - lo *= 2; - hi = -(lo + 1); + hi = (((time_t)1 << (TYPE_BIT(time_t) - 2)) - 1) * 2 + 1; + lo = -hi - 1; } for ( ; ; ) { t = lo / 2 + hi / 2; -- 2.7.4