Skip to content

Commit

Permalink
Add support for rejecting values larger than the max trackable value. #…
Browse files Browse the repository at this point in the history
  • Loading branch information
mikeb01 committed Nov 14, 2024
1 parent e33c19c commit 070ee7e
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 15 deletions.
21 changes: 10 additions & 11 deletions src/hdr_histogram.c
Original file line number Diff line number Diff line change
Expand Up @@ -341,10 +341,10 @@ static int32_t buckets_needed_to_cover_value(int64_t value, int32_t sub_bucket_c
/* ## ## ######## ## ## ####### ## ## ## */

int hdr_calculate_bucket_config(
int64_t lowest_discernible_value,
int64_t highest_trackable_value,
int significant_figures,
struct hdr_histogram_bucket_config* cfg)
int64_t lowest_discernible_value,
int64_t highest_trackable_value,
int significant_figures,
struct hdr_histogram_bucket_config* cfg)
{
int32_t sub_bucket_count_magnitude;
int64_t largest_value_with_single_unit_resolution;
Expand Down Expand Up @@ -406,10 +406,10 @@ void hdr_init_preallocated(struct hdr_histogram* h, struct hdr_histogram_bucket_
}

int hdr_init(
int64_t lowest_discernible_value,
int64_t highest_trackable_value,
int significant_figures,
struct hdr_histogram** result)
int64_t lowest_discernible_value,
int64_t highest_trackable_value,
int significant_figures,
struct hdr_histogram** result)
{
int64_t* counts;
struct hdr_histogram_bucket_config cfg;
Expand Down Expand Up @@ -492,13 +492,12 @@ bool hdr_record_values(struct hdr_histogram* h, int64_t value, int64_t count)
{
int32_t counts_index;

if (value < 0)
if (value < 0 || h->highest_trackable_value < value)
{
return false;
}

counts_index = counts_index_for(h, value);

if (counts_index < 0 || h->counts_len <= counts_index)
{
return false;
Expand All @@ -514,7 +513,7 @@ bool hdr_record_values_atomic(struct hdr_histogram* h, int64_t value, int64_t co
{
int32_t counts_index;

if (value < 0)
if (value < 0 || h->highest_trackable_value < value)
{
return false;
}
Expand Down
4 changes: 2 additions & 2 deletions test/hdr_histogram_atomic_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -435,8 +435,8 @@ static char* test_out_of_range_values(void)
{
struct hdr_histogram *h;
hdr_init(1, 1000, 4, &h);
mu_assert("Should successfully record value", hdr_record_value_atomic(h, 32767));
mu_assert("Should not record value", !hdr_record_value_atomic(h, 32768));
mu_assert("Should successfully record value", hdr_record_value_atomic(h, 1000));
mu_assert("Should not record value", !hdr_record_value_atomic(h, 1001));

return 0;
}
Expand Down
4 changes: 2 additions & 2 deletions test/hdr_histogram_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -450,8 +450,8 @@ static char* test_out_of_range_values(void)
{
struct hdr_histogram *h;
hdr_init(1, 1000, 4, &h);
mu_assert("Should successfully record value", hdr_record_value(h, 32767));
mu_assert("Should not record value", !hdr_record_value(h, 32768));
mu_assert("Should successfully record value", hdr_record_value(h, 1000));
mu_assert("Should not record value", !hdr_record_value(h, 1001));

return 0;
}
Expand Down

0 comments on commit 070ee7e

Please sign in to comment.