diff options
| author | Edward Welbourne <edward.welbourne@qt.io> | 2023-02-14 13:20:04 +0100 |
|---|---|---|
| committer | Edward Welbourne <edward.welbourne@qt.io> | 2023-02-16 16:42:58 +0100 |
| commit | 20fd00effc07b1c8a0a9950ad805853ff28be26a (patch) | |
| tree | 24131af933506e89ae73e90490fc6a40f5a71758 /src/corelib/time/qlocaltime.cpp | |
| parent | 64dc6fe87d05aaad3ce516747ad6dbd048f388cb (diff) | |
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>
Diffstat (limited to 'src/corelib/time/qlocaltime.cpp')
| -rw-r--r-- | src/corelib/time/qlocaltime.cpp | 39 |
1 files changed, 21 insertions, 18 deletions
diff --git a/src/corelib/time/qlocaltime.cpp b/src/corelib/time/qlocaltime.cpp index 4f7496834a3..60df487af62 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. |
