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

Side by Side Diff: base/metrics/sample_vector.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_vector.h" 5 #include "base/metrics/sample_vector.h"
6 6
7 #include "base/lazy_instance.h" 7 #include "base/lazy_instance.h"
8 #include "base/logging.h" 8 #include "base/logging.h"
9 #include "base/memory/ptr_util.h" 9 #include "base/memory/ptr_util.h"
10 #include "base/metrics/persistent_memory_allocator.h" 10 #include "base/metrics/persistent_memory_allocator.h"
(...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after
125 } 125 }
126 126
127 bool SampleVectorBase::AddSubtractImpl(SampleCountIterator* iter, 127 bool SampleVectorBase::AddSubtractImpl(SampleCountIterator* iter,
128 HistogramSamples::Operator op) { 128 HistogramSamples::Operator op) {
129 // Stop now if there's nothing to do. 129 // Stop now if there's nothing to do.
130 if (iter->Done()) 130 if (iter->Done())
131 return true; 131 return true;
132 132
133 // Get the first value and its index. 133 // Get the first value and its index.
134 HistogramBase::Sample min; 134 HistogramBase::Sample min;
135 HistogramBase::Sample max; 135 int64_t max;
136 HistogramBase::Count count; 136 HistogramBase::Count count;
137 iter->Get(&min, &max, &count); 137 iter->Get(&min, &max, &count);
138 size_t dest_index = GetBucketIndex(min); 138 size_t dest_index = GetBucketIndex(min);
139 139
140 // The destination must be a superset of the source meaning that though the 140 // The destination must be a superset of the source meaning that though the
141 // incoming ranges will find an exact match, the incoming bucket-index, if 141 // incoming ranges will find an exact match, the incoming bucket-index, if
142 // it exists, may be offset from the destination bucket-index. Calculate 142 // it exists, may be offset from the destination bucket-index. Calculate
143 // that offset of the passed iterator; there are are no overflow checks 143 // that offset of the passed iterator; there are are no overflow checks
144 // because 2's compliment math will work it out in the end. 144 // because 2's compliment math will work it out in the end.
145 // 145 //
(...skipping 239 matching lines...) Expand 10 before | Expand all | Expand 10 after
385 return index_ >= counts_size_; 385 return index_ >= counts_size_;
386 } 386 }
387 387
388 void SampleVectorIterator::Next() { 388 void SampleVectorIterator::Next() {
389 DCHECK(!Done()); 389 DCHECK(!Done());
390 index_++; 390 index_++;
391 SkipEmptyBuckets(); 391 SkipEmptyBuckets();
392 } 392 }
393 393
394 void SampleVectorIterator::Get(HistogramBase::Sample* min, 394 void SampleVectorIterator::Get(HistogramBase::Sample* min,
395 HistogramBase::Sample* max, 395 int64_t* max,
396 HistogramBase::Count* count) const { 396 HistogramBase::Count* count) const {
397 DCHECK(!Done()); 397 DCHECK(!Done());
398 if (min != NULL) 398 if (min != NULL)
399 *min = bucket_ranges_->range(index_); 399 *min = bucket_ranges_->range(index_);
400 if (max != NULL) 400 if (max != NULL)
401 *max = bucket_ranges_->range(index_ + 1); 401 *max = static_cast<int64_t>(bucket_ranges_->range(index_ + 1));
402 if (count != NULL) 402 if (count != NULL)
403 *count = subtle::NoBarrier_Load(&counts_[index_]); 403 *count = subtle::NoBarrier_Load(&counts_[index_]);
404 } 404 }
405 405
406 bool SampleVectorIterator::GetBucketIndex(size_t* index) const { 406 bool SampleVectorIterator::GetBucketIndex(size_t* index) const {
407 DCHECK(!Done()); 407 DCHECK(!Done());
408 if (index != NULL) 408 if (index != NULL)
409 *index = index_; 409 *index = index_;
410 return true; 410 return true;
411 } 411 }
412 412
413 void SampleVectorIterator::SkipEmptyBuckets() { 413 void SampleVectorIterator::SkipEmptyBuckets() {
414 if (Done()) 414 if (Done())
415 return; 415 return;
416 416
417 while (index_ < counts_size_) { 417 while (index_ < counts_size_) {
418 if (subtle::NoBarrier_Load(&counts_[index_]) != 0) 418 if (subtle::NoBarrier_Load(&counts_[index_]) != 0)
419 return; 419 return;
420 index_++; 420 index_++;
421 } 421 }
422 } 422 }
423 423
424 } // namespace base 424 } // namespace base
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698