Skip to content

Commit

Permalink
mbuf: fix strict aliasing in allocator
Browse files Browse the repository at this point in the history
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 <rjarry@redhat.com>
Reviewed-by: Stephen Hemminger <stephen@networkplumber.org>
Tested-by: Ali Alnubani <alialnu@nvidia.com>
  • Loading branch information
rjarry authored and tmonjalo committed Oct 17, 2024
1 parent 74efd38 commit 6011b12
Showing 1 changed file with 7 additions and 4 deletions.
11 changes: 7 additions & 4 deletions lib/mbuf/rte_mbuf.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand Down

0 comments on commit 6011b12

Please sign in to comment.