From 8951a339d427efbc8b30a9f07319a22ed53e440b Mon Sep 17 00:00:00 2001 From: akarnokd Date: Tue, 23 Jun 2015 23:05:51 +0200 Subject: [PATCH] CompositeException extra NPE protection --- .../java/rx/exceptions/CompositeException.java | 17 ++++++++++++----- .../rx/exceptions/CompositeExceptionTest.java | 13 +++++++++++++ 2 files changed, 25 insertions(+), 5 deletions(-) diff --git a/src/main/java/rx/exceptions/CompositeException.java b/src/main/java/rx/exceptions/CompositeException.java index 2084ef4607..7d6e37e8b9 100644 --- a/src/main/java/rx/exceptions/CompositeException.java +++ b/src/main/java/rx/exceptions/CompositeException.java @@ -49,12 +49,19 @@ public final class CompositeException extends RuntimeException { public CompositeException(String messagePrefix, Collection errors) { Set deDupedExceptions = new LinkedHashSet(); List _exceptions = new ArrayList(); - for (Throwable ex : errors) { - if (ex instanceof CompositeException) { - deDupedExceptions.addAll(((CompositeException) ex).getExceptions()); - } else { - deDupedExceptions.add(ex); + if (errors != null) { + for (Throwable ex : errors) { + if (ex instanceof CompositeException) { + deDupedExceptions.addAll(((CompositeException) ex).getExceptions()); + } else + if (ex != null) { + deDupedExceptions.add(ex); + } else { + deDupedExceptions.add(new NullPointerException()); + } } + } else { + deDupedExceptions.add(new NullPointerException()); } _exceptions.addAll(deDupedExceptions); diff --git a/src/test/java/rx/exceptions/CompositeExceptionTest.java b/src/test/java/rx/exceptions/CompositeExceptionTest.java index cc4a7e0b03..5fadadd42c 100644 --- a/src/test/java/rx/exceptions/CompositeExceptionTest.java +++ b/src/test/java/rx/exceptions/CompositeExceptionTest.java @@ -165,4 +165,17 @@ private static Throwable getRootCause(Throwable ex) { } } } + + @Test + public void testNullCollection() { + CompositeException composite = new CompositeException(null); + composite.getCause(); + composite.printStackTrace(); + } + @Test + public void testNullElement() { + CompositeException composite = new CompositeException(Arrays.asList((Throwable)null)); + composite.getCause(); + composite.printStackTrace(); + } } \ No newline at end of file