From 20fd00effc07b1c8a0a9950ad805853ff28be26a Mon Sep 17 00:00:00 2001 From: Edward Welbourne Date: Tue, 14 Feb 2023 13:20:04 +0100 Subject: [PATCH] Check return from time() in getCurrentStandardUtcOffset() It's possible for time_t to overflow and time() to thus fail, so check against that. We don't need to think about the last second of 1969 for this, as that won't be the current time for anyone running this code. Change-Id: I14f34d5d3e2ab9713593fcd06d6771e1d7f357ee Reviewed-by: Thiago Macieira --- src/corelib/time/qlocaltime.cpp | 39 ++++++++++++++++++--------------- 1 file changed, 21 insertions(+), 18 deletions(-) diff --git a/src/corelib/time/qlocaltime.cpp b/src/corelib/time/qlocaltime.cpp index 4f7496834a..60df487af6 100644 --- a/src/corelib/time/qlocaltime.cpp +++ b/src/corelib/time/qlocaltime.cpp @@ -222,27 +222,30 @@ int getCurrentStandardUtcOffset() #else qTzSet(); const time_t curr = time(nullptr); - /* Set t to the UTC representation of curr; the time whose local standard - time representation coincides with that differs from curr by local time's - standard offset. Note that gmtime() leaves the tm_isdst flag set to 0, - so mktime() will, even if local time is currently using DST, return the - time since epoch at which local standard time would have the same - representation as UTC's representation of curr. The fact that mktime() - also flips tm_isdst and updates the time fields to the DST-equivalent - time needn't concern us here; all that matters is that it returns the - time after epoch at which standard time's representation would have - matched UTC's, had it been in effect. - */ + if (curr != -1) { + /* Set t to the UTC representation of curr; the time whose local + standard time representation coincides with that differs from curr by + local time's standard offset. Note that gmtime() leaves the tm_isdst + flag set to 0, so mktime() will, even if local time is currently + using DST, return the time since epoch at which local standard time + would have the same representation as UTC's representation of + curr. The fact that mktime() also flips tm_isdst and updates the time + fields to the DST-equivalent time needn't concern us here; all that + matters is that it returns the time after epoch at which standard + time's representation would have matched UTC's, had it been in + effect. + */ # if defined(_POSIX_THREAD_SAFE_FUNCTIONS) - struct tm t; - if (gmtime_r(&curr, &t)) - return curr - qMkTime(&t); + struct tm t; + if (gmtime_r(&curr, &t)) + return curr - qMkTime(&t); # else - if (struct tm *tp = gmtime(&curr)) { - struct tm t = *tp; // Copy it quick, hopefully before it can get stomped - return curr - qMkTime(&t); - } + if (struct tm *tp = gmtime(&curr)) { + struct tm t = *tp; // Copy it quick, hopefully before it can get stomped + return curr - qMkTime(&t); + } # endif + } // else, presumably: errno == EOVERFLOW #endif // Platform choice qDebug("Unable to determine current standard time offset from UTC"); // We can't tell, presume UTC.