Skip to content

Commit

Permalink
api_msg: Fix crash to fail-safe error if cannot get semaphore
Browse files Browse the repository at this point in the history
  • Loading branch information
david-cermak committed Mar 20, 2024
1 parent f792214 commit a1bd9e4
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/api/api_lib.c
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,9 @@ netconn_apimsg(tcpip_callback_fn fn, struct api_msg *apimsg)

#if LWIP_NETCONN_SEM_PER_THREAD
apimsg->op_completed_sem = LWIP_NETCONN_THREAD_SEM_GET();
if (!sys_sem_valid(apimsg->op_completed_sem)) {
return ERR_MEM;
}
#endif /* LWIP_NETCONN_SEM_PER_THREAD */

err = tcpip_send_msg_wait_sem(fn, apimsg, LWIP_API_MSG_SEM(apimsg));
Expand Down Expand Up @@ -1314,6 +1317,9 @@ netconn_gethostbyname(const char *name, ip_addr_t *addr)
#endif /* LWIP_IPV4 && LWIP_IPV6 */
#if LWIP_NETCONN_SEM_PER_THREAD
API_VAR_REF(msg).sem = LWIP_NETCONN_THREAD_SEM_GET();
if (!sys_sem_valid(API_VAR_REF(msg).sem)) {
return ERR_MEM;
}
#else /* LWIP_NETCONN_SEM_PER_THREAD*/
err = sys_sem_new(API_EXPR_REF(API_VAR_REF(msg).sem), 0);
if (err != ERR_OK) {
Expand Down
9 changes: 9 additions & 0 deletions src/api/sockets.c
Original file line number Diff line number Diff line change
Expand Up @@ -2029,6 +2029,10 @@ lwip_select(int maxfdp1, fd_set *readset, fd_set *writeset, fd_set *exceptset,
API_SELECT_CB_VAR_REF(select_cb).exceptset = exceptset;
#if LWIP_NETCONN_SEM_PER_THREAD
API_SELECT_CB_VAR_REF(select_cb).sem = LWIP_NETCONN_THREAD_SEM_GET();
if (!sys_sem_valid(API_SELECT_CB_VAR_REF(select_cb).sem)) {
set_errno(ENOMEM);
return -1;
}
#else /* LWIP_NETCONN_SEM_PER_THREAD */
if (sys_sem_new(&API_SELECT_CB_VAR_REF(select_cb).sem, 0) != ERR_OK) {
/* failed to create semaphore */
Expand Down Expand Up @@ -2374,6 +2378,11 @@ lwip_poll(struct pollfd *fds, nfds_t nfds, int timeout)
API_SELECT_CB_VAR_REF(select_cb).poll_nfds = nfds;
#if LWIP_NETCONN_SEM_PER_THREAD
API_SELECT_CB_VAR_REF(select_cb).sem = LWIP_NETCONN_THREAD_SEM_GET();
if (!sys_sem_valid(API_SELECT_CB_VAR_REF(select_cb).sem)) {
set_errno(ENOMEM);
return -1;
}

#else /* LWIP_NETCONN_SEM_PER_THREAD */
if (sys_sem_new(&API_SELECT_CB_VAR_REF(select_cb).sem, 0) != ERR_OK) {
/* failed to create semaphore */
Expand Down

0 comments on commit a1bd9e4

Please sign in to comment.