From 3e4b3c1340266a2e6213d993accce21e62701377 Mon Sep 17 00:00:00 2001 From: zhangkun Date: Tue, 25 Nov 2025 14:09:50 +0800 Subject: [PATCH] fix: add null pointer checks after malloc calls MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Added null pointer checks after malloc calls in three locations to prevent potential segmentation faults and improve error handling. When malloc fails to allocate memory, the code now logs an appropriate error message and returns early instead of proceeding with null pointers. 1. Added check for check_context.conflic_mac allocation in ipwd_analyse function 2. Added check for newdevinfo allocation in ipwd_analyse function 3. Added check for ipconflict_dev_info allocation in ipwd_read_config function Influence: 1. Test memory allocation failure scenarios to verify error messages are logged correctly 2. Verify that null pointer checks prevent crashes when malloc fails 3. Test normal operation to ensure memory allocation still works correctly 4. Check error handling behavior when system is under memory pressure fix: 为malloc调用添加空指针检查 在三个位置为malloc调用添加了空指针检查,以防止潜在的段错误并改进错误处 理。当malloc分配内存失败时,代码现在会记录适当的错误消息并提前返回,而不 是继续使用空指针。 1. 在ipwd_analyse函数中添加了对check_context.conflic_mac分配的检查 2. 在ipwd_analyse函数中添加了对newdevinfo分配的检查 3. 在ipwd_read_config函数中添加了对ipconflict_dev_info分配的检查 Influence: 1. 测试内存分配失败场景,验证错误消息是否正确记录 2. 验证当malloc失败时空指针检查是否能防止崩溃 3. 测试正常操作以确保内存分配仍然正常工作 4. 检查系统内存压力下的错误处理行为 --- src/plugin-sdbus/plugin-ipwatchd/analyse.c | 8 ++++++++ src/plugin-sdbus/plugin-ipwatchd/config.c | 5 +++++ 2 files changed, 13 insertions(+) diff --git a/src/plugin-sdbus/plugin-ipwatchd/analyse.c b/src/plugin-sdbus/plugin-ipwatchd/analyse.c index c791d471..d89221cd 100644 --- a/src/plugin-sdbus/plugin-ipwatchd/analyse.c +++ b/src/plugin-sdbus/plugin-ipwatchd/analyse.c @@ -148,6 +148,10 @@ void ipwd_analyse (u_char * args, const struct pcap_pkthdr *header, const u_char ipwd_message (IPWD_MSG_TYPE_INFO, "IP conflict with: %s %s-%s", check_context.ip, check_context.dev.mac, rcv_smac); check_context.conflic_mac = (char*)malloc(IPWD_MAX_DEVICE_ADDRESS_LEN); + if (check_context.conflic_mac == NULL) { + ipwd_message (IPWD_MSG_TYPE_ERROR, "Unable to allocate memory for conflic_mac - malloc failed"); + return; + } memcpy(check_context.conflic_mac,rcv_smac,IPWD_MAX_DEVICE_ADDRESS_LEN); } } @@ -255,6 +259,10 @@ void ipwd_analyse (u_char * args, const struct pcap_pkthdr *header, const u_char if (exist == 0) { IPCONFLICT_DEV_INFO *newdevinfo = (IPCONFLICT_DEV_INFO *)malloc(sizeof(IPCONFLICT_DEV_INFO)); + if (newdevinfo == NULL) { + ipwd_message (IPWD_MSG_TYPE_ERROR, "Unable to allocate memory for IPCONFLICT_DEV_INFO - malloc failed"); + return; + } memcpy (newdevinfo->ip, devices.dev[i].ip, IPWD_MAX_DEVICE_ADDRESS_LEN); memcpy (newdevinfo->mac, devices.dev[i].mac, IPWD_MAX_DEVICE_ADDRESS_LEN); memcpy (newdevinfo->remote_mac, rcv_smac, IPWD_MAX_DEVICE_ADDRESS_LEN); diff --git a/src/plugin-sdbus/plugin-ipwatchd/config.c b/src/plugin-sdbus/plugin-ipwatchd/config.c index 88ad2058..9af357c4 100644 --- a/src/plugin-sdbus/plugin-ipwatchd/config.c +++ b/src/plugin-sdbus/plugin-ipwatchd/config.c @@ -85,6 +85,11 @@ int ipwd_read_config (const char *filename) devices.devnum = 0; ipconflict_dev_info = (IPCONFLICT_DEV_INFO *)malloc(sizeof(IPCONFLICT_DEV_INFO)); + if (ipconflict_dev_info == NULL) + { + ipwd_message (IPWD_MSG_TYPE_ERROR, "Unable to allocate memory for ipconflict_dev_info - malloc failed"); + return (IPWD_RV_ERROR); + } memset(ipconflict_dev_info->ip, 0, IPWD_MAX_DEVICE_ADDRESS_LEN); memset(ipconflict_dev_info->mac, 0, IPWD_MAX_DEVICE_ADDRESS_LEN); memset(ipconflict_dev_info->remote_mac, 0, IPWD_MAX_DEVICE_ADDRESS_LEN);