From f67317cc61f686bc68c79019a7e1686eab160c5e Mon Sep 17 00:00:00 2001 From: Lemmy Huang Date: Fri, 2 Jun 2023 14:35:16 +0800 Subject: [PATCH] fix coredump caused by lwip_get_socket_nouse Signed-off-by: Lemmy Huang --- src/lstack/api/lstack_epoll.c | 2 +- src/lstack/api/lstack_wrap.c | 2 +- src/lstack/core/lstack_lwip.c | 6 +++--- src/lstack/core/lstack_protocol_stack.c | 15 +++++++++------ 4 files changed, 14 insertions(+), 11 deletions(-) diff --git a/src/lstack/api/lstack_epoll.c b/src/lstack/api/lstack_epoll.c index 64c3600..621762d 100644 --- a/src/lstack/api/lstack_epoll.c +++ b/src/lstack/api/lstack_epoll.c @@ -313,7 +313,7 @@ int32_t lstack_epoll_ctl(int32_t epfd, int32_t op, int32_t fd, struct epoll_even struct wakeup_poll *wakeup = epoll_sock->wakeup; struct lwip_sock *sock = lwip_get_socket_nouse(fd); - if (sock == NULL) { + if (sock == NULL || sock->conn == NULL) { return posix_api->epoll_ctl_fn(epfd, op, fd, event); } diff --git a/src/lstack/api/lstack_wrap.c b/src/lstack/api/lstack_wrap.c index 776646f..1f04ae2 100644 --- a/src/lstack/api/lstack_wrap.c +++ b/src/lstack/api/lstack_wrap.c @@ -288,7 +288,7 @@ static int32_t do_connect(int32_t s, const struct sockaddr *name, socklen_t name } sock = lwip_get_socket_nouse(s); - if (sock == NULL) { + if (sock == NULL || sock->conn == NULL) { return posix_api->connect_fn(s, name, namelen); } diff --git a/src/lstack/core/lstack_lwip.c b/src/lstack/core/lstack_lwip.c index 62b4838..2e40862 100644 --- a/src/lstack/core/lstack_lwip.c +++ b/src/lstack/core/lstack_lwip.c @@ -173,7 +173,7 @@ void gazelle_init_sock(int32_t fd) static _Atomic uint32_t name_tick = 0; struct protocol_stack *stack = get_protocol_stack(); struct lwip_sock *sock = lwip_get_socket_nouse(fd); - if (sock == NULL) { + if (sock == NULL || sock->conn == NULL) { return; } @@ -687,7 +687,7 @@ void stack_send(struct rpc_msg *msg) bool replenish_again; struct lwip_sock *sock = lwip_get_socket_nouse(fd); - if (sock == NULL) { + if (sock == NULL || sock->conn == NULL) { msg->result = -1; LSTACK_LOG(ERR, LSTACK, "stack_send: sock error!\n"); rpc_msg_free(msg); @@ -1251,7 +1251,7 @@ int32_t gazelle_socket(int domain, int type, int protocol) gazelle_init_sock(fd); struct lwip_sock *sock = lwip_get_socket_nouse(fd); - if (sock == NULL || sock->stack == NULL) { + if (sock == NULL || sock->conn == NULL || sock->stack == NULL) { lwip_close(fd); gazelle_clean_sock(fd); posix_api->close_fn(fd); diff --git a/src/lstack/core/lstack_protocol_stack.c b/src/lstack/core/lstack_protocol_stack.c index ce1c6de..8638cd5 100644 --- a/src/lstack/core/lstack_protocol_stack.c +++ b/src/lstack/core/lstack_protocol_stack.c @@ -108,7 +108,7 @@ struct protocol_stack *get_protocol_stack(void) struct protocol_stack *get_protocol_stack_by_fd(int32_t fd) { struct lwip_sock *sock = lwip_get_socket_nouse(fd); - if (sock == NULL) { + if (sock == NULL || sock->conn == NULL) { return NULL; } @@ -683,7 +683,7 @@ void stack_accept(struct rpc_msg *msg) } struct lwip_sock *sock = lwip_get_socket_nouse(accept_fd); - if (sock == NULL || sock->stack == NULL) { + if (sock == NULL || sock->conn == NULL || sock->stack == NULL) { lwip_close(accept_fd); gazelle_clean_sock(accept_fd); posix_api->close_fn(accept_fd); @@ -815,7 +815,7 @@ int32_t stack_broadcast_close(int32_t fd) struct lwip_sock *sock = lwip_get_socket_nouse(fd); int32_t ret = 0; - if (sock == NULL) { + if (sock == NULL || sock->conn == NULL) { return -1; } @@ -850,7 +850,7 @@ int32_t stack_broadcast_listen(int32_t fd, int32_t backlog) int32_t ret, clone_fd; struct lwip_sock *sock = lwip_get_socket_nouse(fd); - if (sock == NULL) { + if (sock == NULL || sock->conn == NULL) { LSTACK_LOG(ERR, LSTACK, "tid %ld, %d get sock null\n", get_stack_tid(), fd); GAZELLE_RETURN(EINVAL); } @@ -895,8 +895,11 @@ int32_t stack_broadcast_listen(int32_t fd, int32_t backlog) static struct lwip_sock *get_min_accept_sock(int32_t fd) { - struct lwip_sock *sock = lwip_get_socket_nouse(fd); struct lwip_sock *min_sock = NULL; + struct lwip_sock *sock = lwip_get_socket_nouse(fd); + if (sock == NULL || sock->conn == NULL) { + return NULL; + } while (sock) { if (!NETCONN_IS_ACCEPTIN(sock)) { @@ -934,7 +937,7 @@ int32_t stack_broadcast_accept4(int32_t fd, struct sockaddr *addr, socklen_t *ad int32_t ret = -1; struct lwip_sock *sock = lwip_get_socket_nouse(fd); - if (sock == NULL) { + if (sock == NULL || sock->conn == NULL) { errno = EINVAL; return -1; } -- 2.23.0