Skip to content

Commit

Permalink
Fix bug in CompositeException.getRootCause (#6380)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
guillermocalvo authored and akarnokd committed Jan 21, 2019
1 parent d40f923 commit 621b8cd
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ public int size() {
*/
/*private */Throwable getRootCause(Throwable e) {
Throwable root = e.getCause();
if (root == null || cause == root) {
if (root == null || e == root) {
return e;
}
while (true) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,22 @@ public synchronized Throwable getCause() {
}
};
CompositeException ex = new CompositeException(throwable);
assertSame(ex, ex.getRootCause(ex));
assertSame(ex0, ex.getRootCause(ex));
}

@Test
public void rootCauseSelf() {
Throwable throwable = new Throwable() {

private static final long serialVersionUID = -4398003222998914415L;

@Override
public synchronized Throwable getCause() {
return this;
}
};
CompositeException tmp = new CompositeException(new TestException());
assertSame(throwable, tmp.getRootCause(throwable));
}
}

Expand Down

0 comments on commit 621b8cd

Please sign in to comment.