Git Inbox Mirror of the ffmpeg-devel mailing list - see https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
 help / color / mirror / Atom feed
From: remi@remlab.net
To: ffmpeg-devel@ffmpeg.org
Subject: [FFmpeg-devel] [PATCHv2] lavc/audiodsp: fix aliasing violation
Date: Tue, 13 Sep 2022 19:10:44 +0300
Message-ID: <20220913161044.15899-1-remi@remlab.net> (raw)

From: Rémi Denis-Courmont <remi@remlab.net>

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);
-- 
2.37.2

_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".

             reply	other threads:[~2022-09-13 16:10 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-09-13 16:10 remi [this message]
2022-09-17 18:39 ` Rémi Denis-Courmont

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20220913161044.15899-1-remi@remlab.net \
    --to=remi@remlab.net \
    --cc=ffmpeg-devel@ffmpeg.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link

Git Inbox Mirror of the ffmpeg-devel mailing list - see https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

This inbox may be cloned and mirrored by anyone:

	git clone --mirror https://master.gitmailbox.com/ffmpegdev/0 ffmpegdev/git/0.git

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V2 ffmpegdev ffmpegdev/ https://master.gitmailbox.com/ffmpegdev \
		ffmpegdev@gitmailbox.com
	public-inbox-index ffmpegdev

Example config snippet for mirrors.


AGPL code for this site: git clone https://public-inbox.org/public-inbox.git