From 4fff07f380e5bb99c03cc4c3f3d38e5ec4770ec9 Mon Sep 17 00:00:00 2001
From: Aldo Nunez <aldonunez1@gmail.com>
Date: Wed, 23 Mar 2016 07:44:56 -0700
Subject: [PATCH] Fix a bug where al_wait_for_event_timed can block despite 0
 timeout.

This was reported at https://github.com/liballeg/allegro5/issues/593.
---
 docs/src/refman/events.txt | 4 ++++
 docs/src/refman/time.txt   | 2 ++
 src/win/wtime.c            | 1 +
 src/win/wxthread.c         | 3 +++
 4 files changed, 10 insertions(+)

diff --git a/docs/src/refman/events.txt b/docs/src/refman/events.txt
index 85ed8d7..d5095db 100644
--- a/docs/src/refman/events.txt
+++ b/docs/src/refman/events.txt
@@ -760,6 +760,8 @@ the first event is left at the head of the queue.
 `secs` determines approximately how many seconds to wait. If the call times
 out, false is returned.  Otherwise, if an event ocurred, true is returned.
 
+For compatibility with all platforms, `secs` must be 2,147,483.647 seconds or less.
+
 See also: [ALLEGRO_EVENT], [al_wait_for_event], [al_wait_for_event_until]
 
 ## API: al_wait_for_event_until
@@ -772,6 +774,8 @@ the first event is left at the head of the queue.
 `timeout` determines how long to wait.  If the call times out, false is
 returned.  Otherwise, if an event ocurred, true is returned.
 
+For compatibility with all platforms, `timeout` must be 2,147,483.647 seconds or less.
+
 See also: [ALLEGRO_EVENT], [ALLEGRO_TIMEOUT], [al_init_timeout],
 [al_wait_for_event], [al_wait_for_event_timed]
 
diff --git a/docs/src/refman/time.txt b/docs/src/refman/time.txt
index b766869..ab55a39 100644
--- a/docs/src/refman/time.txt
+++ b/docs/src/refman/time.txt
@@ -24,6 +24,8 @@ order of microseconds.
 
 Set timeout value of some number of seconds after the function call.
 
+For compatibility with all platforms, `seconds` must be 2,147,483.647 seconds or less.
+
 See also: [ALLEGRO_TIMEOUT], [al_wait_for_event_until]
 
 ## API: al_rest
diff --git a/src/win/wtime.c b/src/win/wtime.c
index d86f262..c159dcf 100644
--- a/src/win/wtime.c
+++ b/src/win/wtime.c
@@ -138,6 +138,7 @@ void al_init_timeout(ALLEGRO_TIMEOUT *timeout, double seconds)
    ALLEGRO_TIMEOUT_WIN *wt = (ALLEGRO_TIMEOUT_WIN *) timeout;
 
    ASSERT(wt);
+   ASSERT(seconds <= INT_MAX/1000.0);
 
    if (seconds <= 0.0) {
       wt->abstime = timeGetTime();
diff --git a/src/win/wxthread.c b/src/win/wxthread.c
index 658d680..aa04a6b 100644
--- a/src/win/wxthread.c
+++ b/src/win/wxthread.c
@@ -338,6 +338,9 @@ int _al_cond_timedwait(_AL_COND *cond, _AL_MUTEX *mtxExternal,
 
    now = timeGetTime();
    rel_msecs = win_timeout->abstime - now;
+   if (rel_msecs > INT_MAX) {
+      rel_msecs = 0;
+   }
    if (rel_msecs == INFINITE) {
       rel_msecs--;
    }
-- 
2.7.4.windows.1

