Skip to content

Commit 8ffab2f

Browse files
committed
Prevent SIOOBE in JSHint#fixPosition
JSHint#fixPosition was missing a safeguard for the case that the error position reported by JSHint exceeds the input string. Fixes #34
1 parent 53a969c commit 8ffab2f

File tree

2 files changed

+21
-2
lines changed
  • bundles/com.eclipsesource.jshint/src/com/eclipsesource/jshint
  • tests/com.eclipsesource.jshint.test/src/com/eclipsesource/jshint

2 files changed

+21
-2
lines changed

bundles/com.eclipsesource.jshint/src/com/eclipsesource/jshint/JSHint.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*******************************************************************************
2-
* Copyright (c) 2012 EclipseSource.
2+
* Copyright (c) 2012, 2013 EclipseSource.
33
* All rights reserved. This program and the accompanying materials
44
* are made available under the terms of the Eclipse Public License v1.0
55
* which accompanies this distribution, and is available at
@@ -240,7 +240,8 @@ private int fixPosition( Text text, int line, int character ) {
240240
int offset = text.getLineOffset( line - 1 );
241241
int indentIndex = 0;
242242
int charIndex = 0;
243-
while( indentIndex < character - 1 ) {
243+
int maxIndex = Math.min( character, string.length() - offset ) - 1;
244+
while( indentIndex < maxIndex ) {
244245
boolean isTab = string.charAt( offset + indentIndex ) == '\t';
245246
indentIndex += isTab ? indent : 1;
246247
charIndex++;

tests/com.eclipsesource.jshint.test/src/com/eclipsesource/jshint/JSHint_Test.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,24 @@ public void errorWithUndefInConfig() {
267267
assertThat( problems.get( 0 ).getMessage(), containsString( "'v' is not defined" ) );
268268
}
269269

270+
@Test
271+
public void errorAfterTabHasCorrectPosition() {
272+
jsHint.configure( new Configuration().addOption( "undef", true ) );
273+
274+
jsHint.check( "var x = 1,\t# y = 2;", handler );
275+
276+
assertEquals( 11, problems.get( 0 ).getCharacter() );
277+
}
278+
279+
@Test
280+
public void errorAtEndDoesNotThrowException() {
281+
jsHint.configure( new Configuration().addOption( "undef", true ) );
282+
283+
// Must not throw SIOOBE
284+
// See https://github.com/eclipsesource/jshint-eclipse/issues/34
285+
jsHint.check( "var x = 1;\t#", handler );
286+
}
287+
270288
private class TestHandler implements ProblemHandler {
271289

272290
public void handleProblem( Problem problem ) {

0 commit comments

Comments
 (0)