Skip to content

Commit

Permalink
Merge pull request #526 from visitorckw/fix-qsort-cmp
Browse files Browse the repository at this point in the history
Fix undefined behavior in qsort comparison functions for rv_histogram
  • Loading branch information
jserv authored Dec 24, 2024
2 parents f69d9dd + 59bf184 commit 337f4ef
Showing 1 changed file with 16 additions and 2 deletions.
18 changes: 16 additions & 2 deletions tools/rv_histogram.c
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,26 @@ static rv_hist_t rv_reg_stats[] = {

static int cmp_dec(const void *a, const void *b)
{
return ((rv_hist_t *) b)->freq - ((rv_hist_t *) a)->freq;
const size_t a_freq = ((rv_hist_t *) a)->freq;
const size_t b_freq = ((rv_hist_t *) b)->freq;

if (a_freq > b_freq)
return -1;
if (a_freq < b_freq)
return 1;
return 0;
}

static int cmp_asc(const void *a, const void *b)
{
return ((rv_hist_t *) a)->freq - ((rv_hist_t *) b)->freq;
const size_t a_freq = ((rv_hist_t *) a)->freq;
const size_t b_freq = ((rv_hist_t *) b)->freq;

if (a_freq < b_freq)
return -1;
if (a_freq > b_freq)
return 1;
return 0;
}

/* used to adjust the length of histogram bar */
Expand Down

0 comments on commit 337f4ef

Please sign in to comment.