From da10c78f9fa9b865ccc1a780a77f405dc093d04a Mon Sep 17 00:00:00 2001
From: yinbin <yinbin8@huawei.com>
Date: Mon, 15 Jul 2024 15:27:53 +0800
Subject: [PATCH] fix some error of NULL pointer

---
 src/lstack/api/lstack_epoll.c           | 23 ++++++++++++++++++-----
 src/lstack/core/lstack_lwip.c           | 15 ++++++++-------
 src/lstack/core/lstack_port_map.c       |  2 +-
 src/lstack/core/lstack_protocol_stack.c |  2 +-
 src/lstack/netif/lstack_ethdev.c        |  8 ++++++--
 src/lstack/netif/lstack_tx_cache.c      |  1 +
 6 files changed, 35 insertions(+), 16 deletions(-)

diff --git a/src/lstack/api/lstack_epoll.c b/src/lstack/api/lstack_epoll.c
index 7d00de7..566443e 100644
--- a/src/lstack/api/lstack_epoll.c
+++ b/src/lstack/api/lstack_epoll.c
@@ -740,6 +740,7 @@ static int32_t init_poll_wakeup_data(struct wakeup_poll *wakeup)
     wakeup->events = calloc(POLL_KERNEL_EVENTS, sizeof(struct epoll_event));
     if (wakeup->events == NULL) {
         free(wakeup->last_fds);
+        wakeup->last_fds = NULL;
         GAZELLE_RETURN(EINVAL);
     }
 
@@ -760,7 +761,7 @@ static int32_t init_poll_wakeup_data(struct wakeup_poll *wakeup)
     return 0;
 }
 
-static void resize_kernel_poll(struct wakeup_poll *wakeup, nfds_t nfds)
+static int resize_kernel_poll(struct wakeup_poll *wakeup, nfds_t nfds)
 {
     if (wakeup->last_fds) {
         free(wakeup->last_fds);
@@ -768,6 +769,7 @@ static void resize_kernel_poll(struct wakeup_poll *wakeup, nfds_t nfds)
     wakeup->last_fds = calloc(nfds, sizeof(struct pollfd));
     if (wakeup->last_fds == NULL) {
         LSTACK_LOG(ERR, LSTACK, "calloc failed errno=%d\n", errno);
+        return -1;
     }
 
     if (wakeup->events) {
@@ -776,9 +778,12 @@ static void resize_kernel_poll(struct wakeup_poll *wakeup, nfds_t nfds)
     wakeup->events = calloc(nfds, sizeof(struct epoll_event));
     if (wakeup->events == NULL) {
         LSTACK_LOG(ERR, LSTACK, "calloc failed errno=%d\n", errno);
+        free(wakeup->last_fds);
+        return -1;
     }
 
     wakeup->last_max_nfds = nfds;
+    return 0;
 }
 
 static void poll_bind_statck(struct wakeup_poll *wakeup, int32_t *stack_count)
@@ -811,14 +816,18 @@ static void update_kernel_poll(struct wakeup_poll *wakeup, uint32_t index, struc
     }
 }
 
-static void poll_init(struct wakeup_poll *wakeup, struct pollfd *fds, nfds_t nfds)
+static int poll_init(struct wakeup_poll *wakeup, struct pollfd *fds, nfds_t nfds)
 {
     int32_t stack_count[PROTOCOL_STACK_MAX] = {0};
     int32_t poll_change = 0;
+    int ret = 0;
 
     /* poll fds num more, recalloc fds size */
     if (nfds > wakeup->last_max_nfds) {
-        resize_kernel_poll(wakeup, nfds);
+        ret = resize_kernel_poll(wakeup, nfds);
+        if (ret < 0) {
+            return -1;
+        }
         poll_change = 1;
     }
 
@@ -855,13 +864,14 @@ static void poll_init(struct wakeup_poll *wakeup, struct pollfd *fds, nfds_t nfd
     }
 
     if (poll_change == 0) {
-        return;
+        return 0;
     }
     wakeup->last_nfds = nfds;
 
     if (get_global_cfg_params()->app_bind_numa) {
         poll_bind_statck(wakeup, stack_count);
     }
+    return 0;
 }
 
 int32_t lstack_poll(struct pollfd *fds, nfds_t nfds, int32_t timeout)
@@ -880,7 +890,10 @@ int32_t lstack_poll(struct pollfd *fds, nfds_t nfds, int32_t timeout)
         }
     }
 
-    poll_init(wakeup, fds, nfds);
+    if (poll_init(wakeup, fds, nfds) < 0) {
+        free(wakeup);
+        GAZELLE_RETURN(EINVAL);
+    }
 
     int32_t kernel_num = 0;
     int32_t lwip_num = 0;
