Avoid ArrayIndexOutOfBoundsException in SpEL's Indexer
When index == arrayLength, the array index is also out of bounds. For this scenario, a SpelEvaluationException should be thrown instead of ArrayIndexOutOfBoundsException. Closes gh-23658
This commit is contained in:
parent
b2704e1db6
commit
fde7b1e545
|
|
@ -439,7 +439,7 @@ public class Indexer extends SpelNodeImpl {
|
||||||
}
|
}
|
||||||
|
|
||||||
private void checkAccess(int arrayLength, int index) throws SpelEvaluationException {
|
private void checkAccess(int arrayLength, int index) throws SpelEvaluationException {
|
||||||
if (index > arrayLength) {
|
if (index >= arrayLength) {
|
||||||
throw new SpelEvaluationException(getStartPosition(), SpelMessage.ARRAY_INDEX_OUT_OF_BOUNDS,
|
throw new SpelEvaluationException(getStartPosition(), SpelMessage.ARRAY_INDEX_OUT_OF_BOUNDS,
|
||||||
arrayLength, index);
|
arrayLength, index);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,23 @@
|
||||||
|
package org.springframework.expression.spel.ast;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.springframework.expression.EvaluationException;
|
||||||
|
import org.springframework.expression.spel.standard.SpelExpression;
|
||||||
|
import org.springframework.expression.spel.standard.SpelExpressionParser;
|
||||||
|
import org.springframework.expression.spel.support.SimpleEvaluationContext;
|
||||||
|
import org.springframework.expression.spel.testresources.Inventor;
|
||||||
|
|
||||||
|
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author Joyce Zhan
|
||||||
|
*/
|
||||||
|
public class IndexerTests {
|
||||||
|
@Test
|
||||||
|
public void testAccess() {
|
||||||
|
SimpleEvaluationContext context = SimpleEvaluationContext.forReadOnlyDataBinding().build();
|
||||||
|
SpelExpression expression = (SpelExpression) new SpelExpressionParser().parseExpression("stringArrayOfThreeItems[3]");
|
||||||
|
assertThatExceptionOfType(EvaluationException.class).isThrownBy(() ->
|
||||||
|
expression.getValue(context, new Inventor()));
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue