From 6f5749f26c431b37f4ee111f2f347f25102a1d93 Mon Sep 17 00:00:00 2001 From: shaozhimin Date: Thu, 25 Apr 2024 20:20:27 +0800 Subject: [PATCH] =?UTF-8?q?=E8=A7=A3=E5=86=B3=E7=BD=91=E5=8D=A1=E6=AE=B5?= =?UTF-8?q?=E9=94=99=E8=AF=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/hardware/libkync.c | 56 +++++++++++++++++++++++------------------- 1 file changed, 31 insertions(+), 25 deletions(-) diff --git a/src/hardware/libkync.c b/src/hardware/libkync.c index 6105b98..0b88b0b 100644 --- a/src/hardware/libkync.c +++ b/src/hardware/libkync.c @@ -837,6 +837,7 @@ char *kdk_nc_get_duplex(char *nc) { char path[1024] = {0}, real_path[1024]; char res[1024] = {0}; + char *result = NULL; sprintf(path, "/sys/class/net/%s/duplex", nc); if (NULL == realpath(path, real_path)) return NULL; @@ -845,7 +846,8 @@ char *kdk_nc_get_duplex(char *nc) return NULL; fgets(res, 1024, fp); strstripspace(res); - char *result = strdup(res); + if(strlen(res) != 0) + result = strdup(res); fclose(fp); return result; } @@ -962,14 +964,6 @@ char *kdk_nc_get_broadAddr(const char *eth_name, const char *address) memset(ip, 0, 32); strcpy(ip, address); - char *broadAddr = (char *)malloc(sizeof(char) * 64); - if(!broadAddr) - { - free(ip); - return NULL; - } - memset(broadAddr, 0, 64); - struct nl_sock *sk = nl_socket_alloc(); nl_connect(sk, NETLINK_ROUTE); @@ -997,8 +991,19 @@ char *kdk_nc_get_broadAddr(const char *eth_name, const char *address) rtnl_link_put(link); free(ip); - strcpy(broadAddr, ctx.broadaddr); - free(ctx.broadaddr); + char *broadAddr = NULL; + + if(ctx.broadaddr) + { + broadAddr = (char *)malloc(sizeof(ctx.broadaddr) + 1); + if(!broadAddr) + { + return NULL; + } + memset(broadAddr, 0, sizeof(ctx.broadaddr) + 1); + strcpy(broadAddr, ctx.broadaddr); + free(ctx.broadaddr); + } nl_close(sk); nl_socket_free(sk); return broadAddr; @@ -1007,7 +1012,6 @@ error_out: nl_close(sk); nl_socket_free(sk); free(ip); - free(broadAddr); return NULL; } @@ -1020,21 +1024,15 @@ char *kdk_nc_get_netmask(const char *eth_name, const char *addr) int ret = 0; struct nl_cache *addr_cache; struct rtnl_link *link; - char *ip = (char *)malloc(sizeof(char) * 32); + char *ip = (char *)malloc(sizeof(addr) + 1); if(!ip) { return NULL; } - memset(ip, 0, 32); + memset(ip, 0, sizeof(addr) + 1); strcpy(ip, addr); - char *netmask = (char *)malloc(sizeof(char) * 64); - if(!netmask) - { - free(ip); - return NULL; - } - memset(netmask, 0, 64); + char *netmask = NULL; struct nl_sock *sk = nl_socket_alloc(); nl_connect(sk, NETLINK_ROUTE); @@ -1062,9 +1060,18 @@ char *kdk_nc_get_netmask(const char *eth_name, const char *addr) nl_cache_put(addr_cache); rtnl_link_put(link); free(ip); - - strcpy(netmask, ctx.netmask); - free(ctx.netmask); + if(ctx.netmask) + { + netmask = (char *)malloc(sizeof(char) * 64); + if(!netmask) + { + return NULL; + } + memset(netmask, 0, 64); + strcpy(netmask, ctx.netmask); + free(ctx.netmask); + } + nl_close(sk); nl_socket_free(sk); return netmask; @@ -1073,7 +1080,6 @@ error_out: nl_close(sk); nl_socket_free(sk); free(ip); - free(netmask); return NULL; }