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 <stdio.h>
#include <stdarg.h>
#include <limits.h>
#ifdef _WIN32
#include <windows.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();
#else
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;
struct pollfd *pollfds;
unsigned int i;
@ -1461,7 +1463,8 @@ SP_API enum sp_return sp_wait(struct sp_event_set *event_set,
if (!timeout_ms) {
timeout_remaining_ms = -1;
} else if (!started) {
timeout_remaining_ms = timeout_ms;
timeout_overflow = (timeout_ms > INT_MAX);
timeout_remaining_ms = timeout_overflow ? INT_MAX : timeout_ms;
} else {
gettimeofday(&now, NULL);
if (timercmp(&now, &end, >)) {
@ -1469,6 +1472,8 @@ SP_API enum sp_return sp_wait(struct sp_event_set *event_set,
break;
}
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;
}
@ -1485,7 +1490,8 @@ SP_API enum sp_return sp_wait(struct sp_event_set *event_set,
}
} else if (result == 0) {
DEBUG("poll() timed out");
break;
if (!timeout_overflow)
break;
} else {
DEBUG("poll() completed");
break;