Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.

Bug 99299

Summary: [StyledText] DefaultContent.getLine(DefaultContent.java:489) - java.lang.StringIndexOutOfBoundsException
Product: [Eclipse Project] Platform Reporter: Rutger Ovidius <ovidr>
Component: SWTAssignee: Platform-SWT-Inbox <platform-swt-inbox>
Status: CLOSED WONTFIX QA Contact: Felipe Heidrich <eclipse.felipe>
Severity: normal    
Priority: P3 CC: eclipse.felipe, james.willans, mlists, n.a.edgar, snorthov
Version: 3.1Keywords: triaged
Target Milestone: ---   
Hardware: PC   
OS: Windows XP   
Whiteboard: stalebug
Attachments:
Description Flags
testcase
none
Proposed patch in DefaultContent.java none

Description Rutger Ovidius CLA 2005-06-09 23:43:44 EDT
WIN32 - SWT 3.1M4 and 3.1RC1

Resizing a composite with a StyledText contained within will ocassionally result
in the following crash:
(line # might be off as I've added debugging printlns).

java.lang.StringIndexOutOfBoundsException: String index out of range: -927
     at java.lang.String.<init>(Unknown Source)
     at org.eclipse.swt.custom.DefaultContent.getLine(DefaultContent.java:599)
     at
org.eclipse.swt.custom.StyledText$ContentWidthCache.calculate(StyledText.java:1219)
     at org.eclipse.swt.custom.StyledText.computeSize(StyledText.java:2102)
     at org.eclipse.swt.layout.GridData.computeSize(GridData.java:450)
     at org.eclipse.swt.layout.GridLayout.layout(GridLayout.java:197)
     at org.eclipse.swt.layout.GridLayout.computeSize(GridLayout.java:155)
     at org.eclipse.swt.widgets.Composite.computeSize(Composite.java:207)
     at org.eclipse.swt.layout.FillLayout.computeSize(FillLayout.java:110)
     at org.eclipse.swt.widgets.Composite.computeSize(Composite.java:207)
     at org.eclipse.swt.widgets.Control.computeSize(Control.java:382)

Quick observation:

The 'gap' length in DefaultContent is positive when calculated by 'gapEnd -
gapStart', but
lines[2][1] has been set negative in moveAndResizeGap() which is called on
insert()s and delete()s.

344: lines[gapLine][1] = lines[gapLine][1] - oldSize;

getLine is using this negative value to construct a String.

482: int length = lines[index][1];

which is the called from the StyledText$ContentWidthCache.calculate() call
during a resize.

Attached is a testcase that appends some lines of text to a styledText and
manually calls getLine(2), and crashes in a similar manner.

Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String
index out of range: -33673
  at java.lang.String.<init>(Unknown Source)
  at org.eclipse.swt.custom.DefaultContent.getLine(DefaultContent.java:489)
  at STC.appendLine(STC.java:61)
  at STC.main(STC.java:44)
Comment 1 Rutger Ovidius CLA 2005-06-09 23:45:59 EDT
Created attachment 22778 [details]
testcase
Comment 2 Steve Northover CLA 2005-06-15 13:44:27 EDT
FH, is this a candidate for RC3?
Comment 3 Felipe Heidrich CLA 2005-06-15 13:59:34 EDT
I don't think we should fix this for 3.1.
Comment 4 Steve Northover CLA 2005-06-15 14:03:53 EDT
Really?  An SIOOBE?
Comment 5 Nick Edgar CLA 2005-06-15 14:36:00 EDT
How rude!
Comment 6 Rutger Ovidius CLA 2005-10-15 12:47:26 EDT
I also receive the following stacktrace with 3.2M2 when using the scrollbar on 
a StyledText widget. Looks to be the same getLine problem. (This is a really 
bad bug that comes up a lot for me).

java.lang.StringIndexOutOfBoundsException: String index out of range: -427
	at java.lang.String.<init>(Unknown Source)
	at org.eclipse.swt.custom.DefaultContent.getLine
(DefaultContent.java:487)
	at org.eclipse.swt.custom.StyledText$ContentWidthCache.calculate
(StyledText.java:1251)
	at org.eclipse.swt.custom.StyledText.calculateTopIndex
(StyledText.java:2002)
	at org.eclipse.swt.custom.StyledText.setVerticalScrollOffset
(StyledText.java:7701)
	at org.eclipse.swt.custom.StyledText.handleVerticalScroll
(StyledText.java:5443)
	at org.eclipse.swt.custom.StyledText$8.handleEvent(StyledText.java:4874)
	at org.eclipse.swt.widgets.EventTable.sendEvent(EventTable.java:66)
	at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:884)
	at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:908)
	at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:893)
	at org.eclipse.swt.widgets.ScrollBar.wmScrollChild(ScrollBar.java:902)
	at org.eclipse.swt.widgets.Scrollable.wmScroll(Scrollable.java:394)
	at org.eclipse.swt.widgets.Scrollable.WM_VSCROLL(Scrollable.java:341)
	at org.eclipse.swt.widgets.Control.windowProc(Control.java:3137)
	at org.eclipse.swt.widgets.Display.windowProc(Display.java:3854)
	at org.eclipse.swt.internal.win32.OS.DispatchMessageW(Native Method)
	at org.eclipse.swt.internal.win32.OS.DispatchMessage(OS.java:1693)
	at org.eclipse.swt.widgets.Display.readAndDispatch(Display.java:2839)
	at sancho.view.MainWindow.<init>(MainWindow.java:186)
