From 34cae27bff9813a73c643c1f8ef27a783264f438 Mon Sep 17 00:00:00 2001 From: Bhagya Rajangam Date: Thu, 19 Sep 2024 12:34:48 +0530 Subject: [PATCH] Added safe index subscript logic to avoid crash --- Source/Charts/Data/Implementations/Standard/ChartData.swift | 4 ++++ .../Charts/Data/Implementations/Standard/RadarChartData.swift | 2 +- Source/Charts/Renderers/BarChartRenderer.swift | 2 +- Source/Charts/Renderers/BubbleChartRenderer.swift | 2 +- Source/Charts/Renderers/CandleStickChartRenderer.swift | 2 +- Source/Charts/Renderers/LineChartRenderer.swift | 2 +- Source/Charts/Renderers/PieChartRenderer.swift | 2 +- Source/Charts/Renderers/RadarChartRenderer.swift | 2 +- Source/Charts/Renderers/ScatterChartRenderer.swift | 2 +- 9 files changed, 12 insertions(+), 8 deletions(-) diff --git a/Source/Charts/Data/Implementations/Standard/ChartData.swift b/Source/Charts/Data/Implementations/Standard/ChartData.swift index 81fc37b3a8..8481f25657 100644 --- a/Source/Charts/Data/Implementations/Standard/ChartData.swift +++ b/Source/Charts/Data/Implementations/Standard/ChartData.swift @@ -452,6 +452,10 @@ extension ChartData: MutableCollection get { return dataSets[position] } set { self._dataSets[position] = newValue } } + + public subscript(safe index: Index) -> Element? { + return indices.contains(index) ? self[index] : nil + } } // MARK: RandomAccessCollection diff --git a/Source/Charts/Data/Implementations/Standard/RadarChartData.swift b/Source/Charts/Data/Implementations/Standard/RadarChartData.swift index 2f8a2db7ae..6313408ae2 100644 --- a/Source/Charts/Data/Implementations/Standard/RadarChartData.swift +++ b/Source/Charts/Data/Implementations/Standard/RadarChartData.swift @@ -46,6 +46,6 @@ open class RadarChartData: ChartData @objc open override func entry(for highlight: Highlight) -> ChartDataEntry? { - return self[highlight.dataSetIndex].entryForIndex(Int(highlight.x)) + return self[safe: highlight.dataSetIndex]?.entryForIndex(Int(highlight.x)) } } diff --git a/Source/Charts/Renderers/BarChartRenderer.swift b/Source/Charts/Renderers/BarChartRenderer.swift index 2aa69a828f..eb9878fec6 100644 --- a/Source/Charts/Renderers/BarChartRenderer.swift +++ b/Source/Charts/Renderers/BarChartRenderer.swift @@ -701,7 +701,7 @@ open class BarChartRenderer: BarLineScatterCandleBubbleRenderer for high in indices { guard - let set = barData[high.dataSetIndex] as? BarChartDataSetProtocol, + let set = barData[safe: high.dataSetIndex] as? BarChartDataSetProtocol, set.isHighlightEnabled else { continue } diff --git a/Source/Charts/Renderers/BubbleChartRenderer.swift b/Source/Charts/Renderers/BubbleChartRenderer.swift index fa96a6f91e..60ec7d442a 100644 --- a/Source/Charts/Renderers/BubbleChartRenderer.swift +++ b/Source/Charts/Renderers/BubbleChartRenderer.swift @@ -252,7 +252,7 @@ open class BubbleChartRenderer: BarLineScatterCandleBubbleRenderer for high in indices { guard - let dataSet = bubbleData[high.dataSetIndex] as? BubbleChartDataSetProtocol, + let dataSet = bubbleData[safe: high.dataSetIndex] as? BubbleChartDataSetProtocol, dataSet.isHighlightEnabled, let entry = dataSet.entryForXValue(high.x, closestToY: high.y) as? BubbleChartDataEntry, isInBoundsX(entry: entry, dataSet: dataSet) diff --git a/Source/Charts/Renderers/CandleStickChartRenderer.swift b/Source/Charts/Renderers/CandleStickChartRenderer.swift index 40f80efb52..36b8c2b9e0 100644 --- a/Source/Charts/Renderers/CandleStickChartRenderer.swift +++ b/Source/Charts/Renderers/CandleStickChartRenderer.swift @@ -362,7 +362,7 @@ open class CandleStickChartRenderer: LineScatterCandleRadarRenderer for high in indices { guard - let set = candleData[high.dataSetIndex] as? CandleChartDataSetProtocol, + let set = candleData[safe: high.dataSetIndex] as? CandleChartDataSetProtocol, set.isHighlightEnabled else { continue } diff --git a/Source/Charts/Renderers/LineChartRenderer.swift b/Source/Charts/Renderers/LineChartRenderer.swift index ec622967ee..33cd05fad4 100644 --- a/Source/Charts/Renderers/LineChartRenderer.swift +++ b/Source/Charts/Renderers/LineChartRenderer.swift @@ -760,7 +760,7 @@ open class LineChartRenderer: LineRadarRenderer for high in indices { - guard let set = lineData[high.dataSetIndex] as? LineChartDataSetProtocol, + guard let set = lineData[safe: high.dataSetIndex] as? LineChartDataSetProtocol, set.isHighlightEnabled else { continue } diff --git a/Source/Charts/Renderers/PieChartRenderer.swift b/Source/Charts/Renderers/PieChartRenderer.swift index 18f571bc79..afb44b642e 100644 --- a/Source/Charts/Renderers/PieChartRenderer.swift +++ b/Source/Charts/Renderers/PieChartRenderer.swift @@ -727,7 +727,7 @@ open class PieChartRenderer: NSObject, DataRenderer // get the index to highlight let index = Int(hightlight.x) guard index < drawAngles.count, - let set = data[hightlight.dataSetIndex] as? PieChartDataSetProtocol, + let set = data[safe: hightlight.dataSetIndex] as? PieChartDataSetProtocol, set.isHighlightEnabled else { diff --git a/Source/Charts/Renderers/RadarChartRenderer.swift b/Source/Charts/Renderers/RadarChartRenderer.swift index 21f1246049..bbba4bee0d 100644 --- a/Source/Charts/Renderers/RadarChartRenderer.swift +++ b/Source/Charts/Renderers/RadarChartRenderer.swift @@ -354,7 +354,7 @@ open class RadarChartRenderer: LineRadarRenderer for high in indices { guard - let set = chart.data?[high.dataSetIndex] as? RadarChartDataSetProtocol, + let set = chart.data?[safe: high.dataSetIndex] as? RadarChartDataSetProtocol, set.isHighlightEnabled else { continue } diff --git a/Source/Charts/Renderers/ScatterChartRenderer.swift b/Source/Charts/Renderers/ScatterChartRenderer.swift index 9fb1f560c7..d27a7a81ba 100644 --- a/Source/Charts/Renderers/ScatterChartRenderer.swift +++ b/Source/Charts/Renderers/ScatterChartRenderer.swift @@ -203,7 +203,7 @@ open class ScatterChartRenderer: LineScatterCandleRadarRenderer for high in indices { guard - let set = scatterData[high.dataSetIndex] as? ScatterChartDataSetProtocol, + let set = scatterData[safe: high.dataSetIndex] as? ScatterChartDataSetProtocol, set.isHighlightEnabled else { continue }