Skip to contentSkip to conversation

include: add unlikely() notation and use it

PATCH New Pablo Neira Ayuso 1 participant 1 message Nov 21 netfilter

lore
Share & tools
lore

Pablo Neira Ayuso

author maintainer
#0 lore raw

PATCH 1/1 include: add unlikely() notation and use it

Add this notation to highlight codepath that is unlikely to happen.

Use it a recent LLM fix that hardens mnl_attr_get_payload_len() against. malformed netlink messages, which should not happen in netlink messages coming from the kernel.

---
 src/attr.c     | 2 +-
 src/internal.h | 8 ++++++++
 2 files changed, 9 insertions(+), 1 deletion(-)
src/attr.c +1 -1
index 69d23af50855..1456b0c03ad9 100644
@@ -63,7 +63,7 @@ EXPORT_SYMBOL uint16_t mnl_attr_get_len(const struct nlattr *attr)
  */
 EXPORT_SYMBOL uint16_t mnl_attr_get_payload_len(const struct nlattr *attr)
 {
-	if (attr->nla_len < MNL_ATTR_HDRLEN)
+	if (unlikely(attr->nla_len < MNL_ATTR_HDRLEN))
 		return 0;
 
 	return attr->nla_len - MNL_ATTR_HDRLEN;
src/internal.h +8 -0
index d69eaf3ef203..340f17008e55 100644
@@ -8,4 +8,12 @@
 #	define EXPORT_SYMBOL
 #endif
 
+#ifdef __GNUC__
+# define likely(x)	__builtin_expect(!!(x), 1)
+# define unlikely(x)	__builtin_expect(!!(x), 0)
+#else
+# define likely(x)	(x)
+# define unlikely(x)	(x)
+#endif
+
 #endif
Signature · 1 line
2.47.3