Comment 7 Steve Northover CLA 2005-10-24 14:39:20 EDT
FH, any idea about this one?  Seems bad.
Comment 8 Rutger Ovidius CLA 2006-04-13 13:49:54 EDT
With latest CVS (swt 3230) I receive the following different stacktrace.  Any chance this will be looked at for 3.2?


Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException
	at java.lang.System.arraycopy(Native Method)
	at java.lang.AbstractStringBuilder.append(Unknown Source)
	at java.lang.StringBuffer.append(Unknown Source)
	at org.eclipse.swt.custom.DefaultContent.getLine(DefaultContent.java:493)
	at org.eclipse.swt.custom.StyledTextRenderer.getHeight(StyledTextRenderer.java:496)
	at org.eclipse.swt.custom.StyledText.setScrollBars(StyledText.java:7350)
	at org.eclipse.swt.custom.StyledText.resetCache(StyledText.java:6307)
	at org.eclipse.swt.custom.StyledText.handleTextChanged(StyledText.java:5292)
	at org.eclipse.swt.custom.StyledText$6.textChanged(StyledText.java:4785)
	at org.eclipse.swt.custom.StyledTextListener.handleEvent(StyledTextListener.java:66)
	at org.eclipse.swt.custom.DefaultContent.sendTextEvent(DefaultContent.java:794)
	at org.eclipse.swt.custom.DefaultContent.replaceTextRange(DefaultContent.java:787)
	at org.eclipse.swt.custom.StyledText.modifyContent(StyledText.java:5763)
	at org.eclipse.swt.custom.StyledText.replaceTextRange(StyledText.java:6278)
	at STC.appendLine(STC.java:53)
	at STC.main(STC.java:46)
Comment 9 Steve Northover CLA 2006-04-13 13:56:22 EDT
FH, still a problem?
Comment 10 Felipe Heidrich CLA 2006-04-13 15:02:01 EDT
Yes, it is still a problem. 

This part of the code is pretty complicated and I never touch it, I had the fix once but I decided not to release cause I was afraid of breaking eclipse. BTW, this doesn't happen on Eclipse.
Comment 11 Felipe Heidrich CLA 2006-04-18 14:26:52 EDT
It was really hard to find this one. The testcase would only show the bug in the 16th iteration when the line count is over one thousand and the char count over hundred thousand. The problem actualy happens in the 2nd iteration.

Simple test case:
s.setText("abc\r");
s.append("\ndef");
At this point lineCount == 3, it should be 2, the text really is:
line0: "abc\r\n"
line1: "def"

Or,
s.setText("abc\rX\ndef");
s.replaceTextRange(4, 1, "");
At this point lineCount 3, it should be 2. The result text is same as the first case.


Still, I don't have a elegant fix for it.
Comment 12 Felipe Heidrich CLA 2009-08-19 12:24:01 EDT
Your bug has been moved to triage, visit http://www.eclipse.org/swt/triage.php for more info.
Comment 13 Patrick Tasse CLA 2010-11-24 16:58:04 EST
Created attachment 183807 [details]
Proposed patch in DefaultContent.java

Proposing a patch that solves the following three cases:

1) creating a CR+LF by inserting a LF after a CR
2) creating a CR+LF by inserting a CR before a LF
3) creating a CR+LF by deleting text between a CR and a LF

Legal Message: I, Patrick Tassé, declare that I developed attached code from
scratch, without referencing any 3rd party materials except material licensed
under the EPL. I am authorized by my employer to make this contribution under
the EPL.
Comment 14 Leo Ufimtsev CLA 2017-08-03 12:31:16 EDT
This is a one-off bulk update. (The last one in the triage migration).

Moving bugs from swt-triaged@eclipse to platform-swt-inbox@eclipse.org and adding "triaged" keyword as per new triage process:
https://wiki.eclipse.org/SWT/Devel/Triage

See Bug 518478 for details.

Tag for notification/mail filters:
@TriageBulkUpdate
Comment 15 Eclipse Genie CLA 2019-09-24 13:17:37 EDT
This bug hasn't had any activity in quite some time. Maybe the problem got resolved, was a duplicate of something else, or became less pressing for some reason - or maybe it's still relevant but just hasn't been looked at yet.

If you have further information on the current state of the bug, please add it. The information can be, for example, that the problem still occurs, that you still want the feature, that more information is needed, or that the bug is (for whatever reason) no longer relevant.

--
The automated Eclipse Genie.