Fix possible binder IndexOutOfBoundsException

Update RelaxedDataBinder.extendCollectionIfNecessary to use the current
index when checking if the path node is an array.

Fixes gh-5635
This commit is contained in:
Phillip Webb 2016-04-12 14:32:31 -07:00
parent 1412eaa0e0
commit b1656be3d0
2 changed files with 25 additions and 3 deletions

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2012-2015 the original author or authors. * Copyright 2012-2016 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -343,7 +343,7 @@ public class RelaxedDataBinder extends DataBinder {
return; return;
} }
Object extend = new LinkedHashMap<String, Object>(); Object extend = new LinkedHashMap<String, Object>();
if (!elementDescriptor.isMap() && path.isArrayIndex(index + 1)) { if (!elementDescriptor.isMap() && path.isArrayIndex(index)) {
extend = new ArrayList<Object>(); extend = new ArrayList<Object>();
} }
wrapper.setPropertyValue(path.prefix(index + 1), extend); wrapper.setPropertyValue(path.prefix(index + 1), extend);

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2012-2015 the original author or authors. * Copyright 2012-2016 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -648,6 +648,16 @@ public class RelaxedDataBinderTests {
assertEquals("boo", target.getFooBaz()); assertEquals("boo", target.getFooBaz());
} }
@Test
public void testIndexBounds() throws Exception {
VanillaTarget target = new VanillaTarget();
RelaxedDataBinder binder = getBinder(target, "test");
MutablePropertyValues values = new MutablePropertyValues();
values.add("test.objects[0]", "teststring");
binder.bind(values);
assertEquals("teststring", target.getObjects().get(0));
}
private void doTestBindCaseInsensitiveEnums(VanillaTarget target) throws Exception { private void doTestBindCaseInsensitiveEnums(VanillaTarget target) throws Exception {
BindingResult result = bind(target, "bingo: THIS"); BindingResult result = bind(target, "bingo: THIS");
assertThat(result.getErrorCount(), equalTo(0)); assertThat(result.getErrorCount(), equalTo(0));
@ -1006,6 +1016,8 @@ public class RelaxedDataBinderTests {
private List<Bingo> bingos; private List<Bingo> bingos;
private List<Object> objects;
public char[] getBar() { public char[] getBar() {
return this.bar; return this.bar;
} }
@ -1061,6 +1073,15 @@ public class RelaxedDataBinderTests {
public void setBingos(List<Bingo> bingos) { public void setBingos(List<Bingo> bingos) {
this.bingos = bingos; this.bingos = bingos;
} }
public List<Object> getObjects() {
return this.objects;
}
public void setObjects(List<Object> objects) {
this.objects = objects;
}
} }
enum Bingo { enum Bingo {
@ -1081,4 +1102,5 @@ public class RelaxedDataBinderTests {
} }
} }
} }