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

Bug 339614

Summary: [search] JavaElementLine crashes with ArrayIndexOutOfBoundsException when there is EOF encountered
Product: [Eclipse Project] JDT Reporter: Andrew Eisenberg <andrew.eisenberg>
Component: UIAssignee: Markus Keller <markus.kell.r>
Status: RESOLVED FIXED QA Contact:
Severity: normal    
Priority: P3 CC: juan_r958, markus.kell.r
Version: 3.6.2   
Target Milestone: 3.7 M7   
Hardware: All   
OS: All   
Whiteboard:

Description Andrew Eisenberg CLA 2011-03-10 17:46:28 EST
The constructor org.eclipse.jdt.internal.ui.search.JavaElementLine.JavaElementLine(ITypeRoot, int, int) expects that the end of the line ends with a \n or a \r char.  However, there are situations where an EOF is reached with no \r or \n.

The only time I have encountered this in Java code is with a syntax error.  See this class:

class Foo {
int x

Now, select the x field and do CTRL-Shift-U.  And the result is an exception in the log:

eclipse.buildId=M20110210-1200
java.version=1.6.0_22
java.vendor=Apple Inc.
BootLoader constants: OS=macosx, ARCH=x86, WS=cocoa, NL=en_US
Framework arguments:  -product com.springsource.sts.ide
Command-line arguments:  -os macosx -ws cocoa -arch x86 -product com.springsource.sts.ide -data /users/Andrew/Eclipse/Workspaces/workspaceAJDT36


Error
Thu Mar 10 14:42:35 PST 2011
An internal error occurred during: "Search for Occurrences in File".

java.lang.ArrayIndexOutOfBoundsException
	at org.eclipse.jdt.internal.ui.javaeditor.DocumentAdapter.getChar(DocumentAdapter.java:369)
	at org.eclipse.jdt.internal.ui.search.JavaElementLine.<init>(JavaElementLine.java:70)
	at org.eclipse.jdt.internal.ui.search.OccurrencesSearchQuery.getLineElement(OccurrencesSearchQuery.java:113)
	at org.eclipse.jdt.internal.ui.search.OccurrencesSearchQuery.run(OccurrencesSearchQuery.java:79)
	at org.eclipse.search2.internal.ui.InternalSearchUI$InternalSearchJob.run(InternalSearchUI.java:91)
	at org.eclipse.core.internal.jobs.Worker.run(Worker.java:54)



The fix is simple.  In the condition for the while loop on line 64, add a check for EOF:
while (i < length && !IndentManipulation.isLineDelimiterChar(ch) && !IndentManipulation.isEOFChar(ch)) {
Comment 1 Markus Keller CLA 2011-03-11 09:41:35 EST
Fixed in HEAD of JavaElementLine (was not as simple as suggested in comment 0 ;-).
Comment 2 Andrew Eisenberg CLA 2011-03-11 11:24:43 EST
Thanks for fixing so fast.
Comment 3 Dani Megert CLA 2011-04-07 02:09:31 EDT
*** Bug 341717 has been marked as a duplicate of this bug. ***