You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@felix.apache.org by da...@apache.org on 2021/05/04 16:09:59 UTC

[felix-dev] branch master updated: FELIX-6402 don't swallow exception when converting to array

This is an automated email from the ASF dual-hosted git repository.

davidb pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/felix-dev.git


The following commit(s) were added to refs/heads/master by this push:
     new 18aa59f  FELIX-6402 don't swallow exception when converting to array
     new b178fda  Merge pull request #74 from kwin/FELIX-6402_dont-swallow-CME
18aa59f is described below

commit 18aa59f1e05a25ef510a29f20f96922dfb6ebd94
Author: Konrad Windszus <kw...@apache.org>
AuthorDate: Tue May 4 16:47:42 2021 +0200

    FELIX-6402 don't swallow exception when converting to array
---
 .../org/osgi/util/converter/ConvertingImpl.java    |  8 +++---
 .../org/osgi/util/converter/ConverterTest.java     | 33 ++++++++++++++++++++++
 2 files changed, 37 insertions(+), 4 deletions(-)

diff --git a/converter/converter/src/main/java/org/osgi/util/converter/ConvertingImpl.java b/converter/converter/src/main/java/org/osgi/util/converter/ConvertingImpl.java
index 71e333a..87410a8 100644
--- a/converter/converter/src/main/java/org/osgi/util/converter/ConvertingImpl.java
+++ b/converter/converter/src/main/java/org/osgi/util/converter/ConvertingImpl.java
@@ -297,20 +297,20 @@ class ConvertingImpl extends AbstractSpecifying<Converting>
 	@SuppressWarnings("unchecked")
 	private <T> T convertToArray(Class< ? > componentClz, Type componentType, InternalConverter c) {
 		Collection< ? > collectionView = collectionView(c);
-		Iterator< ? > itertor = collectionView.iterator();
+		Iterator< ? > iterator = collectionView.iterator();
 		try {
 			Object array = Array.newInstance(componentClz,
 					collectionView.size());
 			for (int i = 0; i < collectionView.size()
-					&& itertor.hasNext(); i++) {
-				Object next = itertor.next();
+					&& iterator.hasNext(); i++) {
+				Object next = iterator.next();
 				Object converted = c.convert(next)
 						.to(componentType);
 				Array.set(array, i, converted);
 			}
 			return (T) array;
 		} catch (Exception e) {
-			return null;
+			throw new ConversionException("Cannot iterate over " + collectionView.getClass(), e);
 		}
 	}
 
diff --git a/converter/converter/src/test/java/org/osgi/util/converter/ConverterTest.java b/converter/converter/src/test/java/org/osgi/util/converter/ConverterTest.java
index b635edf..f92205e 100644
--- a/converter/converter/src/test/java/org/osgi/util/converter/ConverterTest.java
+++ b/converter/converter/src/test/java/org/osgi/util/converter/ConverterTest.java
@@ -50,6 +50,7 @@ import java.util.Arrays;
 import java.util.Calendar;
 import java.util.Collection;
 import java.util.Collections;
+import java.util.ConcurrentModificationException;
 import java.util.Date;
 import java.util.Deque;
 import java.util.GregorianCalendar;
@@ -388,6 +389,38 @@ public class ConverterTest {
     	checkArray(double[][][].class);
     }
 
+    @Test
+    public void testPropagatingExceptionInArray() {
+        try {
+            Set<String> concurrentModificationSet = new HashSet<String>() {
+                private static final long serialVersionUID = 1L;
+
+                @Override
+                public Iterator<String> iterator() {
+                    return new Iterator<String>() {
+
+                        @Override
+                        public boolean hasNext() {
+                            return true;
+                        }
+
+                        @Override
+                        public String next() {
+                            throw new ConcurrentModificationException("This iterator deliberately throws CMEs!");
+                        }
+                    };
+                }
+                
+            };
+            concurrentModificationSet.add("one");
+            concurrentModificationSet.add("two");
+            converter.convert(concurrentModificationSet).to(String[].class);
+            fail("Should have thrown a Conversion Exception when a collection throwing a CME was used as source");
+        } catch (ConversionException e) {
+            // good
+        }
+    }
+
 	private void checkArray(Class<?> arrayType) {
 		assertTrue(arrayType.isArray());