Avoid overflow in WebRtcSpl_Sqrt
When the input to WebRtcSpl_Sqrt was the maximum negative value
(-2147483648), the calculations would overflow. This is now solved by
nudging this particular input value one step.
BUG=webrtc:5512
Review URL: https://codereview.webrtc.org/1685743003
Cr-Commit-Position: refs/heads/master@{#11631}
diff --git a/webrtc/common_audio/signal_processing/spl_sqrt.c b/webrtc/common_audio/signal_processing/spl_sqrt.c
index 24db4f8..579e714 100644
--- a/webrtc/common_audio/signal_processing/spl_sqrt.c
+++ b/webrtc/common_audio/signal_processing/spl_sqrt.c
@@ -139,8 +139,19 @@
A = value;
- if (A == 0)
- return (int32_t)0; // sqrt(0) = 0
+ // The convention in this function is to calculate sqrt(abs(A)). Negate the
+ // input if it is negative.
+ if (A < 0) {
+ if (A == WEBRTC_SPL_WORD32_MIN) {
+ // This number cannot be held in an int32_t after negating.
+ // Map it to the maximum positive value.
+ A = WEBRTC_SPL_WORD32_MAX;
+ } else {
+ A = -A;
+ }
+ } else if (A == 0) {
+ return 0; // sqrt(0) = 0
+ }
sh = WebRtcSpl_NormW32(A); // # shifts to normalize A
A = WEBRTC_SPL_LSHIFT_W32(A, sh); // Normalize A