[FFmpeg-cvslog] mathops: remove undefined behaviour from sign_extend()

Mans Rullgard git at videolan.org
Fri Oct 14 03:58:29 CEST 2011


ffmpeg | branch: master | Mans Rullgard <mans at mansr.com> | Sun Oct  9 12:57:08 2011 +0100| [f59bb3d8f3eb50aa182b0d7ae209848b94a507cc] | committer: Mans Rullgard

mathops: remove undefined behaviour from sign_extend()

This function intentionally overflows the signed range on
the left shift.  Using this type-punning avoids errors from
the overflow checker without disabling this test globally.

Signed-off-by: Mans Rullgard <mans at mansr.com>

> http://git.videolan.org/gitweb.cgi/ffmpeg.git/?a=commit;h=f59bb3d8f3eb50aa182b0d7ae209848b94a507cc
---

 libavcodec/mathops.h |    4 +++-
 1 files changed, 3 insertions(+), 1 deletions(-)

diff --git a/libavcodec/mathops.h b/libavcodec/mathops.h
index ec76eaa..45b1ecf 100644
--- a/libavcodec/mathops.h
+++ b/libavcodec/mathops.h
@@ -116,7 +116,9 @@ static inline av_const int mid_pred(int a, int b, int c)
 #ifndef sign_extend
 static inline av_const int sign_extend(int val, unsigned bits)
 {
-    return (val << ((8 * sizeof(int)) - bits)) >> ((8 * sizeof(int)) - bits);
+    unsigned shift = 8 * sizeof(int) - bits;
+    union { unsigned u; int s; } v = { (unsigned) val << shift };
+    return v.s >> shift;
 }
 #endif
 



More information about the ffmpeg-cvslog mailing list