From ceff7a79bc7a32a3a6590f2032b3192402a6d845 Mon Sep 17 00:00:00 2001 From: John Halley Gotway Date: Mon, 14 Aug 2023 14:54:21 -0600 Subject: [PATCH 1/4] Per #2644, prevent reading past the end of the array. --- src/basic/vx_math/ptile.cc | 25 ++++++++++++++++++++----- 1 file changed, 20 insertions(+), 5 deletions(-) diff --git a/src/basic/vx_math/ptile.cc b/src/basic/vx_math/ptile.cc index 6bf84ce5e0..b9b0d20da9 100644 --- a/src/basic/vx_math/ptile.cc +++ b/src/basic/vx_math/ptile.cc @@ -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 ); @@ -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]; + } } From 886d59eecc33e4a96b247b3f729e607df351b416 Mon Sep 17 00:00:00 2001 From: John Halley Gotway Date: Mon, 14 Aug 2023 15:32:08 -0600 Subject: [PATCH 2/4] Per #2644, additional range checking to handle bad input values of t. --- src/basic/vx_math/ptile.cc | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/basic/vx_math/ptile.cc b/src/basic/vx_math/ptile.cc index b9b0d20da9..75b834f934 100644 --- a/src/basic/vx_math/ptile.cc +++ b/src/basic/vx_math/ptile.cc @@ -72,7 +72,7 @@ if ( n > 0 ) { p = ordered_array[index]; } // Interpolate linearly between two values - else { + else if ( index >= 0 && index < (n - 1) ) { delta = (n - 1)*t - index; p = (1 - delta)*ordered_array[index] + delta*ordered_array[index + 1]; } @@ -119,7 +119,7 @@ if ( n > 0 ) { p = ordered_array[index]; } // Interpolate linearly between two values - else { + else if ( index >= 0 && index < (n - 1) ) { delta = (n - 1)*t - index; p = (1 - delta)*ordered_array[index] + delta*ordered_array[index + 1]; } From fa96e6f11f6d09a5eddae11366d36d543bef42f3 Mon Sep 17 00:00:00 2001 From: John Halley Gotway Date: Wed, 16 Aug 2023 10:57:03 -0600 Subject: [PATCH 3/4] Per #2644, error out if the requested percentile is out of range. --- src/basic/vx_math/ptile.cc | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/src/basic/vx_math/ptile.cc b/src/basic/vx_math/ptile.cc index 75b834f934..9ff068b8b1 100644 --- a/src/basic/vx_math/ptile.cc +++ b/src/basic/vx_math/ptile.cc @@ -63,6 +63,17 @@ int index; double delta; double p = bad_data_double; +// Range check +if ( t < 0.0 || t > 1.0 ) { + + mlog << Error << "\npercentile() -> " + << "requested percentile value (" << t + << ") must be between 0 and 1!\n\n"; + + exit ( 1 ); + +} + if ( n > 0 ) { index = nint(floor((n - 1)*t)); @@ -76,6 +87,7 @@ if ( n > 0 ) { delta = (n - 1)*t - index; p = (1 - delta)*ordered_array[index] + delta*ordered_array[index + 1]; } + } return ( p ); @@ -110,6 +122,17 @@ int index; float delta; float p = bad_data_float; +// Range check +if ( t < 0.0 || t > 1.0 ) { + + mlog << Error << "\npercentile_f() -> " + << "requested percentile value (" << t + << ") must be between 0 and 1!\n\n"; + + exit ( 1 ); + +} + if ( n > 0 ) { index = nint(floor((n - 1)*t)); From 346c1bb78e86dd0552a284fa9be311a1d7adc5e8 Mon Sep 17 00:00:00 2001 From: John Halley Gotway Date: Wed, 16 Aug 2023 11:15:24 -0600 Subject: [PATCH 4/4] Per #2644, include vx_log.h --- src/basic/vx_math/ptile.cc | 1 + 1 file changed, 1 insertion(+) diff --git a/src/basic/vx_math/ptile.cc b/src/basic/vx_math/ptile.cc index 9ff068b8b1..9812403a84 100644 --- a/src/basic/vx_math/ptile.cc +++ b/src/basic/vx_math/ptile.cc @@ -23,6 +23,7 @@ using namespace std; #include "is_bad_data.h" #include "nint.h" +#include "vx_log.h" ///////////////////////////////////////////////////////////////////////////////