Skip to content

Commit 621b8cd

Browse files
guillermocalvoakarnokd
authored andcommitted
Fix bug in CompositeException.getRootCause (#6380)
* Fix bug in CompositeException.getRootCause The original code use to be `if (root == null || root == e)`, but apparently after some refactoring it ended up as `if (root == null || cause == root)`, which I believe is a bug. This method should probably be `static` (that would have prevented the bug). * Update unit tests for CompositeException.getRootCause
1 parent d40f923 commit 621b8cd

File tree

2 files changed

+17
-2
lines changed

2 files changed

+17
-2
lines changed

src/main/java/io/reactivex/exceptions/CompositeException.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,7 @@ public int size() {
280280
*/
281281
/*private */Throwable getRootCause(Throwable e) {
282282
Throwable root = e.getCause();
283-
if (root == null || cause == root) {
283+
if (root == null || e == root) {
284284
return e;
285285
}
286286
while (true) {

src/test/java/io/reactivex/exceptions/CompositeExceptionTest.java

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -364,7 +364,22 @@ public synchronized Throwable getCause() {
364364
}
365365
};
366366
CompositeException ex = new CompositeException(throwable);
367-
assertSame(ex, ex.getRootCause(ex));
367+
assertSame(ex0, ex.getRootCause(ex));
368+
}
369+
370+
@Test
371+
public void rootCauseSelf() {
372+
Throwable throwable = new Throwable() {
373+
374+
private static final long serialVersionUID = -4398003222998914415L;
375+
376+
@Override
377+
public synchronized Throwable getCause() {
378+
return this;
379+
}
380+
};
381+
CompositeException tmp = new CompositeException(new TestException());
382+
assertSame(throwable, tmp.getRootCause(throwable));
368383
}
369384
}
370385

0 commit comments

Comments
 (0)