eclipse.platform.swt icon indicating copy to clipboard operation
eclipse.platform.swt copied to clipboard

StyledText: possible AIOOBE in several places with set line spacing

Open tmssngr opened this issue 6 months ago • 1 comments
trafficstars

Describe the bug After having set a line spacing to a StyledText (styledText.setLineSpacing(10)), at different positions in StyledTextRenderer AIOOBE may occur, e.g.:

java.lang.ArrayIndexOutOfBoundsException: Index 7 out of bounds for length 7
	at org.eclipse.swt.custom.StyledTextRenderer.getLineSize(StyledTextRenderer.java:320)
	at org.eclipse.swt.custom.StyledTextRenderer.getLineHeight(StyledTextRenderer.java:752)
	at org.eclipse.swt.custom.StyledTextRenderer.getLineHeight(StyledTextRenderer.java:749)
	at org.eclipse.swt.custom.StyledText.getLineIndex(StyledText.java:3920)
	at org.eclipse.swt.custom.StyledText.getPartialBottomIndex(StyledText.java:4236)
	at org.eclipse.swt.custom.StyledText.getOffsetAtPoint(StyledText.java:4191)
	at org.eclipse.swt.custom.StyledText.getOffsetAtPoint(StyledText.java:4140)
	at org.eclipse.swt.custom.StyledText.setCaretLocations(StyledText.java:8486)
	at org.eclipse.swt.custom.StyledText.setCaretLocations(StyledText.java:8443)
	at org.eclipse.swt.custom.StyledText.setCaret(StyledText.java:8394)
... (our code)
	at org.eclipse.swt.widgets.EventTable.sendEvent(EventTable.java:91)
	at org.eclipse.swt.widgets.Display.sendEvent(Display.java:4344)
	at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1208)
	at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1232)
	at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1217)
	at org.eclipse.swt.widgets.Widget.notifyListeners(Widget.java:854)
	at org.eclipse.swt.custom.StyledText.setCaretOffsets(StyledText.java:8603)
	at org.eclipse.swt.custom.StyledText.reset(StyledText.java:7791)
	at org.eclipse.swt.custom.StyledText.handleTextSet(StyledText.java:6157)
	at org.eclipse.swt.custom.StyledText$5.textSet(StyledText.java:5410)
	at org.eclipse.swt.custom.StyledTextListener.handleEvent(StyledTextListener.java:84)
	at org.eclipse.swt.custom.DefaultContent.sendTextEvent(DefaultContent.java:820)
	at org.eclipse.swt.custom.DefaultContent.setText(DefaultContent.java:840)
	at org.eclipse.swt.custom.StyledText.setText(StyledText.java:10497)
...

Debugging revealed that in https://github.com/eclipse-platform/eclipse.platform.swt/blob/4df99baddc2949c5b749a82d6a02cea3ff579524/bundles/org.eclipse.swt/Eclipse%20SWT%20Custom%20Widgets/common/org/eclipse/swt/custom/StyledText.java#L3920 the values are line = 7 lineCount = 2469 lineHeight = 35 y = 570 It tries to access line numbers > 6 though the lineSizes array in StyledTextRenderer just has a size of 7.

I've also had AIOOBE in other methods of StyledTextRenderer

To Reproduce That's the hard point. I can only reproduce this in SmartGit by selecting a certain commit.

  • run SmartGit 25.1 preview (build #251042), Log or Standard window
  • Edit | Preferences > Low-Level Properties, search for styledText.lineSpacing and set it to 10 (this will cause a styledText.setLineSpacing(10))
  • restart SmartGit (so the line spacing is applied
  • clone repository https://github.com/swt-initiative31/prototype-skija.git, switch to branch styledtext-linespacing-bug
  • select commit 4ea0024b in the graph

Expected behavior No AIOOBE.

Environment:

  1. Select the platform(s) on which the behavior is seen:
    • [ ] All OS
    • [x] Windows
    • [ ] Linux
    • [ ] macOS
  1. Additional OS info (e.g. OS version, Linux Desktop, etc) Windows 11, 150% zoom

tmssngr avatar May 09 '25 09:05 tmssngr

This looks like StyledText.reset indirectly causes the ST.CaretMoved event being sent before all the parts of the StyledText are already configured correctly. When delaying styledText.setCaret (to show or hide the caret depending on the line) in our ST.CaretMoved listener using display.asyncExec, this problem can be avoided.

tmssngr avatar May 09 '25 09:05 tmssngr