Skip to content

Commit aade493

Browse files
committed
Fix #453, Select API and unit test updates
Confirm that the "selectable" flag is set before calling the underlying select() API. Also update unit tests to match.
1 parent e237ab3 commit aade493

File tree

5 files changed

+53
-21
lines changed

5 files changed

+53
-21
lines changed

src/os/inc/osapi.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@
7777
#define OS_ERR_INCORRECT_OBJ_STATE (-35) /**< @brief Incorrect object state */
7878
#define OS_ERR_INCORRECT_OBJ_TYPE (-36) /**< @brief Incorrect object type */
7979
#define OS_ERR_STREAM_DISCONNECTED (-37) /**< @brief Stream disconnected */
80+
#define OS_ERR_OPERATION_NOT_SUPPORTED (-38) /**< @brief Requested operation is not support on the supplied object(s) */
8081
/**@}*/
8182

8283
/*

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

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ static int OS_FdSet_ConvertIn_Impl(fd_set *os_set, OS_FdSet *OSAL_set)
8282
{
8383
id = (offset * 8) + bit;
8484
osfd = OS_impl_filehandle_table[id].fd;
85-
if (osfd >= 0)
85+
if (osfd >= 0 && OS_impl_filehandle_table[id].selectable)
8686
{
8787
FD_SET(osfd, os_set);
8888
if (osfd > maxfd)
@@ -247,6 +247,15 @@ int32 OS_SelectSingle_Impl(uint32 stream_id, uint32 *SelectFlags, int32 msecs)
247247
fd_set wr_set;
248248
fd_set rd_set;
249249

250+
/*
251+
* If called on a stream_id which does not support this
252+
* operation, return immediately and do not invoke the system call
253+
*/
254+
if (!OS_impl_filehandle_table[stream_id].selectable)
255+
{
256+
return OS_ERR_OPERATION_NOT_SUPPORTED;
257+
}
258+
250259
if (*SelectFlags != 0)
251260
{
252261
FD_ZERO(&wr_set);
@@ -304,6 +313,13 @@ int32 OS_SelectMultiple_Impl(OS_FdSet *ReadSet, OS_FdSet *WriteSet, int32 msecs)
304313
int maxfd;
305314
int32 return_code;
306315

316+
/*
317+
* This return code will be used if the set(s) do not
318+
* contain any file handles capable of select(). It
319+
* will be overwritten with the real result of the
320+
* select call, if selectable file handles were specified.
321+
*/
322+
return_code = OS_ERR_OPERATION_NOT_SUPPORTED;
307323
FD_ZERO(&rd_set);
308324
FD_ZERO(&wr_set);
309325
maxfd = -1;
@@ -324,7 +340,10 @@ int32 OS_SelectMultiple_Impl(OS_FdSet *ReadSet, OS_FdSet *WriteSet, int32 msecs)
324340
}
325341
}
326342

327-
return_code = OS_DoSelect(maxfd, &rd_set, &wr_set, msecs);
343+
if (maxfd >= 0)
344+
{
345+
return_code = OS_DoSelect(maxfd, &rd_set, &wr_set, msecs);
346+
}
328347

