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

Unified Diff: webrtc/modules/audio_coding/codecs/ilbc/my_corr.c

Issue 2014033002: Fix UBSan errors (signed integer overflow) (Closed) Base URL: https://chromium.googlesource.com/external/webrtc.git@bug601787-2
Patch Set: logic fix 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/modules/audio_coding/codecs/ilbc/smooth.c » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: webrtc/modules/audio_coding/codecs/ilbc/my_corr.c
diff --git a/webrtc/modules/audio_coding/codecs/ilbc/my_corr.c b/webrtc/modules/audio_coding/codecs/ilbc/my_corr.c
index bd6ff561c2c5643e8961f2e0ab1f2594205a801a..80847b6099cd856aaccf5c00c8ed4d4c31295389 100644
--- a/webrtc/modules/audio_coding/codecs/ilbc/my_corr.c
+++ b/webrtc/modules/audio_coding/codecs/ilbc/my_corr.c
@@ -29,25 +29,26 @@ void WebRtcIlbcfix_MyCorr(
const int16_t* seq2, /* (i) second sequence */
size_t dim2 /* (i) dimension seq2 */
){
- int16_t max;
+ uint32_t max1, max2;
size_t loops;
- int scale;
-
- /* Calculate correlation between the two sequences. Scale the
- result of the multiplcication to maximum 26 bits in order
- to avoid overflow */
- max=WebRtcSpl_MaxAbsValueW16(seq1, dim1);
- scale=WebRtcSpl_GetSizeInBits(max);
-
- scale = 2 * scale - 26;
- if (scale<0) {
- scale=0;
+ int right_shift;
+
+ // Calculate a right shift that will let us sum dim2 pairwise products of
+ // values from the two sequences without overflowing an int32_t. (The +1 in
+ // max1 and max2 are because WebRtcSpl_MaxAbsValueW16 will return 2**15 - 1
+ // if the input array contains -2**15.)
+ max1 = WebRtcSpl_MaxAbsValueW16(seq1, dim1) + 1;
+ max2 = WebRtcSpl_MaxAbsValueW16(seq2, dim2) + 1;
+ right_shift =
+ (64 - 31) - WebRtcSpl_CountLeadingZeros64((max1 * max2) * (uint64_t)dim2);
+ if (right_shift < 0) {
+ right_shift = 0;
}
loops=dim1-dim2+1;
/* Calculate the cross correlations */
- WebRtcSpl_CrossCorrelation(corr, seq2, seq1, dim2, loops, scale, 1);
+ WebRtcSpl_CrossCorrelation(corr, seq2, seq1, dim2, loops, right_shift, 1);
return;
}
« no previous file with comments | « no previous file | webrtc/modules/audio_coding/codecs/ilbc/smooth.c » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698