Skip to content

Commit bf30d44

Browse files
committed
Added null checks to getEntryIndex
Optimized mid-point formula in get EntryIndex to prevent potential int overflow
1 parent 6c9fcc7 commit bf30d44

File tree

1 file changed

+69
-54
lines changed
  • MPChartLib/src/main/java/com/github/mikephil/charting/data

1 file changed

+69
-54
lines changed

MPChartLib/src/main/java/com/github/mikephil/charting/data/DataSet.java

Lines changed: 69 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -331,77 +331,92 @@ public int getEntryIndex(float xValue, float closestToY, Rounding rounding) {
331331
int closest = high;
332332

333333
while (low < high) {
334-
int m = (low + high) / 2;
335-
336-
final float d1 = mEntries.get(m).getX() - xValue,
337-
d2 = mEntries.get(m + 1).getX() - xValue,
338-
ad1 = Math.abs(d1), ad2 = Math.abs(d2);
339-
340-
if (ad2 < ad1) {
341-
// [m + 1] is closer to xValue
342-
// Search in an higher place
343-
low = m + 1;
344-
} else if (ad1 < ad2) {
345-
// [m] is closer to xValue
346-
// Search in a lower place
347-
high = m;
348-
} else {
349-
// We have multiple sequential x-value with same distance
334+
int m = low + (high - low) / 2;
335+
336+
Entry currentEntry = mEntries.get(m);
337+
338+
if (currentEntry != null) {
339+
Entry nextEntry = mEntries.get(m + 1);
340+
341+
if (nextEntry != null) {
342+
final float d1 = currentEntry.getX() - xValue,
343+
d2 = nextEntry.getX() - xValue,
344+
ad1 = Math.abs(d1),
345+
ad2 = Math.abs(d2);
346+
347+
if (ad2 < ad1) {
348+
// [m + 1] is closer to xValue
349+
// Search in an higher place
350+
low = m + 1;
351+
} else if (ad1 < ad2) {
352+
// [m] is closer to xValue
353+
// Search in a lower place
354+
high = m;
355+
} else {
356+
// We have multiple sequential x-value with same distance
357+
358+
if (d1 >= 0.0) {
359+
// Search in a lower place
360+
high = m;
361+
} else if (d1 < 0.0) {
362+
// Search in an higher place
363+
low = m + 1;
364+
}
365+
}
350366

351-
if (d1 >= 0.0) {
352-
// Search in a lower place
353-
high = m;
354-
} else if (d1 < 0.0) {
355-
// Search in an higher place
356-
low = m + 1;
367+
closest = high;
357368
}
358369
}
359-
360-
closest = high;
361370
}
362371

363372
if (closest != -1) {
364-
float closestXValue = mEntries.get(closest).getX();
365-
if (rounding == Rounding.UP) {
366-
// If rounding up, and found x-value is lower than specified x, and we can go upper...
367-
if (closestXValue < xValue && closest < mEntries.size() - 1) {
368-
++closest;
369-
}
370-
} else if (rounding == Rounding.DOWN) {
371-
// If rounding down, and found x-value is upper than specified x, and we can go lower...
372-
if (closestXValue > xValue && closest > 0) {
373-
--closest;
373+
Entry closestEntry = mEntries.get(closest);
374+
if (closestEntry != null) {
375+
float closestXValue = closestEntry.getX();
376+
if (rounding == Rounding.UP) {
377+
// If rounding up, and found x-value is lower than specified x, and we can go upper...
378+
if (closestXValue < xValue && closest < mEntries.size() - 1) {
379+
++closest;
380+
}
381+
} else if (rounding == Rounding.DOWN) {
382+
// If rounding down, and found x-value is upper than specified x, and we can go lower...
383+
if (closestXValue > xValue && closest > 0) {
384+
--closest;
385+
}
374386
}
375-
}
376387

377-
// Search by closest to y-value
378-
if (!Float.isNaN(closestToY)) {
379-
while (closest > 0 && mEntries.get(closest - 1).getX() == closestXValue)
380-
closest -= 1;
388+
// Search by closest to y-value
389+
if (!Float.isNaN(closestToY)) {
390+
while (closest > 0 && mEntries.get(closest - 1).getX() == closestXValue)
391+
closest -= 1;
381392

382-
float closestYValue = mEntries.get(closest).getY();
383-
int closestYIndex = closest;
393+
float closestYValue = closestEntry.getY();
394+
int closestYIndex = closest;
384395

385-
while (true) {
386-
closest += 1;
387-
if (closest >= mEntries.size())
388-
break;
396+
while (true) {
397+
closest += 1;
398+
if (closest >= mEntries.size())
399+
break;
389400

390-
final Entry value = mEntries.get(closest);
401+
final Entry value = mEntries.get(closest);
391402

392-
if (value.getX() != closestXValue)
393-
break;
403+
if (value == null) {
404+
continue;
405+
}
406+
407+
if (value.getX() != closestXValue)
408+
break;
394409

395-
if (Math.abs(value.getY() - closestToY) <= Math.abs(closestYValue - closestToY)) {
396-
closestYValue = closestToY;
397-
closestYIndex = closest;
410+
if (Math.abs(value.getY() - closestToY) <= Math.abs(closestYValue - closestToY)) {
411+
closestYValue = closestToY;
412+
closestYIndex = closest;
413+
}
398414
}
399-
}
400415

401-
closest = closestYIndex;
416+
closest = closestYIndex;
417+
}
402418
}
403419
}
404-
405420
return closest;
406421
}
407422

0 commit comments

Comments
 (0)