From patchwork Thu Sep 22 15:00:32 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?R=C3=A9mi_Denis-Courmont?= X-Patchwork-Id: 38149 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a20:3b1c:b0:96:9ee8:5cfd with SMTP id c28csp372398pzh; Thu, 22 Sep 2022 08:00:43 -0700 (PDT) X-Google-Smtp-Source: AMsMyM4vRyWU1T7eBuynUrL4MGOjnsLbhs7VPn/LlrFXAvqN180021sA/cG33xeRghLSoCAlRvV6 X-Received: by 2002:a17:907:6d9f:b0:77a:424b:23d4 with SMTP id sb31-20020a1709076d9f00b0077a424b23d4mr3242988ejc.75.1663858842823; Thu, 22 Sep 2022 08:00:42 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1663858842; cv=none; d=google.com; s=arc-20160816; b=Kk0xKlidnwHFyyuL6OTQS+d0fgAwhjxeajRSQgOr4rdz3JU7ucKZlylKxXjeiRJ29Z otuqt7E+MQZdXeBCewBmGwCvKJeMn4737Iw6lwU7PwvypvuiMNuma3jXjBkSQcbguhWJ tLz/fsQPdVAGkHFa8z9mE4H3l1B3fVWsC6XjbNbiFTm5hgk6H3cMOXwyq2yh+juL+cAi 3xvYcGODuUuQdtZtvrPE/fIxqqz+Gb2+lTVR51EKykgl00LaH7mm1stOECAfNAUkTpzi PAMwVJXEnoizya3JcMp74lSmIZQRM6oQ1aYlFyUKnbXt+N7bq93lULxweOqDB3I+pIwh GDfQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:reply-to:list-subscribe :list-help:list-post:list-archive:list-unsubscribe:list-id :precedence:subject:mime-version:message-id:date:to:from :delivered-to; bh=J9jSjEfFXc7EqlHI2OP1cvRpPwI1125z40413Angfsg=; b=ujr7XlYMJU0q5fIyPz+ErgZH8FxIZ9qyMbxP34ci2EO5DLjWnpIsnB1Cs3TNf0aer5 YCETdHeCAt1xFpkt5BNlWkZRbJcfpzgwDz5LjdrNDGvPziyKPlMs4bOcbFX8Knuoo+9E VZ4EY7mJultbJJvH5LNWJ1+W3i7RooinOk3hlYSO/1aw73cKdR/kF7aU9scp3Io44GM6 kRBtvztupBQa7ssl05bh1InrWGzgwTib/hvxKRu3XDmfWkQlS9XEMlEoeDugWPYp94LR KmbqzdXCZPoiaR1tZgSObMVIcSzsEnqoU0VoJC9Ytf0FNW3fRKUfYd0fCwVf3u1Q7sa7 WL7g== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id s7-20020a50ab07000000b00446dc8f5e44si5420136edc.243.2022.09.22.08.00.42; Thu, 22 Sep 2022 08:00:42 -0700 (PDT) Received-SPF: pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) client-ip=79.124.17.100; Authentication-Results: mx.google.com; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id A7ED968BBD8; Thu, 22 Sep 2022 18:00:39 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from ursule.remlab.net (vps-a2bccee9.vps.ovh.net [51.75.19.47]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 0BD2A68BBCB for ; Thu, 22 Sep 2022 18:00:33 +0300 (EEST) Received: from basile.remlab.net (localhost [IPv6:::1]) by ursule.remlab.net (Postfix) with ESMTP id 8A196C007A for ; Thu, 22 Sep 2022 18:00:32 +0300 (EEST) From: remi@remlab.net To: ffmpeg-devel@ffmpeg.org Date: Thu, 22 Sep 2022 18:00:32 +0300 Message-Id: <20220922150032.21811-2-remi@remlab.net> X-Mailer: git-send-email 2.37.2 MIME-Version: 1.0 Subject: [FFmpeg-devel] [REPOST] [PATCHv2 2/2] lavc/audiodsp: fix aliasing violation X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: FFmpeg development discussions and patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: FFmpeg development discussions and patches Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" X-TUID: aub7mOFdiAue From: RĂ©mi Denis-Courmont Even though they have the same size, and typically the same alignment, uint32_t and float are under no circumstances compatible types in C. The casts from float * to uint32_t * are invalid here. Insofar as the resulting pointers are dereferenced, this is undefined behaviour. This patch uses av_float2int() / av_int2float() instead. --- libavcodec/audiodsp.c | 40 ++++++++++++++++++++-------------------- 1 file changed, 20 insertions(+), 20 deletions(-) diff --git a/libavcodec/audiodsp.c b/libavcodec/audiodsp.c index ff43e87dce..8c0847f53f 100644 --- a/libavcodec/audiodsp.c +++ b/libavcodec/audiodsp.c @@ -22,36 +22,36 @@ #include "libavutil/common.h" #include "audiodsp.h" -static inline uint32_t clipf_c_one(uint32_t a, uint32_t mini, - uint32_t maxi, uint32_t maxisign) +static inline float clipf_c_one(float a, uint32_t mini, + uint32_t maxi, uint32_t maxisign) { - if (a > mini) - return mini; - else if ((a ^ (1U << 31)) > maxisign) - return maxi; + uint32_t ai = av_float2int(a); + + if (ai > mini) + return av_int2float(mini); + else if ((ai ^ (1U << 31)) > maxisign) + return av_int2float(maxi); else return a; } static void vector_clipf_c_opposite_sign(float *dst, const float *src, - float *min, float *max, int len) + float min, float max, int len) { int i; - uint32_t mini = *(uint32_t *) min; - uint32_t maxi = *(uint32_t *) max; + uint32_t mini = av_float2int(min); + uint32_t maxi = av_float2int(max); uint32_t maxisign = maxi ^ (1U << 31); - uint32_t *dsti = (uint32_t *) dst; - const uint32_t *srci = (const uint32_t *) src; for (i = 0; i < len; i += 8) { - dsti[i + 0] = clipf_c_one(srci[i + 0], mini, maxi, maxisign); - dsti[i + 1] = clipf_c_one(srci[i + 1], mini, maxi, maxisign); - dsti[i + 2] = clipf_c_one(srci[i + 2], mini, maxi, maxisign); - dsti[i + 3] = clipf_c_one(srci[i + 3], mini, maxi, maxisign); - dsti[i + 4] = clipf_c_one(srci[i + 4], mini, maxi, maxisign); - dsti[i + 5] = clipf_c_one(srci[i + 5], mini, maxi, maxisign); - dsti[i + 6] = clipf_c_one(srci[i + 6], mini, maxi, maxisign); - dsti[i + 7] = clipf_c_one(srci[i + 7], mini, maxi, maxisign); + dst[i + 0] = clipf_c_one(src[i + 0], mini, maxi, maxisign); + dst[i + 1] = clipf_c_one(src[i + 1], mini, maxi, maxisign); + dst[i + 2] = clipf_c_one(src[i + 2], mini, maxi, maxisign); + dst[i + 3] = clipf_c_one(src[i + 3], mini, maxi, maxisign); + dst[i + 4] = clipf_c_one(src[i + 4], mini, maxi, maxisign); + dst[i + 5] = clipf_c_one(src[i + 5], mini, maxi, maxisign); + dst[i + 6] = clipf_c_one(src[i + 6], mini, maxi, maxisign); + dst[i + 7] = clipf_c_one(src[i + 7], mini, maxi, maxisign); } } @@ -61,7 +61,7 @@ static void vector_clipf_c(float *dst, const float *src, int len, int i; if (min < 0 && max > 0) { - vector_clipf_c_opposite_sign(dst, src, &min, &max, len); + vector_clipf_c_opposite_sign(dst, src, min, max, len); } else { for (i = 0; i < len; i += 8) { dst[i] = av_clipf(src[i], min, max);