From b122c0cec2b449a0dd126307729249c72a570328 Mon Sep 17 00:00:00 2001 From: "rtenneti@chromium.org" Date: Wed, 23 Feb 2011 22:31:18 +0000 Subject: Added CHECKs to make sure histogram caller's don't create histogram that are larger 1G. Added CHECK's to catch bucket_count_ is not corrupted before accessing ranges_ vector. BUG=73452 TEST=none. histogram unit tests are enough. Review URL: http://codereview.chromium.org/6542034 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@75804 0039d316-1c4b-4281-b951-d872f2087c98 --- base/metrics/histogram.cc | 8 ++++++-- base/metrics/histogram.h | 2 ++ 2 files changed, 8 insertions(+), 2 deletions(-) (limited to 'base/metrics') diff --git a/base/metrics/histogram.cc b/base/metrics/histogram.cc index a308932..6ac92ed 100644 --- a/base/metrics/histogram.cc +++ b/base/metrics/histogram.cc @@ -23,6 +23,9 @@ namespace base { typedef Histogram::Count Count; +// static +const size_t Histogram::kBucketCount_MAX = 10000u; + scoped_refptr Histogram::FactoryGet(const std::string& name, Sample minimum, Sample maximum, size_t bucket_count, Flags flags) { scoped_refptr histogram(NULL); @@ -413,7 +416,7 @@ size_t Histogram::BucketIndex(Sample value) const { do { DCHECK_GE(over, under); - mid = (over + under)/2; + mid = under + (over - under)/2; if (mid == under) break; if (ranges(mid) <= value) @@ -423,7 +426,7 @@ size_t Histogram::BucketIndex(Sample value) const { } while (true); DCHECK_LE(ranges(mid), value); - DCHECK_GT(ranges(mid+1), value); + CHECK_GT(ranges(mid+1), value); return mid; } @@ -483,6 +486,7 @@ void Histogram::Initialize() { declared_max_ = kSampleType_MAX - 1; DCHECK_LE(declared_min_, declared_max_); DCHECK_GT(bucket_count_, 1u); + CHECK_LT(bucket_count_, kBucketCount_MAX); size_t maximal_bucket_count = declared_max_ - declared_min_ + 2; DCHECK_LE(bucket_count_, maximal_bucket_count); DCHECK_EQ(0, ranges_[0]); diff --git a/base/metrics/histogram.h b/base/metrics/histogram.h index 3bb3f030..2ec56e6 100644 --- a/base/metrics/histogram.h +++ b/base/metrics/histogram.h @@ -241,6 +241,8 @@ class Histogram : public base::RefCountedThreadSafe { typedef int Sample; // Used for samples (and ranges of samples). typedef int Count; // Used to count samples in a bucket. static const Sample kSampleType_MAX = INT_MAX; + // Initialize maximum number of buckets in histograms as 10,000. + static const size_t kBucketCount_MAX; typedef std::vector Counts; typedef std::vector Ranges; -- cgit v1.1