Skip to content

Commit ebcea77

Browse files
committed
Fix #857, correct interval calculation in DoSelect
When calculating the relative time interval for the select() call, the increment should have been a decrement. This also adds a UT case that specifically exercises the carryover described.
1 parent 3b53af0 commit ebcea77

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

src/os/portable/os-impl-bsd-select.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ static int32 OS_DoSelect(int maxfd, fd_set *rd_set, fd_set *wr_set, int32 msecs)
214214
if (tv.tv_usec < 0)
215215
{
216216
tv.tv_usec += 1000000;
217-
++tv.tv_sec;
217+
--tv.tv_sec;
218218
}
219219
}
220220

src/unit-test-coverage/portable/src/coveragetest-bsd-select.c

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
#include "os-shared-idmap.h"
3030

3131
#include <OCS_sys_select.h>
32+
#include <OCS_errno.h>
3233

3334
void Test_OS_SelectSingle_Impl(void)
3435
{
@@ -39,6 +40,7 @@ void Test_OS_SelectSingle_Impl(void)
3940
OS_object_token_t token;
4041
struct OCS_timespec nowtime;
4142
struct OCS_timespec latertime;
43+
struct OCS_timespec latertime2;
4244

4345
memset(&token, 0, sizeof(token));
4446

@@ -52,6 +54,23 @@ void Test_OS_SelectSingle_Impl(void)
5254
SelectFlags = 0;
5355
OSAPI_TEST_FUNCTION_RC(OS_SelectSingle_Impl, (&token, &SelectFlags, 0), OS_SUCCESS);
5456

57+
/* try a case where select() needs to be repeated to achieve the desired wait time */
58+
UT_SetDefaultReturnValue(UT_KEY(OCS_select), -1);
59+
OCS_errno = OCS_EINTR;
60+
UT_SetDeferredRetcode(UT_KEY(OCS_select), 2, 0);
61+
SelectFlags = OS_STREAM_STATE_READABLE | OS_STREAM_STATE_WRITABLE;
62+
nowtime.tv_sec = 1;
63+
nowtime.tv_nsec = 0;
64+
latertime.tv_sec = 1;
65+
latertime.tv_nsec = 800000000;
66+
latertime2.tv_sec = 2;
67+
latertime2.tv_nsec = 200000000;
68+
UT_SetDataBuffer(UT_KEY(OCS_clock_gettime), &nowtime, sizeof(nowtime), false);
69+
UT_SetDataBuffer(UT_KEY(OCS_clock_gettime), &nowtime, sizeof(nowtime), false);
70+
UT_SetDataBuffer(UT_KEY(OCS_clock_gettime), &latertime, sizeof(latertime), false);
71+
UT_SetDataBuffer(UT_KEY(OCS_clock_gettime), &latertime2, sizeof(latertime2), false);
72+
OSAPI_TEST_FUNCTION_RC(OS_SelectSingle_Impl, (&token, &SelectFlags, 1200), OS_ERROR_TIMEOUT);
73+
5574
UT_SetDefaultReturnValue(UT_KEY(OCS_select), 0);
5675
SelectFlags = OS_STREAM_STATE_READABLE | OS_STREAM_STATE_WRITABLE;
5776
nowtime.tv_sec = 1;
@@ -63,6 +82,7 @@ void Test_OS_SelectSingle_Impl(void)
6382
OSAPI_TEST_FUNCTION_RC(OS_SelectSingle_Impl, (&token, &SelectFlags, 999), OS_ERROR_TIMEOUT);
6483

6584
UT_SetDefaultReturnValue(UT_KEY(OCS_select), -1);
85+
OCS_errno = OCS_ETIMEDOUT;
6686
SelectFlags = OS_STREAM_STATE_READABLE | OS_STREAM_STATE_WRITABLE;
6787
nowtime.tv_sec = 1;
6888
nowtime.tv_nsec = 0;

0 commit comments

Comments
 (0)