From patchwork Fri Jul 14 16:10:47 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: =?utf-8?q?R=C3=A9mi_Denis-Courmont?= X-Patchwork-Id: 42675 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a20:6da1:b0:131:a7d0:bc6d with SMTP id gl33csp3779514pzb; Fri, 14 Jul 2023 09:11:01 -0700 (PDT) X-Google-Smtp-Source: APBJJlFbc3knRX9g/EaO6jFga5UqVVyJBGAZi4b5SacAb/q6KVfq0wWjZx4G1I7qQ6tni3B8aPbH X-Received: by 2002:a17:906:e:b0:993:da87:1c7b with SMTP id 14-20020a170906000e00b00993da871c7bmr4408425eja.10.1689351061021; Fri, 14 Jul 2023 09:11:01 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1689351061; cv=none; d=google.com; s=arc-20160816; b=dRk0XMk3IzZjJIrqwBi+5P5bfJH6iMeyqJmoxq++JBRMupuobogdtlyNezYvajdFr6 y6DSI7WTZjYygI5M1ySoZ6l1YMJ7tCcedz/4yGPlyUPGcOzJLPBJs6UDnOZcEgp8n0Sy KTGuCEFuf8rnUmfrEVfnFbc5VCit9pFrpFa2BPG4KWdmLkP+3lnVTxQCXLr/G5wi7D/R BKbnyD7aPtTUH0lMW02Y9QZP7fAkp4FT88VP9qBztpa+6Jvcgq4j7KB0JNs5c59udBYS aJHOKvn8bYl8Nn02Mmqq+yLgA6uc8Qlafw1AOv3jCKK8pxRu5zN9pc2f5HlFSaRUKSpu 53YA== 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=PoR2fi8zW4itagd9WJcnUkGHHpoa8nbXB14puueg8k0=; fh=hQcp50obTJ8bXC1it5NuEN23RGKfx0/zZ3s2gmreL+A=; b=mOMZq/gdyMBYbYT+c2MyOypkH2Iy19l3TA6Fxe6en7P9iBdIr4ifxxC4sd1nzCG9y0 1jFWFAQLt6H5/rY5VAxif3NEMv6YsCYn0j5MxD84ANFAuy3P5KZk0Y88K+U2PYy/uNlI lY6EP4E8s3wOhqzGWwxcsKQxvHLsJblOtKcAwHb1REpiCmCjdh2eVzP1RTwI8O1ZLAtj JQfEsqPs11Q/QeLU1Zuy9mNm4jxecIaq/G8na0Q/82xJxTAxpMf8p4dW3OQDNS2r8igz 15h8dztWW6Fv8fNZUfBYHT8Xh2U3PQk/qgTRZSwia19JvAiV5kVkEC+j2YuIfhUpo56x AP5g== 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 hh15-20020a170906a94f00b0098d2f9279fcsi9452010ejb.498.2023.07.14.09.11.00; Fri, 14 Jul 2023 09:11:01 -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 4112D68C626; Fri, 14 Jul 2023 19:10:54 +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 BD3A268C4C5 for ; Fri, 14 Jul 2023 19:10:47 +0300 (EEST) Received: from basile.remlab.net (localhost [IPv6:::1]) by ursule.remlab.net (Postfix) with ESMTP id 602C3C000E for ; Fri, 14 Jul 2023 19:10:47 +0300 (EEST) From: =?utf-8?q?R=C3=A9mi_Denis-Courmont?= To: ffmpeg-devel@ffmpeg.org Date: Fri, 14 Jul 2023 19:10:47 +0300 Message-Id: <20230714161047.3196-1-remi@remlab.net> X-Mailer: git-send-email 2.40.1 MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH] 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: embe4Ub40WDn 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 eba6e809fd..c5427d3535 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);