Avoid overflow in WebRtcSpl_DotProductWithScale

BUG=chromium:676935

Review-Url: https://codereview.webrtc.org/2717123004
Cr-Commit-Position: refs/heads/master@{#17091}
diff --git a/webrtc/common_audio/BUILD.gn b/webrtc/common_audio/BUILD.gn
index 6162793..4367a0b 100644
--- a/webrtc/common_audio/BUILD.gn
+++ b/webrtc/common_audio/BUILD.gn
@@ -116,7 +116,6 @@
     "signal_processing/copy_set_operations.c",
     "signal_processing/cross_correlation.c",
     "signal_processing/division_operations.c",
-    "signal_processing/dot_product_with_scale.c",
     "signal_processing/downsample_fast.c",
     "signal_processing/energy.c",
     "signal_processing/filter_ar.c",
@@ -203,6 +202,22 @@
 
   public_configs = [ ":common_audio_config" ]
   deps = [
+    ":common_audio_cc",
+    "..:webrtc_common",
+    "../base:rtc_base_approved",
+    "../system_wrappers:system_wrappers",
+  ]
+}
+
+rtc_source_set("common_audio_cc") {
+  visibility = [ ":*" ]  # Only targets in this file can depend on this.
+  sources = [
+    "signal_processing/dot_product_with_scale.cc",
+    "signal_processing/dot_product_with_scale.h",
+  ]
+
+  public_configs = [ ":common_audio_config" ]
+  deps = [
     "..:webrtc_common",
     "../base:rtc_base_approved",
     "../system_wrappers:system_wrappers",
diff --git a/webrtc/common_audio/signal_processing/dot_product_with_scale.c b/webrtc/common_audio/signal_processing/dot_product_with_scale.cc
similarity index 85%
rename from webrtc/common_audio/signal_processing/dot_product_with_scale.c
rename to webrtc/common_audio/signal_processing/dot_product_with_scale.cc
index 1302d62..4067ab5 100644
--- a/webrtc/common_audio/signal_processing/dot_product_with_scale.c
+++ b/webrtc/common_audio/signal_processing/dot_product_with_scale.cc
@@ -8,13 +8,15 @@
  *  be found in the AUTHORS file in the root of the source tree.
  */
 
-#include "webrtc/common_audio/signal_processing/include/signal_processing_library.h"
+#include "webrtc/common_audio/signal_processing/dot_product_with_scale.h"
+
+#include "webrtc/base/safe_conversions.h"
 
 int32_t WebRtcSpl_DotProductWithScale(const int16_t* vector1,
                                       const int16_t* vector2,
                                       size_t length,
                                       int scaling) {
-  int32_t sum = 0;
+  int64_t sum = 0;
   size_t i = 0;
 
   /* Unroll the loop to improve performance. */
@@ -28,5 +30,5 @@
     sum += (vector1[i] * vector2[i]) >> scaling;
   }
 
-  return sum;
+  return rtc::saturated_cast<int32_t>(sum);
 }
diff --git a/webrtc/common_audio/signal_processing/dot_product_with_scale.h b/webrtc/common_audio/signal_processing/dot_product_with_scale.h
new file mode 100644
index 0000000..288fe5e
--- /dev/null
+++ b/webrtc/common_audio/signal_processing/dot_product_with_scale.h
@@ -0,0 +1,41 @@
+/*
+ *  Copyright (c) 2017 The WebRTC project authors. All Rights Reserved.
+ *
+ *  Use of this source code is governed by a BSD-style license
+ *  that can be found in the LICENSE file in the root of the source
+ *  tree. An additional intellectual property rights grant can be found
+ *  in the file PATENTS.  All contributing project authors may
+ *  be found in the AUTHORS file in the root of the source tree.
+ */
+
+#ifndef WEBRTC_COMMON_AUDIO_SIGNAL_PROCESSING_DOT_PRODUCT_WITH_SCALE_H_
+#define WEBRTC_COMMON_AUDIO_SIGNAL_PROCESSING_DOT_PRODUCT_WITH_SCALE_H_
+
+#include <string.h>
+
+#include "webrtc/typedefs.h"
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+// Calculates the dot product between two (int16_t) vectors.
+//
+// Input:
+//      - vector1       : Vector 1
+//      - vector2       : Vector 2
+//      - vector_length : Number of samples used in the dot product
+//      - scaling       : The number of right bit shifts to apply on each term
+//                        during calculation to avoid overflow, i.e., the
+//                        output will be in Q(-|scaling|)
+//
+// Return value         : The dot product in Q(-scaling)
+int32_t WebRtcSpl_DotProductWithScale(const int16_t* vector1,
+                                      const int16_t* vector2,
+                                      size_t length,
+                                      int scaling);
+
+#ifdef __cplusplus
+}
+#endif  // __cplusplus
+#endif  // WEBRTC_COMMON_AUDIO_SIGNAL_PROCESSING_DOT_PRODUCT_WITH_SCALE_H_
diff --git a/webrtc/common_audio/signal_processing/include/signal_processing_library.h b/webrtc/common_audio/signal_processing/include/signal_processing_library.h
index 7fa68e0..f1d605b 100644
--- a/webrtc/common_audio/signal_processing/include/signal_processing_library.h
+++ b/webrtc/common_audio/signal_processing/include/signal_processing_library.h
@@ -19,6 +19,7 @@
 #define WEBRTC_SPL_SIGNAL_PROCESSING_LIBRARY_H_
 
 #include <string.h>
+#include "webrtc/common_audio/signal_processing/dot_product_with_scale.h"
 #include "webrtc/typedefs.h"
 
 // Macros specific for the fixed point implementation
@@ -597,22 +598,6 @@
                          size_t vector_length,
                          int* scale_factor);
 
-// Calculates the dot product between two (int16_t) vectors.
-//
-// Input:
-//      - vector1       : Vector 1
-//      - vector2       : Vector 2
-//      - vector_length : Number of samples used in the dot product
-//      - scaling       : The number of right bit shifts to apply on each term
-//                        during calculation to avoid overflow, i.e., the
-//                        output will be in Q(-|scaling|)
-//
-// Return value         : The dot product in Q(-scaling)
-int32_t WebRtcSpl_DotProductWithScale(const int16_t* vector1,
-                                      const int16_t* vector2,
-                                      size_t length,
-                                      int scaling);
-
 // Filter operations.
 size_t WebRtcSpl_FilterAR(const int16_t* ar_coef,
                           size_t ar_coef_length,