From 2e9adcc8086fe88c077d3a54690ae506eb86b5fc Mon Sep 17 00:00:00 2001 From: Jacob Christie <19879272+jjatie@users.noreply.github.com> Date: Sun, 7 Feb 2021 00:09:14 -0400 Subject: [PATCH] Fix bounds checks on binary search --- .../Charts/Data/Implementations/Standard/ChartDataSet.swift | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/Source/Charts/Data/Implementations/Standard/ChartDataSet.swift b/Source/Charts/Data/Implementations/Standard/ChartDataSet.swift index e898682de0..703c0abe72 100644 --- a/Source/Charts/Data/Implementations/Standard/ChartDataSet.swift +++ b/Source/Charts/Data/Implementations/Standard/ChartDataSet.swift @@ -214,7 +214,7 @@ open class ChartDataSet: ChartBaseDataSet rounding: ChartDataSetRounding) -> Int { var closest = partitioningIndex { $0.x >= xValue } - guard closest < endIndex else { return closest } + guard closest < endIndex else { return -1 } let closestXValue = self[closest].x @@ -237,8 +237,6 @@ open class ChartDataSet: ChartBaseDataSet break } - guard closest < endIndex else { return endIndex } - // Search by closest to y-value if !yValue.isNaN { @@ -250,7 +248,7 @@ open class ChartDataSet: ChartBaseDataSet var closestYValue = self[closest].y var closestYIndex = closest - while closest < endIndex + while closest < index(before: endIndex) { formIndex(after: &closest) let value = self[closest]