From a9553a8c491900deb39f21a8de4435f654b03db8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Manuel=20P=C3=A9gouri=C3=A9-Gonnard?= Date: Fri, 10 Apr 2015 17:30:46 +0200 Subject: [PATCH] Fix warnings from mingw64 in timing.c Backport from dda52139 from the 1.3 branch --- ChangeLog | 1 + include/polarssl/timing.h | 4 ++++ library/timing.c | 20 ++++++++++++-------- 3 files changed, 17 insertions(+), 8 deletions(-) diff --git a/ChangeLog b/ChangeLog index 7ee4050a5..69ecd603e 100644 --- a/ChangeLog +++ b/ChangeLog @@ -7,6 +7,7 @@ Security crash it remotely (found by Caj Larsson). Bugfix + * Fix warnings from mingw64 in timing.c (found by kxjklele). * Fix potential unintended sign extension in asn1_get_len() on 64-bit platforms (found with Coverity Scan). diff --git a/include/polarssl/timing.h b/include/polarssl/timing.h index 163477406..2f59f0c17 100644 --- a/include/polarssl/timing.h +++ b/include/polarssl/timing.h @@ -55,6 +55,10 @@ unsigned long get_timer( struct hr_time *val, int reset ); * \brief Setup an alarm clock * * \param seconds delay before the "alarmed" flag is set + * + * \warning Only one alarm at a time is supported. In a threaded + * context, this means one for the whole process, not one per + * thread. */ void set_alarm( int seconds ); diff --git a/library/timing.c b/library/timing.c index 13dc924bb..1ba02fec1 100644 --- a/library/timing.c +++ b/library/timing.c @@ -229,20 +229,24 @@ unsigned long get_timer( struct hr_time *val, int reset ) return( delta ); } -DWORD WINAPI TimerProc( LPVOID uElapse ) -{ - Sleep( (DWORD) uElapse ); - alarmed = 1; +/* It's OK to use a global because alarm() is supposed to be global anyway */ +static DWORD alarmMs; + +static DWORD WINAPI TimerProc( LPVOID TimerContext ) +{ + ((void) TimerContext); + Sleep( alarmMs ); + alarmed = 1; return( TRUE ); } void set_alarm( int seconds ) -{ +{ DWORD ThreadId; - alarmed = 0; - CloseHandle( CreateThread( NULL, 0, TimerProc, - (LPVOID) ( seconds * 1000 ), 0, &ThreadId ) ); + alarmed = 0; + alarmMs = seconds * 1000; + CloseHandle( CreateThread( NULL, 0, TimerProc, NULL, 0, &ThreadId ) ); } void m_sleep( int milliseconds )