Skip to content

Commit 90cfde9

Browse files
sbrannenbclozel
authored andcommitted
Improve diagnostics in SpEL for large array creation
Attempting to create a large array in a SpEL expression can result in an OutOfMemoryError. Although the JVM recovers from that, the error message is not very helpful to the user. This commit improves the diagnostics in SpEL for large array creation by throwing a SpelEvaluationException with a meaningful error message in order to improve diagnostics for the user. Closes gh-28257
1 parent 94f52bc commit 90cfde9

File tree

2 files changed

+29
-4
lines changed

2 files changed

+29
-4
lines changed

Diff for: spring-expression/src/main/java/org/springframework/expression/spel/SpelMessage.java

+7-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2018 the original author or authors.
2+
* Copyright 2002-2022 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.
@@ -31,6 +31,7 @@
3131
*
3232
* @author Andy Clement
3333
* @author Juergen Hoeller
34+
* @author Sam Brannen
3435
* @since 3.0
3536
*/
3637
public enum SpelMessage {
@@ -255,7 +256,11 @@ public enum SpelMessage {
255256

256257
/** @since 4.3.17 */
257258
FLAWED_PATTERN(Kind.ERROR, 1073,
258-
"Failed to efficiently evaluate pattern ''{0}'': consider redesigning it");
259+
"Failed to efficiently evaluate pattern ''{0}'': consider redesigning it"),
260+
261+
/** @since 5.2.20 */
262+
MAX_ARRAY_ELEMENTS_THRESHOLD_EXCEEDED(Kind.ERROR, 1075,
263+
"Array declares too many elements, exceeding the threshold of ''{0}''");
259264

260265

261266
private final Kind kind;

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

+22-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2021 the original author or authors.
2+
* Copyright 2002-2022 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.
@@ -53,10 +53,18 @@
5353
*
5454
* @author Andy Clement
5555
* @author Juergen Hoeller
56+
* @author Sam Brannen
5657
* @since 3.0
5758
*/
5859
public class ConstructorReference extends SpelNodeImpl {
5960

61+
/**
62+
* Maximum number of elements permitted in an array declaration, applying
63+
* to one-dimensional as well as multi-dimensional arrays.
64+
* @since 5.2.20
65+
*/
66+
private static final int MAX_ARRAY_ELEMENTS = 256 * 1024; // 256K
67+
6068
private final boolean isArrayConstructor;
6169

6270
@Nullable
@@ -259,14 +267,19 @@ private TypedValue createArray(ExpressionState state) throws EvaluationException
259267
// Shortcut for 1-dimensional
260268
TypedValue o = this.dimensions[0].getTypedValue(state);
261269
int arraySize = ExpressionUtils.toInt(typeConverter, o);
270+
checkNumElements(arraySize);
262271
newArray = Array.newInstance(componentType, arraySize);
263272
}
264273
else {
265274
// Multi-dimensional - hold onto your hat!
266275
int[] dims = new int[this.dimensions.length];
276+
long numElements = 1;
267277
for (int d = 0; d < this.dimensions.length; d++) {
268278
TypedValue o = this.dimensions[d].getTypedValue(state);
269-
dims[d] = ExpressionUtils.toInt(typeConverter, o);
279+
int arraySize = ExpressionUtils.toInt(typeConverter, o);
280+
dims[d] = arraySize;
281+
numElements *= arraySize;
282+
checkNumElements(numElements);
270283
}
271284
newArray = Array.newInstance(componentType, dims);
272285
}
@@ -327,6 +340,13 @@ else if (arrayTypeCode == TypeCode.SHORT) {
327340
return new TypedValue(newArray);
328341
}
329342

343+
private void checkNumElements(long numElements) {
344+
if (numElements >= MAX_ARRAY_ELEMENTS) {
345+
throw new SpelEvaluationException(getStartPosition(),
346+
SpelMessage.MAX_ARRAY_ELEMENTS_THRESHOLD_EXCEEDED, MAX_ARRAY_ELEMENTS);
347+
}
348+
}
349+
330350
private void populateReferenceTypeArray(ExpressionState state, Object newArray, TypeConverter typeConverter,
331351
InlineList initializer, Class<?> componentType) {
332352

0 commit comments

Comments
 (0)