Skip to content

Commit 55ace13

Browse files
pabigotnashif
authored andcommitted
lib/timeutil: avoid implementation-defined behavior
The algorithm for converting broken-down civil time to seconds in the POSIX epoch time scale would produce undefined behavior on a toolchain that uses a 32-bit time_t in cases where the referenced time could not be represented exactly. However, there are use cases in Zephyr for civil time conversions outside the 32-bit representable range of 1901-12-13T20:45:52Z through 2038-01-19T03:14:07Z inclusive. Add new API that specifically returns a 64-bit signed seconds count, and revise the existing API to detect out-of-range values and convert them to a diagnosible error. Closes #18465 Signed-off-by: Peter A. Bigot <[email protected]>
1 parent cc1594a commit 55ace13

File tree

3 files changed

+157
-15
lines changed

3 files changed

+157
-15
lines changed

include/sys/timeutil.h

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,11 @@
1111
* POSIX defines gmtime() to convert from time_t to struct tm, but all
1212
* inverse transformations are non-standard or require access to time
1313
* zone information. timeutil_timegm() implements the functionality
14-
* of the GNU extension timegm() function.
14+
* of the GNU extension timegm() function, but changes the error value
15+
* as @c EOVERFLOW is not a standard C error identifier.
16+
*
17+
* timeutil_timegm64() is provided to support full precision
18+
* conversion on platforms where @c time_t is limited to 32 bits.
1519
*/
1620

1721
#ifndef ZEPHYR_INCLUDE_SYS_TIMEUTIL_H_
@@ -32,6 +36,19 @@ extern "C" {
3236
*
3337
* @see http://man7.org/linux/man-pages/man3/timegm.3.html
3438
*/
39+
s64_t timeutil_timegm64(const struct tm *tm);
40+
41+
/**
42+
* @brief Convert broken-down time to a POSIX epoch offset in seconds.
43+
*
44+
* @param tm pointer to broken down time.
45+
*
46+
* @return the corresponding time in the POSIX epoch time scale. If
47+
* the time cannot be represented then @c (time_t)-1 is returned and
48+
* @c errno is set to @c ERANGE`.
49+
*
50+
* @see http://man7.org/linux/man-pages/man3/timegm.3.html
51+
*/
3552
time_t timeutil_timegm(const struct tm *tm);
3653

3754
#ifdef __cplusplus

lib/os/timeutil.c

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
*/
1212

1313
#include <zephyr/types.h>
14+
#include <errno.h>
1415
#include <sys/timeutil.h>
1516

1617
/** Convert a civil (proleptic Gregorian) date to days relative to
@@ -39,23 +40,31 @@ static s64_t time_days_from_civil(s64_t y,
3940
return era * 146097 + (time_t)doe - 719468;
4041
}
4142

42-
/** Convert civil time to UNIX time.
43-
*
44-
* @param tvp pointer to a civil time structure. `tm_year`, `tm_mon`,
45-
* `tm_mday`, `tm_hour`, `tm_min`, and `tm_sec` must be valid. All
46-
* other fields are ignored.
47-
*
48-
* @return the signed number of seconds between 1970-01-01T00:00:00
49-
* and the specified time ignoring leap seconds and DST offsets.
50-
*/
51-
time_t timeutil_timegm(const struct tm *tm)
43+
s64_t timeutil_timegm64(const struct tm *tm)
5244
{
5345
s64_t y = 1900 + (s64_t)tm->tm_year;
5446
unsigned int m = tm->tm_mon + 1;
5547
unsigned int d = tm->tm_mday - 1;
5648
s64_t ndays = time_days_from_civil(y, m, d);
49+
s64_t time = tm->tm_sec;
50+
51+
time += 60LL * (tm->tm_min + 60LL * tm->tm_hour);
52+
time += 86400LL * ndays;
53+
54+
return time;
55+
}
56+
57+
time_t timeutil_timegm(const struct tm *tm)
58+
{
59+
s64_t time = timeutil_timegm64(tm);
60+
time_t rv = (time_t)time;
5761

58-
return (time_t)tm->tm_sec
59-
+ 60 * (tm->tm_min + 60 * tm->tm_hour)
60-
+ 86400 * ndays;
62+
errno = 0;
63+
if ((sizeof(rv) == sizeof(s32_t))
64+
&& ((time < (s64_t)INT32_MIN)
65+
|| (time > (s64_t)INT32_MAX))) {
66+
errno = ERANGE;
67+
rv = -1;
68+
}
69+
return rv;
6170
}

tests/lib/timeutil/src/test_s64.c

Lines changed: 117 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
/* Tests where time_t requires a 64-bit value */
88

9+
#include <errno.h>
910
#include <ztest.h>
1011
#include "timeutil_test.h"
1112

@@ -210,10 +211,125 @@ static const struct timeutil_test_data tests[] = {
210211
} },
211212
};
212213

