summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorThomas Haller <thaller@redhat.com>2018-02-12 14:10:18 +0100
committerThomas Haller <thaller@redhat.com>2018-02-12 14:17:11 +0100
commitdaa4a68177a26dcf6c7185d82cafdee5fe22f2ec (patch)
tree646db9fb88a306370cb7830b1d105e798d517de1 /lib
parent52057832033ac8f6c6969e4ae3a7e65db1c9f540 (diff)
downloadlibnl-daa4a68177a26dcf6c7185d82cafdee5fe22f2ec.tar.gz
lib: merge implementations of nl_attr_end() and nl_attr_keep_empty()
Both functions are almost identical. Merge them into a common helper function with an @keep_empty argument, so it is clear at which point they differ. Also, fix symbols versioning for nl_attr_keep_empty(). For symbol versioning, once released a version cannot be modifified/extended.
Diffstat (limited to 'lib')
-rw-r--r--lib/attr.c64
1 files changed, 18 insertions, 46 deletions
diff --git a/lib/attr.c b/lib/attr.c
index d90fa479..0928630b 100644
--- a/lib/attr.c
+++ b/lib/attr.c
@@ -912,22 +912,14 @@ struct nlattr *nla_nest_start(struct nl_msg *msg, int attrtype)
return start;
}
-/**
- * Finalize nesting of attributes.
- * @arg msg Netlink message.
- * @arg start Container attribute as returned from nla_nest_start().
- *
- * Corrects the container attribute header to include the appeneded attributes.
- *
- * @return 0 on success or a negative error code.
- */
-int nla_nest_end(struct nl_msg *msg, struct nlattr *start)
+static int _nest_end(struct nl_msg *msg, struct nlattr *start, int keep_empty)
{
size_t pad, len;
len = (void *) nlmsg_tail(msg->nm_nlh) - (void *) start;
- if (len == NLA_HDRLEN || len > USHRT_MAX) {
+ if ( len > USHRT_MAX
+ || (!keep_empty && len == NLA_HDRLEN)) {
/*
* Max nlattr size exceeded or empty nested attribute, trim the
* attribute header again
@@ -962,6 +954,20 @@ int nla_nest_end(struct nl_msg *msg, struct nlattr *start)
}
/**
+ * Finalize nesting of attributes.
+ * @arg msg Netlink message.
+ * @arg start Container attribute as returned from nla_nest_start().
+ *
+ * Corrects the container attribute header to include the appeneded attributes.
+ *
+ * @return 0 on success or a negative error code.
+ */
+int nla_nest_end(struct nl_msg *msg, struct nlattr *start)
+{
+ return _nest_end (msg, start, 0);
+}
+
+/**
* Finalize nesting of attributes without stripping off empty attributes.
* @arg msg Netlink message.
* @arg start Container attribute as returned from nla_nest_start().
@@ -973,41 +979,7 @@ int nla_nest_end(struct nl_msg *msg, struct nlattr *start)
*/
int nla_nest_end_keep_empty(struct nl_msg *msg, struct nlattr *start)
{
- size_t pad, len;
-
- len = (void *) nlmsg_tail(msg->nm_nlh) - (void *) start;
-
- if (len > USHRT_MAX) {
- /*
- * Max nlattr size is exceeded, trim the attribute header again
- */
- nla_nest_cancel(msg, start);
-
- /* Return error only if nlattr size was exceeded */
- return -NLE_ATTRSIZE;
- }
-
- start->nla_len = len;
-
- pad = NLMSG_ALIGN(msg->nm_nlh->nlmsg_len) - msg->nm_nlh->nlmsg_len;
- if (pad > 0) {
- /*
- * Data inside attribute does not end at a alignment boundry.
- * Pad accordingly and accoun for the additional space in
- * the message. nlmsg_reserve() may never fail in this situation,
- * the allocate message buffer must be a multiple of NLMSG_ALIGNTO.
- */
- if (!nlmsg_reserve(msg, pad, 0))
- BUG();
-
- NL_DBG(2, "msg %p: attr <%p> %d: added %zu bytes of padding\n",
- msg, start, start->nla_type, pad);
- }
-
- NL_DBG(2, "msg %p: attr <%p> %d: closing nesting, len=%u\n",
- msg, start, start->nla_type, start->nla_len);
-
- return 0;
+ return _nest_end (msg, start, 1);
}
/**