Skip to content

Commit

Permalink
Per #2644, prevent reading past the end of the array.
Browse files Browse the repository at this point in the history
  • Loading branch information
JohnHalleyGotway committed Aug 14, 2023
1 parent bc95512 commit ceff7a7
Showing 1 changed file with 20 additions and 5 deletions.
25 changes: 20 additions & 5 deletions src/basic/vx_math/ptile.cc
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,18 @@ double delta;
double p = bad_data_double;

if ( n > 0 ) {

index = nint(floor((n - 1)*t));
delta = (n-1)*t - index;
p = (1 - delta)*ordered_array[index] + delta*ordered_array[index+1];

// Use the last value
if ( index == (n - 1) ) {
p = ordered_array[index];
}
// Interpolate linearly between two values
else {
delta = (n - 1)*t - index;
p = (1 - delta)*ordered_array[index] + delta*ordered_array[index + 1];
}
}

return ( p );
Expand Down Expand Up @@ -105,9 +114,15 @@ if ( n > 0 ) {

index = nint(floor((n - 1)*t));

delta = (n - 1)*t - index;

p = (1 - delta)*(ordered_array[index]) + delta*(ordered_array[index + 1]);
// Use the last value
if ( index == (n - 1) ) {
p = ordered_array[index];
}
// Interpolate linearly between two values
else {
delta = (n - 1)*t - index;
p = (1 - delta)*ordered_array[index] + delta*ordered_array[index + 1];
}

}

Expand Down

0 comments on commit ceff7a7

Please sign in to comment.