Skip to content

Commit f075aac

Browse files
spodgurskiysnicoll
authored andcommitted
Fix MethodBasedEvaluationContext.lazyLoadArguments
This commit fix a potential `ArrayIndexOutOfBoundsException` if `lazyLoadArguments` is called with an empty variable argument. See gh-1070
1 parent dd65689 commit f075aac

File tree

2 files changed

+40
-1
lines changed

2 files changed

+40
-1
lines changed

spring-context/src/main/java/org/springframework/context/expression/MethodBasedEvaluationContext.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ protected void lazyLoadArguments() {
8989
String[] parameterNames = this.paramDiscoverer.getParameterNames(this.method);
9090
// save parameter names (if discovered)
9191
if (parameterNames != null) {
92-
for (int i = 0; i < parameterNames.length; i++) {
92+
for (int i = 0; i < args.length; i++) {
9393
setVariable(parameterNames[i], this.args[i]);
9494
}
9595
}

spring-context/src/test/java/org/springframework/context/expression/MethodBasedEvaluationContextTests.java

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,42 @@ public void nullArgument() {
6262
assertNull(context.lookupVariable("p0"));
6363
}
6464

65+
@Test
66+
public void varArgEmpty() {
67+
Method method = ReflectionUtils.findMethod(SampleMethods.class, "hello", Boolean.class, String[].class);
68+
MethodBasedEvaluationContext context = createEvaluationContext(method, new Object[] {null});
69+
70+
assertNull(context.lookupVariable("p0"));
71+
assertNull(context.lookupVariable("p1"));
72+
}
73+
74+
@Test
75+
public void varArgNull() {
76+
Method method = ReflectionUtils.findMethod(SampleMethods.class, "hello", Boolean.class, String[].class);
77+
MethodBasedEvaluationContext context = createEvaluationContext(method, new Object[] {null, null});
78+
79+
assertNull(context.lookupVariable("p0"));
80+
assertNull(context.lookupVariable("p1"));
81+
}
82+
83+
@Test
84+
public void varArgSingle() {
85+
Method method = ReflectionUtils.findMethod(SampleMethods.class, "hello", Boolean.class, String[].class);
86+
MethodBasedEvaluationContext context = createEvaluationContext(method, new Object[] {null, "hello"});
87+
88+
assertNull(context.lookupVariable("p0"));
89+
assertEquals("hello", context.lookupVariable("p1"));
90+
}
91+
92+
@Test
93+
public void varArgMultiple() {
94+
Method method = ReflectionUtils.findMethod(SampleMethods.class, "hello", Boolean.class, String[].class);
95+
MethodBasedEvaluationContext context = createEvaluationContext(method, new Object[] {null, new String[]{"hello", "hi"}});
96+
97+
assertNull(context.lookupVariable("p0"));
98+
assertNotNull(context.lookupVariable("p1"));
99+
}
100+
65101
private MethodBasedEvaluationContext createEvaluationContext(Method method, Object[] args) {
66102
return new MethodBasedEvaluationContext(this, method, args, this.paramDiscover);
67103
}
@@ -73,6 +109,9 @@ private static class SampleMethods {
73109
private void hello(String foo, Boolean flag) {
74110
}
75111

112+
private void hello(Boolean flag, String ... vararg){
113+
114+
}
76115
}
77116

78117
}

0 commit comments

Comments
 (0)