From 6f8208c0d452c616bae02adff8dd8c707d722a07 Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Fri, 15 Feb 2019 11:33:57 +0100 Subject: platform/netlink: cleanup nla_parse*() code by using safer macros - drop explicit MAX sizes like static const struct nla_policy policy[IFLA_INET6_MAX+1] = { The compiler will deduce that. It saves redundant information (which is possibly wrong). Also, the max define might be larger than we actually need it, so we just waste a few bytes of static memory and do unnecesary steps during validation. Also, the compiler will catch bugs, if the array size of policy/tb is too short for what we access later (-Warray-bounds). - avoid redundant size specifiers like: static const struct nla_policy policy[IFLA_INET6_MAX+1] = { ... struct nlattr *tb[IFLA_INET6_MAX+1]; ... err = nla_parse_nested (tb, IFLA_INET6_MAX, attr, policy); - use the nla_parse*_arr() macros that determine the maxtype based on the argument types. - move declaration of "static const struct nla_policy policy" variable to the beginning, before auto variables. - drop unneeded temporay error variables. --- src/platform/wpan/nm-wpan-utils.c | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) (limited to 'src/platform/wpan') diff --git a/src/platform/wpan/nm-wpan-utils.c b/src/platform/wpan/nm-wpan-utils.c index 4ae1770ffe..b7a51e9bf2 100644 --- a/src/platform/wpan/nm-wpan-utils.c +++ b/src/platform/wpan/nm-wpan-utils.c @@ -153,17 +153,19 @@ struct nl802154_interface { static int nl802154_get_interface_handler (struct nl_msg *msg, void *arg) { + static const struct nla_policy nl802154_policy[] = { + [NL802154_ATTR_PAN_ID] = { .type = NLA_U16 }, + [NL802154_ATTR_SHORT_ADDR] = { .type = NLA_U16 }, + }; + struct nlattr *tb[G_N_ELEMENTS (nl802154_policy)]; struct nl802154_interface *info = arg; struct genlmsghdr *gnlh = nlmsg_data (nlmsg_hdr (msg)); - struct nlattr *tb[NL802154_ATTR_MAX + 1] = { 0, }; - static const struct nla_policy nl802154_policy[NL802154_ATTR_MAX + 1] = { - [NL802154_ATTR_PAN_ID] = { .type = NLA_U16 }, - [NL802154_ATTR_SHORT_ADDR] = { .type = NLA_U16 }, - }; - if (nla_parse (tb, NL802154_ATTR_MAX, genlmsg_attrdata (gnlh, 0), - genlmsg_attrlen (gnlh, 0), nl802154_policy) < 0) - return NL_SKIP; + if (nla_parse_arr (tb, + genlmsg_attrdata (gnlh, 0), + genlmsg_attrlen (gnlh, 0), + nl802154_policy) < 0) + return NL_SKIP; if (tb[NL802154_ATTR_PAN_ID]) info->pan_id = le16toh (nla_get_u16 (tb[NL802154_ATTR_PAN_ID])); -- cgit v1.2.1