From mboxrd@z Thu Jan 1 00:00:00 1970 From: Serhey Popovych Subject: [PATCH iproute2 3/3] utils: ll_map: Make network device name fixed size array of char Date: Wed, 20 Dec 2017 09:37:31 +0200 Message-ID: <1513755451-9800-4-git-send-email-serhe.popovych@gmail.com> References: <1513755451-9800-1-git-send-email-serhe.popovych@gmail.com> To: netdev@vger.kernel.org Return-path: Received: from mail-lf0-f65.google.com ([209.85.215.65]:45880 "EHLO mail-lf0-f65.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S932267AbdLTHiI (ORCPT ); Wed, 20 Dec 2017 02:38:08 -0500 Received: by mail-lf0-f65.google.com with SMTP id f13so22887085lff.12 for ; Tue, 19 Dec 2017 23:38:08 -0800 (PST) Received: from tuxracer.localdomain ([2a01:6d80::195:20:96:53]) by smtp.gmail.com with ESMTPSA id a9sm3722880lfg.12.2017.12.19.23.38.06 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 19 Dec 2017 23:38:06 -0800 (PST) In-Reply-To: <1513755451-9800-1-git-send-email-serhe.popovych@gmail.com> Sender: netdev-owner@vger.kernel.org List-ID: Network device names are fixed in size and never exceed IFNAMSIZ (16 bytes). Make name fixed size array to always malloc() same size chunk of memory and use memcpy()/memcmp() with constant IFNAMSIZ to benefit from possible compiler optimizations replacing call to a function with two/four load/store instructions on 64/32 bit systems. Check if IFLA_IFNAME attribute present in netlink message (should always) and use strncpy() to pad name with zeros. Signed-off-by: Serhey Popovych --- lib/ll_map.c | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/lib/ll_map.c b/lib/ll_map.c index abe7bdc..fcbf0fb 100644 --- a/lib/ll_map.c +++ b/lib/ll_map.c @@ -30,7 +30,7 @@ struct ll_cache { unsigned flags; unsigned index; unsigned short type; - char name[]; + char name[IFNAMSIZ]; }; #define IDXMAP_SIZE 1024 @@ -71,7 +71,7 @@ static struct ll_cache *ll_get_by_name(const char *name) struct ll_cache *im = container_of(n, struct ll_cache, name_hash); - if (strncmp(im->name, name, IFNAMSIZ) == 0) + if (!strcmp(im->name, name)) return im; } @@ -82,7 +82,7 @@ int ll_remember_index(const struct sockaddr_nl *who, struct nlmsghdr *n, void *arg) { unsigned int h; - const char *ifname; + char ifname[IFNAMSIZ]; struct ifinfomsg *ifi = NLMSG_DATA(n); struct ll_cache *im; struct rtattr *tb[IFLA_MAX+1]; @@ -105,17 +105,21 @@ int ll_remember_index(const struct sockaddr_nl *who, } parse_rtattr(tb, IFLA_MAX, IFLA_RTA(ifi), IFLA_PAYLOAD(n)); - ifname = rta_getattr_str(tb[IFLA_IFNAME]); - if (ifname == NULL) + + if (!tb[IFLA_IFNAME]) + return 0; + strncpy(ifname, rta_getattr_str(tb[IFLA_IFNAME]), IFNAMSIZ); + if (!ifname[0]) return 0; + ifname[IFNAMSIZ - 1] = '\0'; if (im) { /* change to existing entry */ - rehash = strcmp(im->name, ifname); + rehash = memcmp(im->name, ifname, IFNAMSIZ); if (rehash) hlist_del(&im->name_hash); } else { - im = malloc(sizeof(*im) + strlen(ifname) + 1); + im = malloc(sizeof(*im)); if (!im) return 0; im->index = ifi->ifi_index; @@ -133,7 +137,7 @@ int ll_remember_index(const struct sockaddr_nl *who, h = namehash(ifname) & (IDXMAP_SIZE - 1); hlist_add_head(&im->name_hash, &name_head[h]); - strcpy(im->name, ifname); + memcpy(im->name, ifname, IFNAMSIZ); } return 0; -- 1.7.10.4