From 079dd9b09fd389b91ee80e537e0a31ba549e2dde Mon Sep 17 00:00:00 2001 From: Jan Kneschke Date: Sun, 9 Aug 2009 14:22:39 +0200 Subject: [PATCH 1/2] (SIGAR-137) sdl_data isn't 0-terminated fixed 2 more places where sdl_data is used as it would be 0-term'ed and doesn't honour sdl_nlen --- src/os/darwin/darwin_sigar.c | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/src/os/darwin/darwin_sigar.c b/src/os/darwin/darwin_sigar.c index 8a665a10..62a14ea4 100644 --- a/src/os/darwin/darwin_sigar.c +++ b/src/os/darwin/darwin_sigar.c @@ -2487,7 +2487,11 @@ static int sigar_ifmsg_init(sigar_t *sigar) return SIGAR_OK; } -static int has_ifaddr(char *name) +/** + * @param name name of the interface + * @param name_len length of name (w/o \0) + */ +static int has_ifaddr(char *name, size_t name_len) { int sock, status; struct ifreq ifr; @@ -2495,7 +2499,8 @@ static int has_ifaddr(char *name) if ((sock = socket(AF_INET, SOCK_DGRAM, 0)) < 0) { return errno; } - SIGAR_SSTRCPY(ifr.ifr_name, name); + strncpy(ifr.ift_name, name, MIN(sizeof(ifr.ift_name) - 1, name_len)); + ifr.ift_name[MIN(sizeof(ifr.ift_name) - 1, name_len)] = '\0'; if (ioctl(sock, SIOCGIFADDR, &ifr) == 0) { status = SIGAR_OK; } @@ -2545,7 +2550,7 @@ static int sigar_ifmsg_iter(sigar_t *sigar, ifmsg_iter_t *iter) switch (iter->type) { case IFMSG_ITER_LIST: if (sdl->sdl_type == IFT_OTHER) { - if (has_ifaddr(sdl->sdl_data) != SIGAR_OK) { + if (has_ifaddr(sdl->sdl_data, sdl->sdl_nlen) != SIGAR_OK) { break; } } @@ -2566,7 +2571,7 @@ static int sigar_ifmsg_iter(sigar_t *sigar, ifmsg_iter_t *iter) break; case IFMSG_ITER_GET: - if (strEQ(iter->name, sdl->sdl_data)) { + if (strlen(iter->name) == sdl->sdl_nlen && 0 == memcmp(iter->name, sdl->sdl_data, sdl->sdl_nlen)) { iter->data.ifm = ifm; return SIGAR_OK; } From e3068f466a11315195f1e35baafbf355b8167e59 Mon Sep 17 00:00:00 2001 From: Doug MacEachern Date: Wed, 12 Aug 2009 09:42:28 -0700 Subject: [PATCH 2/2] s/ift_name/ifr_name/g --- src/os/darwin/darwin_sigar.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/os/darwin/darwin_sigar.c b/src/os/darwin/darwin_sigar.c index 62a14ea4..a72b5e99 100644 --- a/src/os/darwin/darwin_sigar.c +++ b/src/os/darwin/darwin_sigar.c @@ -2499,8 +2499,8 @@ static int has_ifaddr(char *name, size_t name_len) if ((sock = socket(AF_INET, SOCK_DGRAM, 0)) < 0) { return errno; } - strncpy(ifr.ift_name, name, MIN(sizeof(ifr.ift_name) - 1, name_len)); - ifr.ift_name[MIN(sizeof(ifr.ift_name) - 1, name_len)] = '\0'; + strncpy(ifr.ifr_name, name, MIN(sizeof(ifr.ifr_name) - 1, name_len)); + ifr.ifr_name[MIN(sizeof(ifr.ifr_name) - 1, name_len)] = '\0'; if (ioctl(sock, SIOCGIFADDR, &ifr) == 0) { status = SIGAR_OK; }