diff --git a/spring-expression/src/main/java/org/springframework/expression/spel/ast/Indexer.java b/spring-expression/src/main/java/org/springframework/expression/spel/ast/Indexer.java index 6bcca8687868..4ef4a0ed4ece 100644 --- a/spring-expression/src/main/java/org/springframework/expression/spel/ast/Indexer.java +++ b/spring-expression/src/main/java/org/springframework/expression/spel/ast/Indexer.java @@ -439,7 +439,7 @@ else if (arrayComponentType == Short.TYPE) { } 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, arrayLength, index); } diff --git a/spring-expression/src/test/java/org/springframework/expression/spel/ast/IndexerTests.java b/spring-expression/src/test/java/org/springframework/expression/spel/ast/IndexerTests.java new file mode 100644 index 000000000000..ccae50f2c6ea --- /dev/null +++ b/spring-expression/src/test/java/org/springframework/expression/spel/ast/IndexerTests.java @@ -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())); + } +} \ No newline at end of file