From a0fe7a1c9abe3ab5bf8c9253fab50e114b02b87b Mon Sep 17 00:00:00 2001 From: Thomas Graf Date: Wed, 23 Mar 2011 13:39:18 +0100 Subject: Omit empty nested attributes Check for empty nested attributes in nla_nest_end() and omit the attribute alltogether if is is the case. --- lib/attr.c | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/lib/attr.c b/lib/attr.c index cccd50d..a045351 100644 --- a/lib/attr.c +++ b/lib/attr.c @@ -1151,10 +1151,22 @@ struct nlattr *nla_nest_start(struct nl_msg *msg, int attrtype) */ int nla_nest_end(struct nl_msg *msg, struct nlattr *start) { - size_t pad; + size_t pad, len; - start->nla_len = (unsigned char *) nlmsg_tail(msg->nm_nlh) - - (unsigned char *) start; + len = (void *) nlmsg_tail(msg->nm_nlh) - (void *) start; + + if (len == NLA_HDRLEN) { + /* + * Kernel can't handle empty nested attributes, trim the + * attribute header again + */ + msg->nm_nlh->nlmsg_len -= NLA_HDRLEN; + memset(nlmsg_tail(msg->nm_nlh), 0, NLA_HDRLEN); + + return 0; + } + + start->nla_len = len; pad = NLMSG_ALIGN(msg->nm_nlh->nlmsg_len) - msg->nm_nlh->nlmsg_len; if (pad > 0) { -- cgit v0.12