Skip to content

Commit

Permalink
Fix windows time (#1136)
Browse files Browse the repository at this point in the history
* util: Avoid potential uninitialized read in FormatISO8601DateTime(int… …64_t nTime) by checking gmtime_s/gmtime_r return value

* build: Detect gmtime_* definitions via configure
  • Loading branch information
Bushstar authored Mar 9, 2022
1 parent a1bea01 commit bb394be
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 7 deletions.
19 changes: 18 additions & 1 deletion configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -853,7 +853,23 @@ if test "x$use_thread_local" = xyes || { test "x$use_thread_local" = xauto && te
LDFLAGS="$TEMP_LDFLAGS"
fi

# Check for different ways of gathering OS randomness
dnl check for gmtime_r(), fallback to gmtime_s() if that is unavailable
dnl fail if neither are available.
AC_MSG_CHECKING(for gmtime_r)
AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[#include <ctime>]],
[[ gmtime_r((const time_t *) nullptr, (struct tm *) nullptr); ]])],
[ AC_MSG_RESULT(yes); AC_DEFINE(HAVE_GMTIME_R, 1, [Define this symbol if gmtime_r is available]) ],
[ AC_MSG_RESULT(no);
AC_MSG_CHECKING(for gmtime_s);
AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[#include <ctime>]],
[[ gmtime_s((struct tm *) nullptr, (const time_t *) nullptr); ]])],
[ AC_MSG_RESULT(yes)],
[ AC_MSG_RESULT(no); AC_MSG_ERROR(Both gmtime_r and gmtime_s are unavailable) ]
)
]
)

dnl Check for different ways of gathering OS randomness
AC_MSG_CHECKING(for Linux getrandom syscall)
AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[#include <unistd.h>
#include <sys/syscall.h>
Expand Down Expand Up @@ -1456,6 +1472,7 @@ AC_SUBST(EVENT_LIBS)
AC_SUBST(EVENT_PTHREADS_LIBS)
AC_SUBST(ZMQ_LIBS)
AC_SUBST(QR_LIBS)
AC_SUBST(HAVE_GMTIME_R)
AC_CONFIG_FILES([Makefile src/Makefile doc/man/Makefile share/setup.nsi test/config.ini])
AC_CONFIG_FILES([contrib/devtools/split-debug.sh],[chmod +x contrib/devtools/split-debug.sh])
AM_COND_IF([HAVE_DOXYGEN], [AC_CONFIG_FILES([doc/Doxyfile])])
Expand Down
16 changes: 10 additions & 6 deletions src/util/time.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,21 +93,25 @@ void MilliSleep(int64_t n)
std::string FormatISO8601DateTime(int64_t nTime) {
struct tm ts;
time_t time_val = nTime;
#ifdef _MSC_VER
gmtime_s(&ts, &time_val);
#ifdef HAVE_GMTIME_R
if (gmtime_r(&time_val, &ts) == nullptr) {
#else
gmtime_r(&time_val, &ts);
if (gmtime_s(&ts, &time_val) != 0) {
#endif
return {};
}
return strprintf("%04i-%02i-%02iT%02i:%02i:%02iZ", ts.tm_year + 1900, ts.tm_mon + 1, ts.tm_mday, ts.tm_hour, ts.tm_min, ts.tm_sec);
}

std::string FormatISO8601Date(int64_t nTime) {
struct tm ts;
time_t time_val = nTime;
#ifdef _MSC_VER
gmtime_s(&ts, &time_val);
#ifdef HAVE_GMTIME_R
if (gmtime_r(&time_val, &ts) == nullptr) {
#else
gmtime_r(&time_val, &ts);
if (gmtime_s(&ts, &time_val) != 0) {
#endif
return {};
}
return strprintf("%04i-%02i-%02i", ts.tm_year + 1900, ts.tm_mon + 1, ts.tm_mday);
}

0 comments on commit bb394be

Please sign in to comment.