From mboxrd@z Thu Jan  1 00:00:00 1970
Return-Path: <dev-bounces@dpdk.org>
Received: from mails.dpdk.org (mails.dpdk.org [217.70.189.124])
	by inbox.dpdk.org (Postfix) with ESMTP id 1268045A2A;
	Wed, 25 Sep 2024 17:41:03 +0200 (CEST)
Received: from mails.dpdk.org (localhost [127.0.0.1])
	by mails.dpdk.org (Postfix) with ESMTP id DA5E14025D;
	Wed, 25 Sep 2024 17:41:02 +0200 (CEST)
Received: from us-smtp-delivery-124.mimecast.com
 (us-smtp-delivery-124.mimecast.com [170.10.129.124])
 by mails.dpdk.org (Postfix) with ESMTP id 6AB13400EF
 for <dev@dpdk.org>; Wed, 25 Sep 2024 17:41:01 +0200 (CEST)
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com;
 s=mimecast20190719; t=1727278861;
 h=from:from:reply-to:subject:subject:date:date:message-id:message-id:
 to:to:cc:mime-version:mime-version:content-type:content-type:
 content-transfer-encoding:content-transfer-encoding:
 in-reply-to:in-reply-to:references:references;
 bh=zrgnUB/FOK9AY9CBbpZbyQE/1nK7PdUwWVXzf+5lokE=;
 b=gQfrQjAMhfSwtYpWozBQl9JY0Lmzgfhj/dU8zYsyO9W8Mw8rwnk7JjjVI5kv5LlKDtRpCk
 N9qrJwlaAq66ri/fRB3P0gQ76waFqP+FokPLtyXeJpf9HKXt80TErT+lFlT6sYIOailehw
 3Kn8Y7BH2TLIST5AzqgHPpzQO3vvfOo=
Received: from mx-prod-mc-02.mail-002.prod.us-west-2.aws.redhat.com
 (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by
 relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3,
 cipher=TLS_AES_256_GCM_SHA384) id us-mta-573-AlVxfcaJPea2KQDgL_hG2w-1; Wed,
 25 Sep 2024 11:40:59 -0400
X-MC-Unique: AlVxfcaJPea2KQDgL_hG2w-1
Received: from mx-prod-int-02.mail-002.prod.us-west-2.aws.redhat.com (unknown
 [10.30.177.15])
 (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)
 key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256)
 (No client certificate requested)
 by mx-prod-mc-02.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS
 id 17B9B18EB2EB
 for <dev@dpdk.org>; Wed, 25 Sep 2024 15:40:59 +0000 (UTC)
Received: from ringo.redhat.com (unknown [10.22.58.9])
 by mx-prod-int-02.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP
 id 42B90195608A; Wed, 25 Sep 2024 15:40:58 +0000 (UTC)
From: Robin Jarry <rjarry@redhat.com>
To: dev@dpdk.org
Subject: [PATCH dpdk v2] mbuf: fix strict aliasing error in allocator
Date: Wed, 25 Sep 2024 11:40:54 -0400
Message-ID: <20240925154053.80861-2-rjarry@redhat.com>
In-Reply-To: <20240925140021.46320-2-rjarry@redhat.com>
References: <20240925140021.46320-2-rjarry@redhat.com>
MIME-Version: 1.0
X-Scanned-By: MIMEDefang 3.0 on 10.30.177.15
X-Mimecast-Spam-Score: 0
X-Mimecast-Originator: redhat.com
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
X-BeenThere: dev@dpdk.org
X-Mailman-Version: 2.1.29
Precedence: list
List-Id: DPDK patches and discussions <dev.dpdk.org>
List-Unsubscribe: <https://mails.dpdk.org/options/dev>,
 <mailto:dev-request@dpdk.org?subject=unsubscribe>
List-Archive: <http://mails.dpdk.org/archives/dev/>
List-Post: <mailto:dev@dpdk.org>
List-Help: <mailto:dev-request@dpdk.org?subject=help>
List-Subscribe: <https://mails.dpdk.org/listinfo/dev>,
 <mailto:dev-request@dpdk.org?subject=subscribe>
Errors-To: dev-bounces@dpdk.org

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>
---

Notes:
    v2: use inline union to fix -Wincompatible-pointer-types

 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 babe16c72ccb..0d2e0e64b3ce 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;
 }
 
 /**
-- 
2.46.1