From cbeb07ef6238a719a2bb84837835ebc228ac4fde Mon Sep 17 00:00:00 2001
From: jiangheng <jiangheng14@huawei.com>
Date: Tue, 20 Jun 2023 14:56:28 +0800
Subject: [PATCH] add error check in hugepage_init and sys_mbox_free

---
 src/api/sys_arch.c     |  8 ++++++--
 src/include/memp_def.h | 13 ++++++++++++-
 2 files changed, 18 insertions(+), 3 deletions(-)

diff --git a/src/api/sys_arch.c b/src/api/sys_arch.c
index f93a00e..1bc3aee 100644
--- a/src/api/sys_arch.c
+++ b/src/api/sys_arch.c
@@ -124,8 +124,12 @@ err_t sys_mbox_new(struct sys_mbox **mb, int size)
 void sys_mbox_free(struct sys_mbox **mb)
 {
     struct sys_mbox *mbox = *mb;
-    rte_ring_free(mbox->ring);
+    if (mbox->ring != NULL) {
+        rte_ring_free(mbox->ring);
+        mbox->ring = NULL;
+    }
     memp_free(MEMP_SYS_MBOX, mbox);
+    sys_mbox_set_invalid(mb);
 }
 
 err_t sys_mbox_trypost(struct sys_mbox **mb, void *msg)
@@ -371,7 +375,7 @@ uint8_t *sys_hugepage_malloc(const char *name, uint32_t size)
 
     mz = rte_memzone_reserve(name, size, rte_socket_id(), 0);
     if (mz == NULL) {
-        rte_exit(EXIT_FAILURE, "failed to reserver memory for mempool[%s]\n", name);
+        LWIP_DEBUGF(SYS_DEBUG, ("sys_hugepage_malloc: failed to reserve memory for mempool\n"));
         return NULL;
     }
 
diff --git a/src/include/memp_def.h b/src/include/memp_def.h
index 082f685..3408c60 100644
--- a/src/include/memp_def.h
+++ b/src/include/memp_def.h
@@ -52,15 +52,26 @@
 #include <lwip/priv/memp_std.h>
 #undef LWIP_MEMPOOL
 
-static inline void hugepage_init(void)
+extern PER_THREAD uint8_t *ram_heap;
+static inline int hugepage_init(void)
 {
 #define LWIP_MEMPOOL(name,num,size,desc)  LWIP_MEMPOOL_BASE_INIT(name)
 #include "lwip/priv/memp_std.h"
+    u16_t i;
+    for (i = 0; i < LWIP_ARRAYSIZE(memp_pools); i++) {
+        if (memp_pools[i]->base == NULL) {
+            return -1;
+        }
+    }
 
 #if !MEM_LIBC_MALLOC
 	LWIP_MEM_MEMORY_DECLARE(ram_heap)
     LWIP_MEM_MEMORY_INIT(ram_heap)
+    if (ram_heap == NULL) {
+        return -1;
+    }
 #endif /* MEM_LIBC_MALLOC */
+    return 0;
 }
 
 #endif /* __MEMP_DEF_H__ */
-- 
2.27.0

