Skip to content

Commit 2095776

Browse files
committed
prevent ArrayIndexOutOfBoundsException in #findInsertionBoundary() (issue #52)
1 parent caaf6af commit 2095776

File tree

1 file changed

+28
-6
lines changed

1 file changed

+28
-6
lines changed

src/main/java/com/tagtraum/perf/gcviewer/renderer/PolygonChartRenderer.java

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -145,10 +145,14 @@ private InsertionBoundary findInsertionBoundary(Polygon polygon, int xMin, int x
145145
// if the resulting point is not between two points in the array, make sure that the range
146146
// is extended until the insertion point is found
147147
while (polygon.xpoints[insertionBoundary.getStartX()] > xMin) {
148-
insertionBoundary.decreaseStartX();
148+
if (!insertionBoundary.decreaseStartX()) {
149+
break;
150+
}
149151
}
150152
while (polygon.xpoints[insertionBoundary.getEndX()] < xMax) {
151-
insertionBoundary.increaseEndX();
153+
if (!insertionBoundary.increaseEndX()) {
154+
break;
155+
}
152156
}
153157

154158
return insertionBoundary;
@@ -308,12 +312,30 @@ public InsertionBoundary(int arrayLength) {
308312
this.maxX = arrayLength-1;
309313
}
310314

311-
public void decreaseStartX() {
312-
--startX;
315+
/**
316+
* Decreases start index by one
317+
* @return <code>true</code> if current start index was &gt;0 before, <code>false</code> otherwise
318+
*/
319+
public boolean decreaseStartX() {
320+
if (startX > 0) {
321+
--startX;
322+
return true;
323+
}
324+
325+
return false;
313326
}
314327

315-
public void increaseEndX() {
316-
++endX;
328+
/**
329+
* Increases end index by one
330+
* @return <code>true</code> if current end index was &lt; max value before, <code>false</code> otherwise
331+
*/
332+
public boolean increaseEndX() {
333+
if (endX < maxX) {
334+
++endX;
335+
return true;
336+
}
337+
338+
return false;
317339
}
318340

319341
public int getDistance() {

0 commit comments

Comments
 (0)