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

Unified Diff: webrtc/common_audio/signal_processing/spl_sqrt.c

Issue 1685743003: Avoid overflow in WebRtcSpl_Sqrt (Closed) Base URL: https://chromium.googlesource.com/external/webrtc.git@master
Patch Set: Patch set 2 didn't work - fixing it Created 4 years, 10 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 | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: webrtc/common_audio/signal_processing/spl_sqrt.c
diff --git a/webrtc/common_audio/signal_processing/spl_sqrt.c b/webrtc/common_audio/signal_processing/spl_sqrt.c
index 24db4f822ca153ceebd85d436135970a651ac559..579e714a1104b965ebdb5065324a08a24637c4f8 100644
--- a/webrtc/common_audio/signal_processing/spl_sqrt.c
+++ b/webrtc/common_audio/signal_processing/spl_sqrt.c
@@ -139,8 +139,19 @@ int32_t WebRtcSpl_Sqrt(int32_t value)
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
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698