From 020000433be30a3f68fa8dd3adb7202110e5b28c Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Mon, 18 Feb 2019 12:09:49 +0100 Subject: [PATCH] platform/netlink: add nla_data_as() macro This macro casts the return pointer and asserts that the netlink attribute is suitably large. --- src/platform/nm-netlink.h | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/platform/nm-netlink.h b/src/platform/nm-netlink.h index d9ba93738..6d71562d9 100644 --- a/src/platform/nm-netlink.h +++ b/src/platform/nm-netlink.h @@ -154,6 +154,20 @@ nla_data (const struct nlattr *nla) return &(((char *) nla)[NLA_HDRLEN]); } +#define nla_data_as(type, nla) \ + ({ \ + const struct nlattr *_nla = (nla); \ + \ + nm_assert (nla_len (_nla) >= sizeof (type)); \ + \ + /* note that casting the pointer is undefined behavior in C, if + * the data has wrong alignment. Netlink data is aligned to 4 bytes, + * that means, if the alignment is larger than 4, this is invalid. */ \ + G_STATIC_ASSERT_EXPR (_nm_alignof (type) <= NLA_ALIGNTO); \ + \ + (type *) nla_data (_nla); \ + }) + static inline uint8_t nla_get_u8 (const struct nlattr *nla) {