214+
static void test_time32_errno_clear(void)
215+
{
216+
const struct timeutil_test_data *tp = &(const struct timeutil_test_data){
217+
.unix = 0,
218+
.civil = "1970-01-01 00:00:00 Thu 001",
219+
.tm = {
220+
.tm_sec = 0,
221+
.tm_min = 0,
222+
.tm_hour = 0,
223+
.tm_mday = 1,
224+
.tm_mon = 0,
225+
.tm_year = 70,
226+
.tm_wday = 4,
227+
.tm_yday = 0,
228+
},
229+
};
230+
231+
errno = EINVAL;
232+
233+
time_t unix = timeutil_timegm(&tp->tm);
234+
235+
zassert_equal(unix, tp->unix,
236+
"conversion incorrect");
237+
zassert_equal(errno, 0,
238+
"errno was not cleared");
239+
}
240+
241+
static void test_time32_epochm1(void)
242+
{
243+
const struct timeutil_test_data *tp = &(const struct timeutil_test_data){
244+
.unix = -1,
245+
.civil = "1969-12-31 23:59:59 Wed 365",
246+
.tm = {
247+
.tm_sec = 59,
248+
.tm_min = 59,
249+
.tm_hour = 23,
250+
.tm_mday = 31,
251+
.tm_mon = 11,
252+
.tm_year = 69,
253+
.tm_wday = 3,
254+
.tm_yday = 364,
255+
},
256+
};
257+
258+
errno = EINVAL;
259+
260+
time_t unix = timeutil_timegm(&tp->tm);
261+
262+
zassert_equal(unix, tp->unix,
263+
"conversion incorrect");
264+
zassert_equal(errno, 0,
265+
"final errno state bad");
266+
}
267+
268+
static void test_time32_underflow(void)
269+
{
270+
const s64_t unix64 = -2147483649;
271+
const struct timeutil_test_data *tp = &(const struct timeutil_test_data){
272+
.civil = "1901-12-13 20:45:51 Fri 347",
273+
.tm = {
274+
.tm_sec = 51,
275+
.tm_min = 45,
276+
.tm_hour = 20,
277+
.tm_mday = 13,
278+
.tm_mon = 11,
279+
.tm_year = 1,
280+
.tm_wday = 5,
281+
.tm_yday = 346,
282+
},
283+
};
284+
285+
zassert_equal(timeutil_timegm64(&tp->tm), unix64,
286+
"fullscale failed");
287+
errno = 0;
288+
289+
time_t unix = timeutil_timegm(&tp->tm);
290+
291+
zassert_equal(unix, -1,
292+
"underflow undetected");
293+
zassert_equal(errno, ERANGE,
294+
"final errno state bad");
295+
}
296+
297+
static void test_time32_overflow(void)
298+
{
299+
const s64_t unix64 = 2147483648;
300+
const struct timeutil_test_data *tp = &(const struct timeutil_test_data){
301+
.civil = "2038-01-19 03:14:08 Tue 019",
302+
.tm = {
303+
.tm_sec = 8,
304+
.tm_min = 14,
305+
.tm_hour = 3,
306+
.tm_mday = 19,
307+
.tm_mon = 0,
308+
.tm_year = 138,
309+
.tm_wday = 2,
310+
.tm_yday = 18,
311+
},
312+
};
313+
314+
zassert_equal(timeutil_timegm64(&tp->tm), unix64,
315+
"fullscale failed");
316+
errno = 0;
317+
318+
time_t unix = timeutil_timegm(&tp->tm);
319+
320+
zassert_equal(unix, -1,
321+
"overflow undetected");
322+
zassert_equal(errno, ERANGE,
323+
"final errno state bad");
324+
}
325+
213326
void test_s64(void)
214327
{
215328
if (sizeof(time_t) < 8U) {
216-
ztest_test_skip();
329+
test_time32_errno_clear();
330+
test_time32_epochm1();
331+
test_time32_underflow();
332+
test_time32_overflow();
217333
return;
218334
}
219335
timeutil_check(tests, sizeof(tests) / sizeof(*tests));

0 commit comments

Comments
 (0)