329348
if (return_code == OS_SUCCESS)
330349
{

src/os/shared/inc/os-shared-select.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
Bits in "SelectFlags" will be unset according to activity
4343
4444
Returns: OS_SUCCESS on success, or relevant error code
45+
OS_ERR_OPERATION_NOT_SUPPORTED if the specified file handle does not support select
4546
------------------------------------------------------------------*/
4647
int32 OS_SelectSingle_Impl(uint32 stream_id, uint32 *SelectFlags, int32 msecs);
4748

@@ -66,6 +67,7 @@ int32 OS_SelectSingle_Impl(uint32 stream_id, uint32 *SelectFlags, int32 msecs);
6667
File descriptors in sets be modified according to activity
6768
6869
Returns: OS_SUCCESS on success, or relevant error code
70+
OS_ERR_OPERATION_NOT_SUPPORTED if the specified file handle(s) do not support select
6971
------------------------------------------------------------------*/
7072
int32 OS_SelectMultiple_Impl(OS_FdSet *ReadSet, OS_FdSet *WriteSet, int32 msecs);
7173

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
*
1818
*/
1919
#include "os-portable-coveragetest.h"
20+
#include "ut-adaptor-portable-posix-io.h"
2021
#include "os-shared-select.h"
2122

2223
#include <OCS_sys_select.h>
@@ -32,7 +33,10 @@ void Test_OS_SelectSingle_Impl(void)
3233
struct OCS_timespec latertime;
3334

3435
StreamID = 0;
36+
UT_PortablePosixIOTest_Set_Selectable(0, false);
3537
SelectFlags = OS_STREAM_STATE_READABLE | OS_STREAM_STATE_WRITABLE;
38+
OSAPI_TEST_FUNCTION_RC(OS_SelectSingle_Impl, (StreamID, &SelectFlags, 0), OS_ERR_OPERATION_NOT_SUPPORTED);
39+
UT_PortablePosixIOTest_Set_Selectable(0, true);
3640
OSAPI_TEST_FUNCTION_RC(OS_SelectSingle_Impl, (StreamID, &SelectFlags, 0), OS_SUCCESS);
3741
SelectFlags = OS_STREAM_STATE_READABLE | OS_STREAM_STATE_WRITABLE;
3842
OSAPI_TEST_FUNCTION_RC(OS_SelectSingle_Impl, (StreamID, &SelectFlags, -1), OS_SUCCESS);

src/unit-tests/oscore-test/ut_oscore_select_test.c

Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@
4343
char *fsAddrPtr = NULL;
4444
static int32 setup_file(void)
4545
{
46-
OS_mkfs(fsAddrPtr, "/ramdev3", " ", 512, 20);
47-
OS_mount("/ramdev3", "/drive3");
46+
UT_SETUP(OS_mkfs(fsAddrPtr, "/ramdev3", "RAM3", 512, 20));
47+
UT_SETUP(OS_mount("/ramdev3", "/drive3"));
4848
return OS_creat("/drive3/select_test.txt", OS_READ_WRITE);
4949
}
5050

@@ -105,20 +105,25 @@ void UT_os_select_single_test(void)
105105
{
106106
uint32 StateFlags;
107107
int32 fd = setup_file();
108+
int32 rc;
108109

109-
if(OS_SelectSingle(fd, &StateFlags, 0) == OS_ERR_NOT_IMPLEMENTED)
110+
UT_RETVAL(OS_SelectSingle(fd, NULL, 0), OS_INVALID_POINTER, "NULL flags pointer");
111+
112+
StateFlags = OS_STREAM_STATE_WRITABLE;
113+
rc = OS_SelectSingle(fd, &StateFlags, 0);
114+
if(rc == OS_ERR_NOT_IMPLEMENTED || rc == OS_ERR_OPERATION_NOT_SUPPORTED)
110115
{
111-
UtAssertEx(false, UTASSERT_CASETYPE_NA, __FILE__, __LINE__, "OS_SelectSingle() not implemented");
116+
UtAssertEx(false, UTASSERT_CASETYPE_NA, __FILE__, __LINE__, "OS_SelectSingle() not supported");
112117
goto UT_os_select_single_test_exit_tag;
113118
}
114119

115-
UtAssert_Simple(OS_SelectSingle(fd, NULL, 0) != OS_SUCCESS);
116-
117-
StateFlags = OS_STREAM_STATE_WRITABLE;
118-
UtAssert_Simple(OS_SelectSingle(fd, &StateFlags, 0) == OS_SUCCESS && StateFlags & OS_STREAM_STATE_WRITABLE);
120+
UT_RETVAL(rc, OS_SUCCESS, "OS_SelectSingle(fd, OS_STREAM_STATE_WRITABLE, 0)");
121+
UtAssert_True((StateFlags & OS_STREAM_STATE_WRITABLE) != 0, "StateFlags (0x%x) & OS_STREAM_STATE_WRITABLE", (unsigned int)StateFlags);
119122

120123
StateFlags = OS_STREAM_STATE_READABLE;
121-
UtAssert_Simple(OS_SelectSingle(fd, &StateFlags, 1) == OS_SUCCESS);
124+
rc = OS_SelectSingle(fd, &StateFlags, 1);
125+
UT_RETVAL(rc, OS_SUCCESS, "OS_SelectSingle(fd, OS_STREAM_STATE_READABLE, 0)");
126+
UtAssert_True((StateFlags & OS_STREAM_STATE_READABLE) != 0, "StateFlags (0x%x) & OS_STREAM_STATE_READABLE", (unsigned int)StateFlags);
122127

123128
UT_os_select_single_test_exit_tag:
124129
teardown_file(fd);
@@ -135,25 +140,26 @@ void UT_os_select_multi_test(void)
135140
{
136141
OS_FdSet ReadSet, WriteSet;
137142
int32 fd = setup_file();
143+
int32 rc;
138144

139-
if(OS_SelectMultiple(&ReadSet, &WriteSet, 1) == OS_ERR_NOT_IMPLEMENTED)
145+
OS_SelectFdZero(&WriteSet);
146+
OS_SelectFdAdd(&WriteSet, fd);
147+
rc = OS_SelectMultiple(NULL, &WriteSet, 1);
148+
if(rc == OS_ERR_NOT_IMPLEMENTED || rc == OS_ERR_OPERATION_NOT_SUPPORTED)
140149
{
141-
UtAssertEx(false, UTASSERT_CASETYPE_NA, __FILE__, __LINE__, "OS_SelectMultiple() not implemented");
150+
UtAssertEx(false, UTASSERT_CASETYPE_NA, __FILE__, __LINE__, "OS_SelectMultiple() not supported");
142151
goto UT_select_multi_test_exit_tag;
143152
}
144153

145-
OS_SelectFdZero(&WriteSet);
146-
OS_SelectFdAdd(&WriteSet, fd);
147-
UtAssert_Simple(OS_SelectMultiple(NULL, &WriteSet, 1) == OS_SUCCESS);
148154

149-
OS_SelectFdZero(&ReadSet);
150-
OS_SelectFdAdd(&ReadSet, fd);
151-
UtAssert_Simple(OS_SelectMultiple(&ReadSet, NULL, 1) == OS_SUCCESS);
155+
UT_RETVAL(rc, OS_SUCCESS, "OS_SelectMultiple(NULL, &WriteSet, 1)");
156+
UtAssert_True(OS_SelectFdIsSet(&WriteSet, fd), "OS_SelectFdIsSet(&WriteSet, fd)");
152157

153158
OS_SelectFdZero(&ReadSet);
154159
OS_SelectFdAdd(&ReadSet, fd);
155-
OS_SelectFdZero(&WriteSet);
156-
UtAssert_Simple(OS_SelectMultiple(&ReadSet, &WriteSet, 0) == OS_SUCCESS);
160+
rc = OS_SelectMultiple(&ReadSet, NULL, 1);
161+
UT_RETVAL(rc, OS_SUCCESS, "OS_SelectMultiple(&ReadSet, NULL, 1)");
162+
UtAssert_True(OS_SelectFdIsSet(&ReadSet, fd), "!OS_SelectFdIsSet(&ReadSet, fd)");
157163

158164
UT_select_multi_test_exit_tag:
159165
teardown_file(fd);

0 commit comments

Comments
 (0)