Skip to content

Commit f44d13c

Browse files
jhoellersbrannen
authored andcommitted
Disable array allocation in case of no constructor resolution
See gh-28808 Closes gh-33386 (cherry picked from commit a3a48a2)
1 parent f00bc7b commit f44d13c

File tree

2 files changed

+24
-5
lines changed

2 files changed

+24
-5
lines changed

spring-expression/src/main/java/org/springframework/expression/spel/ast/ConstructorReference.java

+8-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2022 the original author or authors.
2+
* Copyright 2002-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -268,6 +268,13 @@ private TypedValue createArray(ExpressionState state) throws EvaluationException
268268
}
269269

270270
String type = (String) intendedArrayType;
271+
272+
if (state.getEvaluationContext().getConstructorResolvers().isEmpty()) {
273+
// No constructor resolver -> no array construction either (as of 5.3.38)
274+
throw new SpelEvaluationException(getStartPosition(), SpelMessage.CONSTRUCTOR_NOT_FOUND,
275+
type + "[]", "[]");
276+
}
277+
271278
Class<?> componentType;
272279
TypeCode arrayTypeCode = TypeCode.forName(type);
273280
if (arrayTypeCode == TypeCode.OBJECT) {

spring-expression/src/test/java/org/springframework/expression/spel/ArrayConstructorTests.java

+16-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2022 the original author or authors.
2+
* Copyright 2002-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -18,17 +18,21 @@
1818

1919
import org.junit.jupiter.api.Test;
2020

21+
import org.springframework.expression.EvaluationContext;
2122
import org.springframework.expression.Expression;
2223
import org.springframework.expression.spel.standard.SpelExpressionParser;
24+
import org.springframework.expression.spel.support.SimpleEvaluationContext;
2325
import org.springframework.util.ObjectUtils;
2426

2527
import static org.assertj.core.api.Assertions.assertThat;
28+
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
2629

2730
/**
2831
* Test construction of arrays.
2932
*
3033
* @author Andy Clement
3134
* @author Sam Brannen
35+
* @author Juergen Hoeller
3236
*/
3337
class ArrayConstructorTests extends AbstractExpressionTests {
3438

@@ -97,7 +101,7 @@ void errorCases() {
97101
void typeArrayConstructors() {
98102
evaluate("new String[]{'a','b','c','d'}[1]", "b", String.class);
99103
evaluateAndCheckError("new String[]{'a','b','c','d'}.size()", SpelMessage.METHOD_NOT_FOUND, 30, "size()",
100-
"java.lang.String[]");
104+
"java.lang.String[]");
101105
evaluate("new String[]{'a','b','c','d'}.length", 4, Integer.class);
102106
}
103107

@@ -110,10 +114,18 @@ void basicArray() {
110114
void multiDimensionalArrays() {
111115
evaluate("new String[2][2]", "[Ljava.lang.String;[2]{[2]{null,null},[2]{null,null}}", String[][].class);
112116
evaluate("new String[3][2][1]",
113-
"[[Ljava.lang.String;[3]{[2]{[1]{null},[1]{null}},[2]{[1]{null},[1]{null}},[2]{[1]{null},[1]{null}}}",
114-
String[][][].class);
117+
"[[Ljava.lang.String;[3]{[2]{[1]{null},[1]{null}},[2]{[1]{null},[1]{null}},[2]{[1]{null},[1]{null}}}",
118+
String[][][].class);
115119
}
116120

121+
@Test
122+
void noArrayConstruction() {
123+
EvaluationContext context = SimpleEvaluationContext.forReadWriteDataBinding().build();
124+
assertThatExceptionOfType(SpelEvaluationException.class).isThrownBy(() ->
125+
parser.parseExpression("new int[2]").getValue(context));
126+
}
127+
128+
117129
private void evaluateArrayBuildingExpression(String expression, String expectedToString) {
118130
SpelExpressionParser parser = new SpelExpressionParser();
119131
Expression e = parser.parseExpression(expression);

0 commit comments

Comments
 (0)