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

Side by Side Diff: base/metrics/histogram_samples.cc

Issue 2853853002: Fix overflow when logging MaxInt32 to a sparse histogram. (Closed)
Patch Set: Created 3 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 unified diff | Download patch
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "base/metrics/histogram_samples.h" 5 #include "base/metrics/histogram_samples.h"
6 6
7 #include <limits> 7 #include <limits>
8 8
9 #include "base/compiler_specific.h" 9 #include "base/compiler_specific.h"
10 #include "base/numerics/safe_math.h" 10 #include "base/numerics/safe_math.h"
(...skipping 13 matching lines...) Expand all
24 // handled in the code but it's worth making them as unlikely as possible. 24 // handled in the code but it's worth making them as unlikely as possible.
25 constexpr int32_t kDisabledSingleSample = -1; 25 constexpr int32_t kDisabledSingleSample = -1;
26 26
27 class SampleCountPickleIterator : public SampleCountIterator { 27 class SampleCountPickleIterator : public SampleCountIterator {
28 public: 28 public:
29 explicit SampleCountPickleIterator(PickleIterator* iter); 29 explicit SampleCountPickleIterator(PickleIterator* iter);
30 30
31 bool Done() const override; 31 bool Done() const override;
32 void Next() override; 32 void Next() override;
33 void Get(HistogramBase::Sample* min, 33 void Get(HistogramBase::Sample* min,
34 HistogramBase::Sample* max, 34 int64_t* max,
35 HistogramBase::Count* count) const override; 35 HistogramBase::Count* count) const override;
36 36
37 private: 37 private:
38 PickleIterator* const iter_; 38 PickleIterator* const iter_;
39 39
40 HistogramBase::Sample min_; 40 HistogramBase::Sample min_;
41 HistogramBase::Sample max_; 41 int64_t max_;
42 HistogramBase::Count count_; 42 HistogramBase::Count count_;
43 bool is_done_; 43 bool is_done_;
44 }; 44 };
45 45
46 SampleCountPickleIterator::SampleCountPickleIterator(PickleIterator* iter) 46 SampleCountPickleIterator::SampleCountPickleIterator(PickleIterator* iter)
47 : iter_(iter), 47 : iter_(iter),
48 is_done_(false) { 48 is_done_(false) {
49 Next(); 49 Next();
50 } 50 }
51 51
52 bool SampleCountPickleIterator::Done() const { 52 bool SampleCountPickleIterator::Done() const {
53 return is_done_; 53 return is_done_;
54 } 54 }
55 55
56 void SampleCountPickleIterator::Next() { 56 void SampleCountPickleIterator::Next() {
57 DCHECK(!Done()); 57 DCHECK(!Done());
58 if (!iter_->ReadInt(&min_) || 58 if (!iter_->ReadInt(&min_) || !iter_->ReadInt64(&max_) ||
59 !iter_->ReadInt(&max_) || 59 !iter_->ReadInt(&count_)) {
60 !iter_->ReadInt(&count_))
61 is_done_ = true; 60 is_done_ = true;
61 }
62 } 62 }
63 63
64 void SampleCountPickleIterator::Get(HistogramBase::Sample* min, 64 void SampleCountPickleIterator::Get(HistogramBase::Sample* min,
65 HistogramBase::Sample* max, 65 int64_t* max,
66 HistogramBase::Count* count) const { 66 HistogramBase::Count* count) const {
67 DCHECK(!Done()); 67 DCHECK(!Done());
68 *min = min_; 68 *min = min_;
69 *max = max_; 69 *max = max_;
70 *count = count_; 70 *count = count_;
71 } 71 }
72 72
73 } // namespace 73 } // namespace
74 74
75 static_assert(sizeof(HistogramSamples::AtomicSingleSample) == 75 static_assert(sizeof(HistogramSamples::AtomicSingleSample) ==
(...skipping 137 matching lines...) Expand 10 before | Expand all | Expand 10 after
213 DCHECK(success); 213 DCHECK(success);
214 } 214 }
215 215
216 bool HistogramSamples::Serialize(Pickle* pickle) const { 216 bool HistogramSamples::Serialize(Pickle* pickle) const {
217 if (!pickle->WriteInt64(sum())) 217 if (!pickle->WriteInt64(sum()))
218 return false; 218 return false;
219 if (!pickle->WriteInt(redundant_count())) 219 if (!pickle->WriteInt(redundant_count()))
220 return false; 220 return false;
221 221
222 HistogramBase::Sample min; 222 HistogramBase::Sample min;
223 HistogramBase::Sample max; 223 int64_t max;
224 HistogramBase::Count count; 224 HistogramBase::Count count;
225 for (std::unique_ptr<SampleCountIterator> it = Iterator(); !it->Done(); 225 for (std::unique_ptr<SampleCountIterator> it = Iterator(); !it->Done();
226 it->Next()) { 226 it->Next()) {
227 it->Get(&min, &max, &count); 227 it->Get(&min, &max, &count);
228 if (!pickle->WriteInt(min) || 228 if (!pickle->WriteInt(min) || !pickle->WriteInt64(max) ||
229 !pickle->WriteInt(max) || 229 !pickle->WriteInt(count)) {
230 !pickle->WriteInt(count))
231 return false; 230 return false;
231 }
232 } 232 }
233 return true; 233 return true;
234 } 234 }
235 235
236 bool HistogramSamples::AccumulateSingleSample(HistogramBase::Sample value, 236 bool HistogramSamples::AccumulateSingleSample(HistogramBase::Sample value,
237 HistogramBase::Count count, 237 HistogramBase::Count count,
238 size_t bucket) { 238 size_t bucket) {
239 if (single_sample().Accumulate(bucket, count)) { 239 if (single_sample().Accumulate(bucket, count)) {
240 // Success. Update the (separate) sum and redundant-count. 240 // Success. Update the (separate) sum and redundant-count.
241 IncreaseSumAndCount(static_cast<int64_t>(value) * count, count); 241 IncreaseSumAndCount(static_cast<int64_t>(value) * count, count);
(...skipping 13 matching lines...) Expand all
255 } 255 }
256 256
257 SampleCountIterator::~SampleCountIterator() {} 257 SampleCountIterator::~SampleCountIterator() {}
258 258
259 bool SampleCountIterator::GetBucketIndex(size_t* index) const { 259 bool SampleCountIterator::GetBucketIndex(size_t* index) const {
260 DCHECK(!Done()); 260 DCHECK(!Done());
261 return false; 261 return false;
262 } 262 }
263 263
264 SingleSampleIterator::SingleSampleIterator(HistogramBase::Sample min, 264 SingleSampleIterator::SingleSampleIterator(HistogramBase::Sample min,
265 HistogramBase::Sample max, 265 int64_t max,
266 HistogramBase::Count count) 266 HistogramBase::Count count)
267 : SingleSampleIterator(min, max, count, kSizeMax) {} 267 : SingleSampleIterator(min, max, count, kSizeMax) {}
268 268
269 SingleSampleIterator::SingleSampleIterator(HistogramBase::Sample min, 269 SingleSampleIterator::SingleSampleIterator(HistogramBase::Sample min,
270 HistogramBase::Sample max, 270 int64_t max,
271 HistogramBase::Count count, 271 HistogramBase::Count count,
272 size_t bucket_index) 272 size_t bucket_index)
273 : min_(min), max_(max), bucket_index_(bucket_index), count_(count) {} 273 : min_(min), max_(max), bucket_index_(bucket_index), count_(count) {}
274 274
275 SingleSampleIterator::~SingleSampleIterator() {} 275 SingleSampleIterator::~SingleSampleIterator() {}
276 276
277 bool SingleSampleIterator::Done() const { 277 bool SingleSampleIterator::Done() const {
278 return count_ == 0; 278 return count_ == 0;
279 } 279 }
280 280
281 void SingleSampleIterator::Next() { 281 void SingleSampleIterator::Next() {
282 DCHECK(!Done()); 282 DCHECK(!Done());
283 count_ = 0; 283 count_ = 0;
284 } 284 }
285 285
286 void SingleSampleIterator::Get(HistogramBase::Sample* min, 286 void SingleSampleIterator::Get(HistogramBase::Sample* min,
287 HistogramBase::Sample* max, 287 int64_t* max,
288 HistogramBase::Count* count) const { 288 HistogramBase::Count* count) const {
289 DCHECK(!Done()); 289 DCHECK(!Done());
290 if (min != nullptr) 290 if (min != nullptr)
291 *min = min_; 291 *min = min_;
292 if (max != nullptr) 292 if (max != nullptr)
293 *max = max_; 293 *max = max_;
294 if (count != nullptr) 294 if (count != nullptr)
295 *count = count_; 295 *count = count_;
296 } 296 }
297 297
298 bool SingleSampleIterator::GetBucketIndex(size_t* index) const { 298 bool SingleSampleIterator::GetBucketIndex(size_t* index) const {
299 DCHECK(!Done()); 299 DCHECK(!Done());
300 if (bucket_index_ == kSizeMax) 300 if (bucket_index_ == kSizeMax)
301 return false; 301 return false;
302 *index = bucket_index_; 302 *index = bucket_index_;
303 return true; 303 return true;
304 } 304 }
305 305
306 } // namespace base 306 } // namespace base
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698