commit e6941a1da4a8516d3a822e24f9eec12f9bfcb462
parent d5517ae72e590f20ce302db9cdd19f5811571e91
Author: Vincent Forest <vincent.forest@meso-star.com>
Date: Tue, 9 Feb 2016 11:25:12 +0100
Fix the histogram sampling
The comparator of histogram values was wrongly defined leading to
invalid memory read.
Diffstat:
1 file changed, 7 insertions(+), 5 deletions(-)
diff --git a/src/schiff_histogram.c b/src/schiff_histogram.c
@@ -78,11 +78,11 @@ parse_histogram_proba
static INLINE int
cmp_entry(const void* a, const void* b)
{
- double val;
+ const struct schiff_hentry* key = a;
const struct schiff_hentry* entry = b;
ASSERT(a && b);
- val = *(const double*)a;
- return val < entry->accum_proba ? -1 : val > entry->accum_proba ? 1 : 0;
+ return key->accum_proba < entry->accum_proba
+ ? -1 : key->accum_proba > entry->accum_proba ? 1 : 0;
}
static res_T
@@ -271,6 +271,7 @@ schiff_histogram_sample(const struct darray_hentry* histo, const double u)
{
const struct schiff_hentry* entries;
const struct schiff_hentry* find;
+ struct schiff_hentry samp;
size_t nentries;
double v;
ASSERT(histo && u >= 0.0 && u < 1.0);
@@ -279,8 +280,9 @@ schiff_histogram_sample(const struct darray_hentry* histo, const double u)
nentries = darray_hentry_size_get(histo);
ASSERT(nentries);
- find = search_lower_bound
- (&u, entries, nentries, sizeof(struct schiff_hentry), cmp_entry);
+ samp.accum_proba = u;
+ find = search_lower_bound(&samp, entries, nentries,
+ sizeof(struct schiff_hentry), cmp_entry);
if(find == entries)
return find->value;