From 947fd3d6ea30f2180d4671e73090ba76ad69f165 Mon Sep 17 00:00:00 2001 From: Jerry Yu Date: Mon, 30 Jan 2023 13:27:54 +0800 Subject: [PATCH] Implement ms time with GetSystemTimeAsFile time. There's a potential race condition with calling time(NULL) after GetSystemTime(). See https://learn.microsoft.com/en-us/archive/msdn-magazine/2004/march/implementing-a-high-resolution-time-provider-for-windows Signed-off-by: Jerry Yu --- library/platform_util.c | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/library/platform_util.c b/library/platform_util.c index 1f60404fd6..e885a921ec 100644 --- a/library/platform_util.c +++ b/library/platform_util.c @@ -196,10 +196,13 @@ mbedtls_ms_time_t mbedtls_ms_time(void) #include mbedtls_ms_time_t mbedtls_ms_time(void) { - SYSTEMTIME st; + FILETIME ct; + mbedtls_ms_time_t current_ms; - GetSystemTime(&st); - return time(NULL)*1000LL + st.wMilliseconds; + GetSystemTimeAsFileTime(&ct); + current_ms = ((mbedtls_ms_time_t) ct.dwLowDateTime + + ((mbedtls_ms_time_t) (ct.dwHighDateTime) << 32LL))/10; + return current_ms; } #else #error "No mbedtls_ms_time available"