From f948835e6238d5185168374a05f8da2fc5c4482d Mon Sep 17 00:00:00 2001 From: Ahmad Samir Date: Sat, 11 Mar 2023 03:24:09 +0200 Subject: [PATCH] QLocalTime: getCurrentStandardUtcOffset: fix narrowing warnings UTC offset in seconds can't be bigger than 24 hours, so it should fit in an int. Explicitly cast to int and add asserts. Change-Id: I45460a0489a134e4ad03bdab112f067d5913709a Reviewed-by: Thiago Macieira --- src/corelib/time/qlocaltime.cpp | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/src/corelib/time/qlocaltime.cpp b/src/corelib/time/qlocaltime.cpp index 3b9a236ccf1..d8c0a3f823d 100644 --- a/src/corelib/time/qlocaltime.cpp +++ b/src/corelib/time/qlocaltime.cpp @@ -202,12 +202,19 @@ int getCurrentStandardUtcOffset() */ # if defined(_POSIX_THREAD_SAFE_FUNCTIONS) struct tm t; - if (gmtime_r(&curr, &t)) - return curr - qMkTime(&t); + if (gmtime_r(&curr, &t)) { + time_t mkt = qMkTime(&t); + int offset = int(curr - mkt); + Q_ASSERT(std::abs(offset) <= SECS_PER_DAY); + return offset; + } # else if (struct tm *tp = gmtime(&curr)) { struct tm t = *tp; // Copy it quick, hopefully before it can get stomped - return curr - qMkTime(&t); + time_t mkt = qMkTime(&t); + int offset = int(curr - mkt); + Q_ASSERT(std::abs(offset) <= SECS_PER_DAY); + return offset; } # endif } // else, presumably: errno == EOVERFLOW