From 2c9a672ce9cfbe48f9ef70dfc908c28824581fb5 Mon Sep 17 00:00:00 2001 From: Jacob Christie <19879272+jjatie@users.noreply.github.com> Date: Tue, 9 Feb 2021 17:08:39 -0400 Subject: [PATCH] Fix bounds checks on binary search (#4577) --- .../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]