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

Side by Side Diff: webrtc/base/rollingaccumulator.h

Issue 1744183002: Fix some signed overflow errors causing undefined behavior (in theory). (Closed) Base URL: https://chromium.googlesource.com/external/webrtc.git@master
Patch Set: Format and comment Created 4 years, 9 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 unified diff | Download patch
« no previous file with comments | « webrtc/base/random_unittest.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * Copyright 2011 The WebRTC Project Authors. All rights reserved. 2 * Copyright 2011 The WebRTC Project Authors. All rights reserved.
3 * 3 *
4 * Use of this source code is governed by a BSD-style license 4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source 5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found 6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may 7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree. 8 * be found in the AUTHORS file in the root of the source tree.
9 */ 9 */
10 10
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
49 max_stale_ = false; 49 max_stale_ = false;
50 min_ = T(); 50 min_ = T();
51 min_stale_ = false; 51 min_stale_ = false;
52 } 52 }
53 53
54 void AddSample(T sample) { 54 void AddSample(T sample) {
55 if (count_ == max_count()) { 55 if (count_ == max_count()) {
56 // Remove oldest sample. 56 // Remove oldest sample.
57 T sample_to_remove = samples_[next_index_]; 57 T sample_to_remove = samples_[next_index_];
58 sum_ -= sample_to_remove; 58 sum_ -= sample_to_remove;
59 sum_2_ -= sample_to_remove * sample_to_remove; 59 sum_2_ -= static_cast<double>(sample_to_remove) * sample_to_remove;
60 if (sample_to_remove >= max_) { 60 if (sample_to_remove >= max_) {
61 max_stale_ = true; 61 max_stale_ = true;
62 } 62 }
63 if (sample_to_remove <= min_) { 63 if (sample_to_remove <= min_) {
64 min_stale_ = true; 64 min_stale_ = true;
65 } 65 }
66 } else { 66 } else {
67 // Increase count of samples. 67 // Increase count of samples.
68 ++count_; 68 ++count_;
69 } 69 }
70 // Add new sample. 70 // Add new sample.
71 samples_[next_index_] = sample; 71 samples_[next_index_] = sample;
72 sum_ += sample; 72 sum_ += sample;
73 sum_2_ += sample * sample; 73 sum_2_ += static_cast<double>(sample) * sample;
74 if (count_ == 1 || sample >= max_) { 74 if (count_ == 1 || sample >= max_) {
75 max_ = sample; 75 max_ = sample;
76 max_stale_ = false; 76 max_stale_ = false;
77 } 77 }
78 if (count_ == 1 || sample <= min_) { 78 if (count_ == 1 || sample <= min_) {
79 min_ = sample; 79 min_ = sample;
80 min_stale_ = false; 80 min_stale_ = false;
81 } 81 }
82 // Update next_index_. 82 // Update next_index_.
83 next_index_ = (next_index_ + 1) % max_count(); 83 next_index_ = (next_index_ + 1) % max_count();
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after
164 mutable T min_; 164 mutable T min_;
165 mutable bool min_stale_; 165 mutable bool min_stale_;
166 std::vector<T> samples_; 166 std::vector<T> samples_;
167 167
168 RTC_DISALLOW_COPY_AND_ASSIGN(RollingAccumulator); 168 RTC_DISALLOW_COPY_AND_ASSIGN(RollingAccumulator);
169 }; 169 };
170 170
171 } // namespace rtc 171 } // namespace rtc
172 172
173 #endif // WEBRTC_BASE_ROLLINGACCUMULATOR_H_ 173 #endif // WEBRTC_BASE_ROLLINGACCUMULATOR_H_
OLDNEW
« no previous file with comments | « webrtc/base/random_unittest.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698