1
0
mirror of git://sigrok.org/libserialport synced 2023-08-10 21:13:24 +03:00

sp_wait: Avoid overflow of timeout parameter to poll().

This commit is contained in:
Martin Ling 2015-10-13 15:04:03 +01:00
parent 1a584c45b0
commit 127d8d0ce7
2 changed files with 10 additions and 3 deletions

View File

@ -35,6 +35,7 @@
#include <errno.h> #include <errno.h>
#include <stdio.h> #include <stdio.h>
#include <stdarg.h> #include <stdarg.h>
#include <limits.h>
#ifdef _WIN32 #ifdef _WIN32
#include <windows.h> #include <windows.h>
#include <tchar.h> #include <tchar.h>

View File

@ -1421,7 +1421,9 @@ SP_API enum sp_return sp_wait(struct sp_event_set *event_set,
RETURN_OK(); RETURN_OK();
#else #else
struct timeval start, delta, now, end = {0, 0}; struct timeval start, delta, now, end = {0, 0};
int started = 0; const struct timeval max_delta = {
(INT_MAX / 1000), (INT_MAX % 1000) * 1000};
int started = 0, timeout_overflow = 0;
int result, timeout_remaining_ms; int result, timeout_remaining_ms;
struct pollfd *pollfds; struct pollfd *pollfds;
unsigned int i; unsigned int i;
@ -1461,7 +1463,8 @@ SP_API enum sp_return sp_wait(struct sp_event_set *event_set,
if (!timeout_ms) { if (!timeout_ms) {
timeout_remaining_ms = -1; timeout_remaining_ms = -1;
} else if (!started) { } else if (!started) {
timeout_remaining_ms = timeout_ms; timeout_overflow = (timeout_ms > INT_MAX);
timeout_remaining_ms = timeout_overflow ? INT_MAX : timeout_ms;
} else { } else {
gettimeofday(&now, NULL); gettimeofday(&now, NULL);
if (timercmp(&now, &end, >)) { if (timercmp(&now, &end, >)) {
@ -1469,6 +1472,8 @@ SP_API enum sp_return sp_wait(struct sp_event_set *event_set,
break; break;
} }
timersub(&end, &now, &delta); timersub(&end, &now, &delta);
if ((timeout_overflow = timercmp(&delta, &max_delta, >)))
delta = max_delta;
timeout_remaining_ms = delta.tv_sec * 1000 + delta.tv_usec / 1000; timeout_remaining_ms = delta.tv_sec * 1000 + delta.tv_usec / 1000;
} }
@ -1485,7 +1490,8 @@ SP_API enum sp_return sp_wait(struct sp_event_set *event_set,
} }
} else if (result == 0) { } else if (result == 0) {
DEBUG("poll() timed out"); DEBUG("poll() timed out");
break; if (!timeout_overflow)
break;
} else { } else {
DEBUG("poll() completed"); DEBUG("poll() completed");
break; break;