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

Side by Side Diff: base/metrics/sample_map.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/sample_map.h" 5 #include "base/metrics/sample_map.h"
6 6
7 #include "base/logging.h" 7 #include "base/logging.h"
8 #include "base/memory/ptr_util.h" 8 #include "base/memory/ptr_util.h"
9 #include "base/stl_util.h" 9 #include "base/stl_util.h"
10 10
(...skipping 12 matching lines...) Expand all
23 typedef std::map<HistogramBase::Sample, HistogramBase::Count> 23 typedef std::map<HistogramBase::Sample, HistogramBase::Count>
24 SampleToCountMap; 24 SampleToCountMap;
25 25
26 explicit SampleMapIterator(const SampleToCountMap& sample_counts); 26 explicit SampleMapIterator(const SampleToCountMap& sample_counts);
27 ~SampleMapIterator() override; 27 ~SampleMapIterator() override;
28 28
29 // SampleCountIterator: 29 // SampleCountIterator:
30 bool Done() const override; 30 bool Done() const override;
31 void Next() override; 31 void Next() override;
32 void Get(HistogramBase::Sample* min, 32 void Get(HistogramBase::Sample* min,
33 HistogramBase::Sample* max, 33 int64_t* max,
34 HistogramBase::Count* count) const override; 34 HistogramBase::Count* count) const override;
35 35
36 private: 36 private:
37 void SkipEmptyBuckets(); 37 void SkipEmptyBuckets();
38 38
39 SampleToCountMap::const_iterator iter_; 39 SampleToCountMap::const_iterator iter_;
40 const SampleToCountMap::const_iterator end_; 40 const SampleToCountMap::const_iterator end_;
41 }; 41 };
42 42
43 SampleMapIterator::SampleMapIterator(const SampleToCountMap& sample_counts) 43 SampleMapIterator::SampleMapIterator(const SampleToCountMap& sample_counts)
44 : iter_(sample_counts.begin()), 44 : iter_(sample_counts.begin()),
45 end_(sample_counts.end()) { 45 end_(sample_counts.end()) {
46 SkipEmptyBuckets(); 46 SkipEmptyBuckets();
47 } 47 }
48 48
49 SampleMapIterator::~SampleMapIterator() {} 49 SampleMapIterator::~SampleMapIterator() {}
50 50
51 bool SampleMapIterator::Done() const { 51 bool SampleMapIterator::Done() const {
52 return iter_ == end_; 52 return iter_ == end_;
53 } 53 }
54 54
55 void SampleMapIterator::Next() { 55 void SampleMapIterator::Next() {
56 DCHECK(!Done()); 56 DCHECK(!Done());
57 ++iter_; 57 ++iter_;
58 SkipEmptyBuckets(); 58 SkipEmptyBuckets();
59 } 59 }
60 60
61 void SampleMapIterator::Get(Sample* min, Sample* max, Count* count) const { 61 void SampleMapIterator::Get(Sample* min, int64_t* max, Count* count) const {
62 DCHECK(!Done()); 62 DCHECK(!Done());
63 if (min) 63 if (min)
64 *min = iter_->first; 64 *min = iter_->first;
65 if (max) 65 if (max)
66 *max = iter_->first + 1; 66 *max = static_cast<int64_t>(iter_->first) + 1;
67 if (count) 67 if (count)
68 *count = iter_->second; 68 *count = iter_->second;
69 } 69 }
70 70
71 void SampleMapIterator::SkipEmptyBuckets() { 71 void SampleMapIterator::SkipEmptyBuckets() {
72 while (!Done() && iter_->second == 0) { 72 while (!Done() && iter_->second == 0) {
73 ++iter_; 73 ++iter_;
74 } 74 }
75 } 75 }
76 76
(...skipping 24 matching lines...) Expand all
101 } 101 }
102 return count; 102 return count;
103 } 103 }
104 104
105 std::unique_ptr<SampleCountIterator> SampleMap::Iterator() const { 105 std::unique_ptr<SampleCountIterator> SampleMap::Iterator() const {
106 return WrapUnique(new SampleMapIterator(sample_counts_)); 106 return WrapUnique(new SampleMapIterator(sample_counts_));
107 } 107 }
108 108
109 bool SampleMap::AddSubtractImpl(SampleCountIterator* iter, Operator op) { 109 bool SampleMap::AddSubtractImpl(SampleCountIterator* iter, Operator op) {
110 Sample min; 110 Sample min;
111 Sample max; 111 int64_t max;
112 Count count; 112 Count count;
113 for (; !iter->Done(); iter->Next()) { 113 for (; !iter->Done(); iter->Next()) {
114 iter->Get(&min, &max, &count); 114 iter->Get(&min, &max, &count);
115 if (min + 1 != max) 115 if (static_cast<int64_t>(min) + 1 != max)
116 return false; // SparseHistogram only supports bucket with size 1. 116 return false; // SparseHistogram only supports bucket with size 1.
117 117
118 sample_counts_[min] += (op == HistogramSamples::ADD) ? count : -count; 118 sample_counts_[min] += (op == HistogramSamples::ADD) ? count : -count;
119 } 119 }
120 return true; 120 return true;
121 } 121 }
122 122
123 } // namespace base 123 } // namespace base
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698