Skip to content

Commit

Permalink
stopwatch: Fix buffer underflow when computing percentiles.
Browse files Browse the repository at this point in the history
UB Sanitizer report:
  lib/stopwatch.c:119:22: runtime error: index 18446744073709551615 out of
                          bounds for type 'long long unsigned int [50]'
      #0 0x698358 in calc_percentile lib/stopwatch.c:119
      #1 0x69ada1 in add_sample lib/stopwatch.c:231
      #2 0x69c086 in stopwatch_end_sample_protected lib/stopwatch.c:386
      #3 0x69c522 in stopwatch_thread lib/stopwatch.c:441
      #4 0x684bae in ovsthread_wrapper lib/ovs-thread.c:383
      #5 0x7f042838b298 in start_thread (/lib64/libpthread.so.0+0x9298)
      #6 0x7f04277f2352 in clone (/lib64/libc.so.6+0x100352)

Acked-by: Aaron Conole <aconole@redhat.com>
Signed-off-by: Dumitru Ceara <dceara@redhat.com>
Acked-by: Paolo Valerio <pvalerio@redhat.com>
Signed-off-by: Ilya Maximets <i.maximets@ovn.org>
  • Loading branch information
dceara authored and igsilya committed Feb 14, 2022
1 parent 5a9bb85 commit b07c2e9
Showing 1 changed file with 1 addition and 3 deletions.
4 changes: 1 addition & 3 deletions lib/stopwatch.c
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,6 @@ static void
calc_percentile(unsigned long long n_samples, struct percentile *pctl,
unsigned long long new_sample)
{

if (n_samples < P_SQUARE_MIN) {
pctl->samples[n_samples - 1] = new_sample;
}
Expand Down Expand Up @@ -228,13 +227,12 @@ add_sample(struct stopwatch *sw, unsigned long long new_sample)
sw->min = new_sample;
}

calc_percentile(sw->n_samples, &sw->pctl, new_sample);

if (sw->n_samples++ == 0) {
sw->short_term.average = sw->long_term.average = new_sample;
return;
}

calc_percentile(sw->n_samples, &sw->pctl, new_sample);
calc_average(&sw->short_term, new_sample);
calc_average(&sw->long_term, new_sample);
}
Expand Down

0 comments on commit b07c2e9

Please sign in to comment.