Author: britter
Date: Tue Nov 24 16:21:28 2015
New Revision: 1716195
URL: http://svn.apache.org/viewvc?rev=1716195&view=rev
Log:
JXPATH-160: ValueUtils.getValue throws exception with set and index above size.
Thanks to Stefan Albrecht, Michele Vivoda and Uwe Barthel. This also fixes #6
from github.
Added:
commons/proper/jxpath/trunk/src/test/java/org/apache/commons/jxpath/util/ValueUtilsTest.java
Modified:
commons/proper/jxpath/trunk/src/changes/changes.xml
commons/proper/jxpath/trunk/src/main/java/org/apache/commons/jxpath/util/ValueUtils.java
Modified: commons/proper/jxpath/trunk/src/changes/changes.xml
URL:
http://svn.apache.org/viewvc/commons/proper/jxpath/trunk/src/changes/changes.xml?rev=1716195&r1=1716194&r2=1716195&view=diff
==============================================================================
--- commons/proper/jxpath/trunk/src/changes/changes.xml (original)
+++ commons/proper/jxpath/trunk/src/changes/changes.xml Tue Nov 24 16:21:28 2015
@@ -47,6 +47,9 @@ The <action> type attribute can be add,u
<body>
<!-- The release date is the date RC is cut -->
<release version="1.4" date="2014-??-??" description="New features and bug
fixes.">
+ <action issue="JXPATH-160" dev="britter" type="fix" due-to="Stefan
Albrecht, Michele Vivoda, Uwe Barthel">
+ ValueUtils.getValue throws exception with set and index above size
+ </action>
<action issue="JXPATH-162" dev="britter" type="fix" due-to="Uwe Barthel">
Concurrency problem for JXPathContextFactory.factoryImplName static
field
</action>
Modified:
commons/proper/jxpath/trunk/src/main/java/org/apache/commons/jxpath/util/ValueUtils.java
URL:
http://svn.apache.org/viewvc/commons/proper/jxpath/trunk/src/main/java/org/apache/commons/jxpath/util/ValueUtils.java?rev=1716195&r1=1716194&r2=1716195&view=diff
==============================================================================
---
commons/proper/jxpath/trunk/src/main/java/org/apache/commons/jxpath/util/ValueUtils.java
(original)
+++
commons/proper/jxpath/trunk/src/main/java/org/apache/commons/jxpath/util/ValueUtils.java
Tue Nov 24 16:21:28 2015
@@ -305,6 +305,10 @@ public class ValueUtils {
value = ((List) collection).get(index);
}
else if (collection instanceof Collection) {
+ if (index < 0 || index >= ((Collection) collection).size()) {
+ return null;
+ }
+
int i = 0;
Iterator it = ((Collection) collection).iterator();
for (; i < index; i++) {
Added:
commons/proper/jxpath/trunk/src/test/java/org/apache/commons/jxpath/util/ValueUtilsTest.java
URL:
http://svn.apache.org/viewvc/commons/proper/jxpath/trunk/src/test/java/org/apache/commons/jxpath/util/ValueUtilsTest.java?rev=1716195&view=auto
==============================================================================
---
commons/proper/jxpath/trunk/src/test/java/org/apache/commons/jxpath/util/ValueUtilsTest.java
(added)
+++
commons/proper/jxpath/trunk/src/test/java/org/apache/commons/jxpath/util/ValueUtilsTest.java
Tue Nov 24 16:21:28 2015
@@ -0,0 +1,79 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.jxpath.util;
+
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.HashSet;
+import java.util.Set;
+import junit.framework.TestCase;
+
+public class ValueUtilsTest extends TestCase {
+
+
+
+ public void testGetValueFromArrayTooSmall() {
+ assertNull(ValueUtils.getValue(new Object[0], 2));
+ }
+
+ public void testGetValueFromListTooSmall() {
+ assertNull(ValueUtils.getValue(Collections.EMPTY_LIST, 2));
+ }
+
+ /*
+ * This test would break without the patch and an NoSuchElementException
being
+ * thrown instead.
+ */
+ public void testGetValueFromSetTooSmall() {
+ assertNull(ValueUtils.getValue(Collections.EMPTY_SET, 2));
+ }
+
+ public void testGetValueFromArray() {
+ final Object data = new Object();
+ assertSame(data, ValueUtils.getValue(new Object[] {data}, 0));
+ }
+
+ public void testGetValueFromList() {
+ final Object data = new Object();
+ assertSame(data, ValueUtils.getValue(Arrays.asList(new
Object[]{data}), 0));
+ }
+
+ public void testGetValueFromSet() {
+ final Object data = new Object();
+ final Set dataSet = new HashSet();
+ dataSet.add(data);
+ assertSame(data, ValueUtils.getValue(dataSet, 0));
+ }
+
+ public void testGetValueFromArrayNegativeIndex() {
+ final Object data = new Object();
+ assertNull(ValueUtils.getValue(new Object[] {data}, -1));
+ }
+
+ public void testGetValueFromListNegativeIndex() {
+ final Object data = new Object();
+ final Object res = ValueUtils.getValue(Arrays.asList(new
Object[]{data}), -1);
+ assertNull("Expected null, is " + res, res);
+ }
+
+ public void testGetValueFromSetNegativeIndex() {
+ final Object data = new Object();
+ final Set dataSet = new HashSet();
+ dataSet.add(data);
+ assertNull(ValueUtils.getValue(dataSet, -1));
+ }
+}