From 7915662a8e910451e2dc38bc5fe1d72186afc195 Mon Sep 17 00:00:00 2001 From: John Halley Gotway Date: Mon, 14 Aug 2023 14:49:43 -0600 Subject: [PATCH 1/5] 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 1869320d33..953d5e5598 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 b7f49299f9a1be0308cdf7ed27a4e8e25ec0005a Mon Sep 17 00:00:00 2001 From: John Halley Gotway Date: Mon, 14 Aug 2023 15:33:10 -0600 Subject: [PATCH 2/5] Per #2644, additional range checking to handle bad input values of t. --- src/basic/vx_math/ptile.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/basic/vx_math/ptile.cc b/src/basic/vx_math/ptile.cc index 953d5e5598..97c444e15f 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]; } From a62bb508e9a78b3b03c077165611b9f525495a3b Mon Sep 17 00:00:00 2001 From: John Halley Gotway Date: Mon, 14 Aug 2023 15:34:48 -0600 Subject: [PATCH 3/5] Per #2644, additional range checking to handle bad input values of t. --- src/basic/vx_math/ptile.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/basic/vx_math/ptile.cc b/src/basic/vx_math/ptile.cc index 97c444e15f..9c1ce2bcb7 100644 --- a/src/basic/vx_math/ptile.cc +++ b/src/basic/vx_math/ptile.cc @@ -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 c0a53111c4b95e0ca2a74a67cae35e3b28bb62be Mon Sep 17 00:00:00 2001 From: John Halley Gotway Date: Wed, 16 Aug 2023 11:01:30 -0600 Subject: [PATCH 4/5] 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 9c1ce2bcb7..85c7a884d2 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 512656627854ee1ac6998efc23ef59c2449b24b4 Mon Sep 17 00:00:00 2001 From: John Halley Gotway Date: Wed, 16 Aug 2023 11:04:01 -0600 Subject: [PATCH 5/5] 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 85c7a884d2..6bcc178e52 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" ///////////////////////////////////////////////////////////////////////////////