diff --git a/src/lstack/core/lstack_lwip.c b/src/lstack/core/lstack_lwip.c
index 75ef5f6..4d73d44 100644
--- a/src/lstack/core/lstack_lwip.c
+++ b/src/lstack/core/lstack_lwip.c
@@ -333,7 +333,7 @@ static ssize_t do_app_write(struct lwip_sock *sock, struct pbuf *pbufs[], void *
     }
 
     /* reduce the branch in loop */
-    uint16_t copy_len = len - send_len;
+    size_t copy_len = len - send_len;
     rte_memcpy((char *)pbufs[i]->payload, (char *)buf + send_len, copy_len);
     pbufs[i]->tot_len = pbufs[i]->len = copy_len;
     send_len += copy_len;
@@ -1358,12 +1358,13 @@ void netif_poll(struct netif *netif)
 /* processes on same node handshake packet use this function */
 err_t netif_loop_output(struct netif *netif, struct pbuf *p)
 {
-    if (p != NULL) {
-        const struct ip_hdr *iphdr;
-        iphdr = (const struct ip_hdr *)p->payload;
-        if (IPH_PROTO(iphdr) == IP_PROTO_UDP) {
-            return udp_netif_loop_output(netif, p);
-        }
+    if (!p) {
+        return ERR_ARG;
+    }
+    const struct ip_hdr *iphdr;
+    iphdr = (const struct ip_hdr *)p->payload;
+    if (IPH_PROTO(iphdr) == IP_PROTO_UDP) {
+        return udp_netif_loop_output(netif, p);
     }
 
     struct tcp_pcb *pcb = p->pcb;
diff --git a/src/lstack/core/lstack_port_map.c b/src/lstack/core/lstack_port_map.c
index 5439394..ce9d8df 100644
--- a/src/lstack/core/lstack_port_map.c
+++ b/src/lstack/core/lstack_port_map.c
@@ -39,4 +39,4 @@ uint16_t port_map_get(uint16_t port)
     }
     pthread_mutex_unlock(&g_rule_map_mutex);
     return val;
-}
\ No newline at end of file
+}
diff --git a/src/lstack/core/lstack_protocol_stack.c b/src/lstack/core/lstack_protocol_stack.c
index d8bdd3c..d1bbf9b 100644
--- a/src/lstack/core/lstack_protocol_stack.c
+++ b/src/lstack/core/lstack_protocol_stack.c
@@ -1000,7 +1000,7 @@ void stack_broadcast_arp(struct rte_mbuf *mbuf, struct protocol_stack *cur_stack
             return;
         }
         copy_mbuf(mbuf_copy, mbuf);
-        virtio_tap_process_tx(stack->queue_id, mbuf_copy);
+        virtio_tap_process_tx(cur_stack->queue_id, mbuf_copy);
     }
     return;
 }
diff --git a/src/lstack/netif/lstack_ethdev.c b/src/lstack/netif/lstack_ethdev.c
index 5685d11..d4d0878 100644
--- a/src/lstack/netif/lstack_ethdev.c
+++ b/src/lstack/netif/lstack_ethdev.c
@@ -319,10 +319,14 @@ static err_t eth_dev_init(struct netif *netif)
 int32_t ethdev_init(struct protocol_stack *stack)
 {
     struct cfg_params *cfg = get_global_cfg_params();
-
+    int ret = 0;
+    
     vdev_dev_ops_init(&stack->dev_ops);
     if (cfg->send_cache_mode) {
-        tx_cache_init(stack->queue_id, stack, &stack->dev_ops);
+        ret = tx_cache_init(stack->queue_id, stack, &stack->dev_ops);
+        if (ret < 0) {
+            return ret;
+        }
     }
 
     if (use_ltran()) {
diff --git a/src/lstack/netif/lstack_tx_cache.c b/src/lstack/netif/lstack_tx_cache.c
index cda0003..9a48307 100644
--- a/src/lstack/netif/lstack_tx_cache.c
+++ b/src/lstack/netif/lstack_tx_cache.c
@@ -45,6 +45,7 @@ int tx_cache_init(uint16_t queue_id, void *priv, struct lstack_dev_ops *dev_ops)
     struct tx_cache *tx_cache = calloc(1, sizeof(struct tx_cache));
     if (tx_cache == NULL) {
         LSTACK_LOG(ERR, LSTACK, "queue(%d) tx cache init failed\n", queue_id);
+        return -1;
     }
 
     tx_cache->queue_id = queue_id;
-- 
2.33.0

