From 6011b12f52b60565d4dfcc3b382551ec1f53b3d4 Mon Sep 17 00:00:00 2001 From: Robin Jarry Date: Wed, 25 Sep 2024 11:40:54 -0400 Subject: [PATCH] mbuf: fix strict aliasing in allocator MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit When building an application with -fstrict-aliasing -Wstrict-aliasing=2, we get errors triggered by rte_mbuf_raw_alloc() which is called inline from rte_pktmbuf_alloc(). ../dpdk/lib/mbuf/rte_mbuf.h: In function ‘rte_mbuf_raw_alloc’: ../dpdk/lib/mbuf/rte_mbuf.h:600:42: error: dereferencing type-punned pointer might break strict-aliasing rules [-Werror=strict-aliasing] 600 | if (rte_mempool_get(mp, (void **)&m) < 0) | ^~ Avoid incorrect casting by using an inline union variable. Signed-off-by: Robin Jarry Reviewed-by: Stephen Hemminger Tested-by: Ali Alnubani --- lib/mbuf/rte_mbuf.h | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/lib/mbuf/rte_mbuf.h b/lib/mbuf/rte_mbuf.h index babe16c72cc..0d2e0e64b3c 100644 --- a/lib/mbuf/rte_mbuf.h +++ b/lib/mbuf/rte_mbuf.h @@ -595,12 +595,15 @@ __rte_mbuf_raw_sanity_check(__rte_unused const struct rte_mbuf *m) */ static inline struct rte_mbuf *rte_mbuf_raw_alloc(struct rte_mempool *mp) { - struct rte_mbuf *m; + union { + void *ptr; + struct rte_mbuf *m; + } ret; - if (rte_mempool_get(mp, (void **)&m) < 0) + if (rte_mempool_get(mp, &ret.ptr) < 0) return NULL; - __rte_mbuf_raw_sanity_check(m); - return m; + __rte_mbuf_raw_sanity_check(ret.m); + return ret.m; } /**