From 055c002239c2e5bf32516f94146a76ec743717e6 Mon Sep 17 00:00:00 2001 From: Christopher Hermann Date: Fri, 31 Jan 2025 10:35:01 +0100 Subject: [PATCH] [StickyScrolling] Prevent out of bounds errors by validating sticky line number Handle cases where the sticky line number exceeds the total number of lines to prevent out of bounds errors. Since widgetLineNumber is 0-based, ensure it is not equal to or greater than the line count. Fixes #2765 --- .../ui/internal/texteditor/stickyscroll/StickyLine.java | 2 +- .../texteditor/stickyscroll/StickyScrollingControl.java | 2 +- .../ui/internal/texteditor/stickyscroll/StickyLineTest.java | 5 +++-- .../texteditor/stickyscroll/StickyScrollingControlTest.java | 3 ++- 4 files changed, 7 insertions(+), 5 deletions(-) diff --git a/bundles/org.eclipse.ui.editors/src/org/eclipse/ui/internal/texteditor/stickyscroll/StickyLine.java b/bundles/org.eclipse.ui.editors/src/org/eclipse/ui/internal/texteditor/stickyscroll/StickyLine.java index b62bae32287..9b82b5ab02e 100644 --- a/bundles/org.eclipse.ui.editors/src/org/eclipse/ui/internal/texteditor/stickyscroll/StickyLine.java +++ b/bundles/org.eclipse.ui.editors/src/org/eclipse/ui/internal/texteditor/stickyscroll/StickyLine.java @@ -55,7 +55,7 @@ public StyleRange[] getStyleRanges() { StyledText textWidget= sourceViewer.getTextWidget(); int widgetLineNumber = getWidgetLineNumber(); - if (widgetLineNumber > textWidget.getLineCount()) { + if (widgetLineNumber >= textWidget.getLineCount()) { return null; } diff --git a/bundles/org.eclipse.ui.editors/src/org/eclipse/ui/internal/texteditor/stickyscroll/StickyScrollingControl.java b/bundles/org.eclipse.ui.editors/src/org/eclipse/ui/internal/texteditor/stickyscroll/StickyScrollingControl.java index b79afafa243..3da1b67caf4 100644 --- a/bundles/org.eclipse.ui.editors/src/org/eclipse/ui/internal/texteditor/stickyscroll/StickyScrollingControl.java +++ b/bundles/org.eclipse.ui.editors/src/org/eclipse/ui/internal/texteditor/stickyscroll/StickyScrollingControl.java @@ -441,7 +441,7 @@ public void controlMoved(ControlEvent e) { private boolean areStickyLinesOutDated(StyledText textWidget) { if (stickyLines.size() > 0) { int lastStickyLineNumber = stickyLines.get(stickyLines.size() - 1).getLineNumber(); - return lastStickyLineNumber > textWidget.getLineCount(); + return lastStickyLineNumber >= textWidget.getLineCount(); } return false; } diff --git a/tests/org.eclipse.ui.editors.tests/src/org/eclipse/ui/internal/texteditor/stickyscroll/StickyLineTest.java b/tests/org.eclipse.ui.editors.tests/src/org/eclipse/ui/internal/texteditor/stickyscroll/StickyLineTest.java index 5c34d24f0db..aeace6c47c2 100644 --- a/tests/org.eclipse.ui.editors.tests/src/org/eclipse/ui/internal/texteditor/stickyscroll/StickyLineTest.java +++ b/tests/org.eclipse.ui.editors.tests/src/org/eclipse/ui/internal/texteditor/stickyscroll/StickyLineTest.java @@ -100,9 +100,10 @@ public void testGetStyleRangesIgnoresOutOfBoundLines() { textWidget.setText("line1\nline2\nline3"); StickyLine stickyLineOutOfBound = new StickyLine(10, sourceViewer); - StyleRange[] styleRanges = stickyLineOutOfBound.getStyleRanges(); + assertNull(stickyLineOutOfBound.getStyleRanges()); - assertNull(styleRanges); + stickyLineOutOfBound = new StickyLine(3, sourceViewer); + assertNull(stickyLineOutOfBound.getStyleRanges()); } @Test diff --git a/tests/org.eclipse.ui.editors.tests/src/org/eclipse/ui/internal/texteditor/stickyscroll/StickyScrollingControlTest.java b/tests/org.eclipse.ui.editors.tests/src/org/eclipse/ui/internal/texteditor/stickyscroll/StickyScrollingControlTest.java index 04fdfd18012..c44151f879f 100644 --- a/tests/org.eclipse.ui.editors.tests/src/org/eclipse/ui/internal/texteditor/stickyscroll/StickyScrollingControlTest.java +++ b/tests/org.eclipse.ui.editors.tests/src/org/eclipse/ui/internal/texteditor/stickyscroll/StickyScrollingControlTest.java @@ -256,7 +256,7 @@ public void testLayoutStickyLinesCanvasOnResize() { public void testDontLayoutOutDatedStickyLinesOnResize() { sourceViewer.getTextWidget().setBounds(0, 0, 200, 200); - List stickyLines = List.of(new StickyLineStub("line 10", 10)); + List stickyLines = List.of(new StickyLineStub("line 1", 1)); stickyScrollingControl.setStickyLines(stickyLines); Canvas stickyControlCanvas = getStickyControlCanvas(shell); @@ -329,6 +329,7 @@ public void testHorizontalScrollingIsDispatched() { @Test public void testLimitStickyLinesToTextWidgetHeight() { sourceViewer.getTextWidget().setBounds(0, 0, 200, 200); + sourceViewer.getTextWidget().setText("line1\nline2"); List stickyLines = List.of(new StickyLineStub("line 2", 1)); stickyScrollingControl.setStickyLines(stickyLines);