Skip to content

Commit b1656be

Browse files
committed
Fix possible binder IndexOutOfBoundsException
Update RelaxedDataBinder.extendCollectionIfNecessary to use the current index when checking if the path node is an array. Fixes spring-projectsgh-5635
1 parent 1412eaa commit b1656be

File tree

2 files changed

+25
-3
lines changed

2 files changed

+25
-3
lines changed

spring-boot/src/main/java/org/springframework/boot/bind/RelaxedDataBinder.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2015 the original author or authors.
2+
* Copyright 2012-2016 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.
@@ -343,7 +343,7 @@ private void extendCollectionIfNecessary(BeanWrapper wrapper, BeanPath path,
343343
return;
344344
}
345345
Object extend = new LinkedHashMap<String, Object>();
346-
if (!elementDescriptor.isMap() && path.isArrayIndex(index + 1)) {
346+
if (!elementDescriptor.isMap() && path.isArrayIndex(index)) {
347347
extend = new ArrayList<Object>();
348348
}
349349
wrapper.setPropertyValue(path.prefix(index + 1), extend);

spring-boot/src/test/java/org/springframework/boot/bind/RelaxedDataBinderTests.java

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2015 the original author or authors.
2+
* Copyright 2012-2016 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.
@@ -648,6 +648,16 @@ public void testMixed() throws Exception {
648648
assertEquals("boo", target.getFooBaz());
649649
}
650650

651+
@Test
652+
public void testIndexBounds() throws Exception {
653+
VanillaTarget target = new VanillaTarget();
654+
RelaxedDataBinder binder = getBinder(target, "test");
655+
MutablePropertyValues values = new MutablePropertyValues();
656+
values.add("test.objects[0]", "teststring");
657+
binder.bind(values);
658+
assertEquals("teststring", target.getObjects().get(0));
659+
}
660+
651661
private void doTestBindCaseInsensitiveEnums(VanillaTarget target) throws Exception {
652662
BindingResult result = bind(target, "bingo: THIS");
653663
assertThat(result.getErrorCount(), equalTo(0));
@@ -1006,6 +1016,8 @@ public static class VanillaTarget {
10061016

10071017
private List<Bingo> bingos;
10081018

1019+
private List<Object> objects;
1020+
10091021
public char[] getBar() {
10101022
return this.bar;
10111023
}
@@ -1061,6 +1073,15 @@ public List<Bingo> getBingos() {
10611073
public void setBingos(List<Bingo> bingos) {
10621074
this.bingos = bingos;
10631075
}
1076+
1077+
public List<Object> getObjects() {
1078+
return this.objects;
1079+
}
1080+
1081+
public void setObjects(List<Object> objects) {
1082+
this.objects = objects;
1083+
}
1084+
10641085
}
10651086

10661087
enum Bingo {
@@ -1081,4 +1102,5 @@ public void setFoo(String foo) {
10811102
}
10821103

10831104
}
1105+
10841106
}

0 commit comments

Comments
 (0)