From 144f9f5da375c6f81de9c2d97f4367f7e7f12bd6 Mon Sep 17 00:00:00 2001 From: Olivier Matz Date: Thu, 30 Jun 2011 18:21:17 +0200 Subject: [PATCH] cmdline: parse ip/ether addr, don't write in result if NULL Signed-off-by: Olivier Matz --- src/lib/cmdline_parse_etheraddr.c | 4 ++-- src/lib/cmdline_parse_ipaddr.c | 20 +++++++++++++------- 2 files changed, 15 insertions(+), 9 deletions(-) diff --git a/src/lib/cmdline_parse_etheraddr.c b/src/lib/cmdline_parse_etheraddr.c index bd2717a..490d3e4 100644 --- a/src/lib/cmdline_parse_etheraddr.c +++ b/src/lib/cmdline_parse_etheraddr.c @@ -106,7 +106,6 @@ cmdline_parse_etheraddr(__attribute__((unused)) cmdline_parse_token_hdr_t *tk, { unsigned int token_len = 0; char ether_str[ETHER_ADDRSTRLEN]; - struct ether_addr *etheraddr = res; struct ether_addr *tmp; if (res && ressize < sizeof(struct ether_addr)) @@ -121,7 +120,8 @@ cmdline_parse_etheraddr(__attribute__((unused)) cmdline_parse_token_hdr_t *tk, if (tmp == NULL) return -1; - memcpy(ðeraddr, tmp, sizeof(etheraddr)); + if (res != NULL) + memcpy(res, tmp, sizeof(struct ether_addr)); return 0; } diff --git a/src/lib/cmdline_parse_ipaddr.c b/src/lib/cmdline_parse_ipaddr.c index bcd12a4..ff02eff 100644 --- a/src/lib/cmdline_parse_ipaddr.c +++ b/src/lib/cmdline_parse_ipaddr.c @@ -289,13 +289,15 @@ cmdline_parse_ipaddr(cmdline_parse_token_hdr_t *tk, const char *buf, void *res, struct cmdline_token_ipaddr *tk2 = (struct cmdline_token_ipaddr *)tk; unsigned int token_len = 0; char ip_str[INET6_ADDRSTRLEN+4]; /* '+4' is for prefixlen (if any) */ - cmdline_ipaddr_t *ipaddr = res; + cmdline_ipaddr_t ipaddr; char *prefix, *prefix_end; long prefixlen; if (res && ressize < sizeof(cmdline_ipaddr_t)) return -1; + memset(&ipaddr, 0, sizeof(ipaddr)); + /* if token is too big... */ token_len = snprintf(ip_str, sizeof(ip_str), "%s", buf); if (token_len >= sizeof(ip_str)) @@ -312,21 +314,25 @@ cmdline_parse_ipaddr(cmdline_parse_token_hdr_t *tk, const char *buf, void *res, prefixlen = strtol(prefix, &prefix_end, 10); if (errno || (*prefix_end != '\0') ) return -1; - ipaddr->prefixlen = prefixlen; + ipaddr.prefixlen = prefixlen; } else { - ipaddr->prefixlen = 0; + ipaddr.prefixlen = 0; } /* convert the IP addr */ if ((tk2->ipaddr_data.flags & CMDLINE_IPADDR_V4) && - my_inet_pton(AF_INET, ip_str, &ipaddr->addr.ipv4) == 1) { - ipaddr->family = AF_INET; + my_inet_pton(AF_INET, ip_str, &ipaddr.addr.ipv4) == 1) { + ipaddr.family = AF_INET; + if (res != NULL) + memcpy(res, &ipaddr, sizeof(ipaddr)); return 0; } if ((tk2->ipaddr_data.flags & CMDLINE_IPADDR_V6) && - my_inet_pton(AF_INET6, ip_str, &ipaddr->addr.ipv6) == 1) { - ipaddr->family = AF_INET6; + my_inet_pton(AF_INET6, ip_str, &ipaddr.addr.ipv6) == 1) { + ipaddr.family = AF_INET6; + if (res != NULL) + memcpy(res, &ipaddr, sizeof(ipaddr)); return 0; } return -1; -- 2.20.1