Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(62)

Unified Diff: webrtc/common_audio/signal_processing/include/spl_inl.h

Issue 2002523002: Fix an UBSan error (signed overflow) in saturating addition and subtraction (Closed) Base URL: https://chromium.googlesource.com/external/webrtc.git@master
Patch Set: review suggestions Created 4 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | webrtc/common_audio/signal_processing/signal_processing_unittest.cc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: webrtc/common_audio/signal_processing/include/spl_inl.h
diff --git a/webrtc/common_audio/signal_processing/include/spl_inl.h b/webrtc/common_audio/signal_processing/include/spl_inl.h
index d3cc6dee6c53efd7de904950a8fa9a88ac73e03c..3c0a81cf34a7e5fba062801936d5d2a31adfd922 100644
--- a/webrtc/common_audio/signal_processing/include/spl_inl.h
+++ b/webrtc/common_audio/signal_processing/include/spl_inl.h
@@ -35,42 +35,32 @@ static __inline int16_t WebRtcSpl_SatW32ToW16(int32_t value32) {
return out16;
}
-static __inline int32_t WebRtcSpl_AddSatW32(int32_t l_var1, int32_t l_var2) {
- int32_t l_sum;
-
- // Perform long addition
- l_sum = l_var1 + l_var2;
-
- if (l_var1 < 0) { // Check for underflow.
- if ((l_var2 < 0) && (l_sum >= 0)) {
- l_sum = (int32_t)0x80000000;
- }
- } else { // Check for overflow.
- if ((l_var2 > 0) && (l_sum < 0)) {
- l_sum = (int32_t)0x7FFFFFFF;
- }
+static __inline int32_t WebRtcSpl_AddSatW32(int32_t a, int32_t b) {
+ // Do the addition in unsigned numbers, since signed overflow is undefined
+ // behavior.
+ const int32_t sum = (int32_t)((uint32_t)a + (uint32_t)b);
+
+ // a + b can't overflow if a and b have different signs. If they have the
+ // same sign, a + b also has the same sign iff it didn't overflow.
+ if ((a < 0) == (b < 0) && (a < 0) != (sum < 0)) {
+ // The direction of the overflow is obvious from the sign of a + b.
+ return sum < 0 ? INT32_MAX : INT32_MIN;
}
-
- return l_sum;
+ return sum;
}
-static __inline int32_t WebRtcSpl_SubSatW32(int32_t l_var1, int32_t l_var2) {
- int32_t l_diff;
-
- // Perform subtraction.
- l_diff = l_var1 - l_var2;
+static __inline int32_t WebRtcSpl_SubSatW32(int32_t a, int32_t b) {
+ // Do the subtraction in unsigned numbers, since signed overflow is undefined
+ // behavior.
+ const int32_t diff = (int32_t)((uint32_t)a - (uint32_t)b);
- if (l_var1 < 0) { // Check for underflow.
- if ((l_var2 > 0) && (l_diff > 0)) {
- l_diff = (int32_t)0x80000000;
- }
- } else { // Check for overflow.
- if ((l_var2 < 0) && (l_diff < 0)) {
- l_diff = (int32_t)0x7FFFFFFF;
- }
+ // a - b can't overflow if a and b have the same sign. If they have different
+ // signs, a - b has the same sign as a iff it didn't overflow.
+ if ((a < 0) != (b < 0) && (a < 0) != (diff < 0)) {
+ // The direction of the overflow is obvious from the sign of a - b.
+ return diff < 0 ? INT32_MAX : INT32_MIN;
}
-
- return l_diff;
+ return diff;
}
static __inline int16_t WebRtcSpl_AddSatW16(int16_t a, int16_t b) {
« no previous file with comments | « no previous file | webrtc/common_audio/signal_processing/signal_processing_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698