From patchwork Tue Sep 13 16:10:44 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: 37904 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a20:3b1c:b0:96:9ee8:5cfd with SMTP id c28csp795330pzh; Tue, 13 Sep 2022 09:10:55 -0700 (PDT) X-Google-Smtp-Source: AA6agR7WiBs3QPUKARmQf/Ikk4MS1XIOk0kKY0E5ISgLDtVA/mHPfdQ1ekccnN/Szb1KvzW/ntk6 X-Received: by 2002:a17:907:2d0d:b0:77c:d528:70b8 with SMTP id gs13-20020a1709072d0d00b0077cd52870b8mr8054239ejc.681.1663085455434; Tue, 13 Sep 2022 09:10:55 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1663085455; cv=none; d=google.com; s=arc-20160816; b=owJnEebjCXDSL1r4Nz5YN3HDOyDZQlImsHZz+V7WZu7kNj1GB+GJImwHrI/MllQeDS Jwa/RUsTt85Ak7/3kXM6z/UIghZPTSiqNuYadAYWo0+qetiqKAcGMnsAjyllUEP7ugMb zkkZzzRQNzDryD/egEz4QHUaeDZaUVpCT+CRWuvywBZqhVNL0tnbEKmGg0uA8pCTrLU+ drjkvniUMWT4N3t7gO3coMgScfYvd0JO1wIe+D8CcI9ZQkzGq3M8LVRj/NvGKG99ZG6C OQXWEpKVNMtDGCu+CBGhwmvXqX0+6fn+hAMhNdYJXUXeW9i+u5+nTJvXPeilukPa1vTg +ywg== 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=IESO8JOk+P/YbJa075ezKP12DXHZc3fhywVuJJNFPYC8fzXpPk76B50865pDAGE2JQ lTNjMWR1enyX3w8FQjHpLFUP5gTJkXfZ67wN3FN73iHXsTVAE3bzZWl8KS/C+lofQwQ0 LbbGGVThNxTBipiOaBOXfZh2fm8veJKM6MP8lZmLJIGw1HZQAxXqs4qAZJk8AZBrb57r PAlGOQlfQzxfZo4aROfznE08ibyGw5GVEjl6IV17FA3vSHYu2h0ifQjOxARBVe5FoYr+ QWEiAR0MuKG+xH/rJ/tK3u8OQh4yHi9/ogQ/8svsjC62V5V7wqLQHhs+MRwFAgbc7doL ih8A== 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 js19-20020a17090797d300b0073c5e27e109si10787369ejc.307.2022.09.13.09.10.54; Tue, 13 Sep 2022 09:10:55 -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 6B39868BB01; Tue, 13 Sep 2022 19:10:51 +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 8DB2F68B7EA for ; Tue, 13 Sep 2022 19:10:45 +0300 (EEST) Received: from basile.remlab.net (localhost [IPv6:::1]) by ursule.remlab.net (Postfix) with ESMTP id 2690BC0012 for ; Tue, 13 Sep 2022 19:10:45 +0300 (EEST) From: remi@remlab.net To: ffmpeg-devel@ffmpeg.org Date: Tue, 13 Sep 2022 19:10:44 +0300 Message-Id: <20220913161044.15899-1-remi@remlab.net> X-Mailer: git-send-email 2.37.2 MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCHv2] 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: 5t1D3PubXW6f 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);