From ff176fa509aeb88a7b3b2bb0f1a8170af104acbe Mon Sep 17 00:00:00 2001 From: Igor Zolotarev Date: Wed, 8 Dec 2021 19:44:49 +0300 Subject: [PATCH] Add upper constraint to quicksort --- metrics/quantile.lua | 2 ++ test/quantile_test.lua | 11 +++++++++++ 2 files changed, 13 insertions(+) diff --git a/metrics/quantile.lua b/metrics/quantile.lua index ee1c380c..3460cbdc 100644 --- a/metrics/quantile.lua +++ b/metrics/quantile.lua @@ -13,6 +13,8 @@ local sample_constructor = ffi.typeof('sample') local function quicksort(array, low, high) assert(low >= 0, 'Low bound must be non-negative') + assert(high < ffi.sizeof(array) / ffi.sizeof('double'), + 'Upper bound must be lower than array size') if high - low < 1 then return array end diff --git a/test/quantile_test.lua b/test/quantile_test.lua index 00dcc2c2..861b1325 100644 --- a/test/quantile_test.lua +++ b/test/quantile_test.lua @@ -109,6 +109,17 @@ g.test_low_bound_negative = function() ) end +g.test_high_bound_higher_array_size = function() + local empty = ffi.new('double[?]', 2) + t.assert_error_msg_contains( + 'Upper bound must be lower than array size', + quantile.quicksort, + empty, + 1, + 10 + ) +end + g.test_not_sorted = function() local array = ffi.new('double[?]', 2) array[0] = math.huge