From c34d9a5b98597c1ac029fc6c2fcde00b3bc4839b Mon Sep 17 00:00:00 2001 From: jiangheng12 Date: Fri, 9 Jun 2023 11:22:03 +0800 Subject: [PATCH] add exception handling for is_dst_ip_localhost --- src/lstack/api/lstack_wrap.c | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/src/lstack/api/lstack_wrap.c b/src/lstack/api/lstack_wrap.c index 505e33d..7245873 100644 --- a/src/lstack/api/lstack_wrap.c +++ b/src/lstack/api/lstack_wrap.c @@ -242,12 +242,21 @@ static int32_t do_bind(int32_t s, const struct sockaddr *name, socklen_t namelen bool is_dst_ip_localhost(const struct sockaddr *addr) { struct sockaddr_in *servaddr = (struct sockaddr_in *) addr; - FILE *ifh = fopen("/proc/net/dev", "r"); char *line = NULL; char *p; size_t linel = 0; int linenum = 0; + FILE *ifh = fopen("/proc/net/dev", "r"); + if (ifh == NULL) { + LSTACK_LOG(ERR, LSTACK, "failed to open /proc/net/dev, errno is %d\n", errno); + return false; + } struct sockaddr_in* sin = malloc(sizeof(struct sockaddr_in)); + if (sin == NULL) { + LSTACK_LOG(ERR, LSTACK, "sockaddr_in malloc failed\n"); + fclose(ifh); + return false; + } while (getdelim(&line, &linel, '\n', ifh) > 0) { /* 2: skip the first two lines, which are not nic name */ @@ -268,13 +277,16 @@ bool is_dst_ip_localhost(const struct sockaddr *addr) int ret = get_addr(sin, interface); if (ret == 0) { if (sin->sin_addr.s_addr == servaddr->sin_addr.s_addr) { - return 1; + free(sin); + fclose(ifh); + return true; } } } free(sin); + fclose(ifh); - return 0; + return false; } static int32_t do_connect(int32_t s, const struct sockaddr *name, socklen_t namelen) -- 2.23.0