From 3fea1af0ad8f057ccc2135aa083d5d7ae7d7c8f4 Mon Sep 17 00:00:00 2001 From: Irfan Mohammad Date: Fri, 14 Mar 2025 09:21:39 +0000 Subject: [PATCH 1/2] utests - call tzset after setenv TZ unit-tests can fail if run in an env where TZ is already set. This is because utest.h setup calls setenv("TZ"), but does not call tzset() immediately after that. This causes tzname, timezone, and daylight global variables to by out of sync from the newly set environment value of "TZ". To fix this, explicitly call tzset(). It's unlikely that a program would call setenv("TZ") during it's lifetime, and this should ideally only happen in a test env. --- tests/utests/utests.h | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tests/utests/utests.h b/tests/utests/utests.h index b1f72cc42..a81a34d8b 100644 --- a/tests/utests/utests.h +++ b/tests/utests/utests.h @@ -1326,6 +1326,8 @@ utest_setup(void **state) /* set CET */ setenv("TZ", "CET+02:00", 1); + /* call tzset explicitly, to update the tzname, timezone and daylight global variables */ + tzset(); return 0; } From fa1d3d43e111c189825b17631ca2bc802e5415cc Mon Sep 17 00:00:00 2001 From: Irfan Mohammad Date: Fri, 14 Mar 2025 13:54:34 +0000 Subject: [PATCH 2/2] avoid tz_set calls in ly_time functions. It does not seem necessary to call `ly_tzset_once()` from `ly_time_time2str()` or `ly_time_tz_offset_at()`. If the user changes TZ env variable, they must explicitly call `tzset()` to update the global variables `tzname`, `timezone` and `daylight` --- src/tree_data_common.c | 21 --------------------- 1 file changed, 21 deletions(-) diff --git a/src/tree_data_common.c b/src/tree_data_common.c index d97f643ea..6363e98cc 100644 --- a/src/tree_data_common.c +++ b/src/tree_data_common.c @@ -1620,30 +1620,12 @@ ly_time_tz_offset(void) return ly_time_tz_offset_at(time(NULL)); } -/** - * @brief Call tzset() if not already called by this process. - */ -static void -ly_tzset_once(void) -{ - static int ly_tzset_called = 0; - - if (ly_tzset_called) { - return; - } - tzset(); - ly_tzset_called = 1; -} - LIBYANG_API_DEF int ly_time_tz_offset_at(time_t time) { struct tm tm_local, tm_utc; int result = 0; - /* init timezone */ - ly_tzset_once(); - /* get local and UTC time */ localtime_r(&time, &tm_local); gmtime_r(&time, &tm_utc); @@ -1791,9 +1773,6 @@ ly_time_time2str(time_t time, const char *fractions_s, char **str) LY_CHECK_ARG_RET(NULL, str, LY_EINVAL); - /* init timezone */ - ly_tzset_once(); - /* convert */ if (!localtime_r(&time, &tm)) { return LY_ESYS;