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 <thiago.macieira@intel.com>
bb10
Edward Welbourne 2023-02-14 13:20:04 +01:00
parent 64dc6fe87d
commit 20fd00effc
1 changed files with 21 additions and 18 deletions

View File

@ -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.