Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid StackOverflowError (fixes LOGBACK-1454) #515

Merged
merged 1 commit into from
Aug 18, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,11 @@

import ch.qos.logback.core.CoreConstants;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Collections;
import java.util.IdentityHashMap;
import java.util.List;
import java.util.Set;

public class ThrowableProxy implements IThrowableProxy {

Expand All @@ -29,59 +32,52 @@ public class ThrowableProxy implements IThrowableProxy {
int commonFrames;
private ThrowableProxy cause;
private ThrowableProxy[] suppressed = NO_SUPPRESSED;
private final Set<Throwable> alreadyProcessedSet;

private transient PackagingDataCalculator packagingDataCalculator;
private boolean calculatedPackageData = false;

private static final Method GET_SUPPRESSED_METHOD;

static {
Method method = null;
try {
method = Throwable.class.getMethod("getSuppressed");
} catch (NoSuchMethodException e) {
// ignore, will get thrown in Java < 7
}
GET_SUPPRESSED_METHOD = method;
}

private static final ThrowableProxy[] NO_SUPPRESSED = new ThrowableProxy[0];

public ThrowableProxy(Throwable throwable) {
// Using Collections.newSetFromMap(new IdentityHashMap<>()) is inspired from
// Throwable.printStackTrace(PrintStreamOrWriter):
// Guard against malicious overrides of Throwable.equals by
// using a Set with identity equality semantics.
this(throwable, Collections.newSetFromMap(new IdentityHashMap<>()));
}

public ThrowableProxy(Throwable throwable, Set<Throwable> alreadyProcessedSet) {

this.throwable = throwable;
this.className = throwable.getClass().getName();
this.message = throwable.getMessage();
this.stackTraceElementProxyArray = ThrowableProxyUtil.steArrayToStepArray(throwable.getStackTrace());
this.alreadyProcessedSet = alreadyProcessedSet;

alreadyProcessedSet.add(throwable);

Throwable nested = throwable.getCause();

if (nested != null) {
this.cause = new ThrowableProxy(nested);
if (nested != null && !alreadyProcessedSet.contains(nested)) {
this.cause = new ThrowableProxy(nested, alreadyProcessedSet);
this.cause.commonFrames = ThrowableProxyUtil.findNumberOfCommonFrames(nested.getStackTrace(), stackTraceElementProxyArray);
}
if (GET_SUPPRESSED_METHOD != null) {
// this will only execute on Java 7
try {
Object obj = GET_SUPPRESSED_METHOD.invoke(throwable);
if (obj instanceof Throwable[]) {
Throwable[] throwableSuppressed = (Throwable[]) obj;
if (throwableSuppressed.length > 0) {
suppressed = new ThrowableProxy[throwableSuppressed.length];
for (int i = 0; i < throwableSuppressed.length; i++) {
this.suppressed[i] = new ThrowableProxy(throwableSuppressed[i]);
this.suppressed[i].commonFrames = ThrowableProxyUtil.findNumberOfCommonFrames(throwableSuppressed[i].getStackTrace(),
stackTraceElementProxyArray);
}
}
Throwable[] throwableSuppressed = throwable.getSuppressed();
if (throwableSuppressed.length > 0) {
List<ThrowableProxy> suppressedList = new ArrayList<>(throwableSuppressed.length);
for (int i = 0; i < throwableSuppressed.length; i++) {
Throwable sup = throwableSuppressed[i];
if (alreadyProcessedSet.contains(sup)) {
continue; // loop detected
}
} catch (IllegalAccessException e) {
// ignore
} catch (InvocationTargetException e) {
// ignore
ThrowableProxy throwableProxy = new ThrowableProxy(sup, alreadyProcessedSet);
throwableProxy.commonFrames = ThrowableProxyUtil.findNumberOfCommonFrames(sup.getStackTrace(),
stackTraceElementProxyArray);
suppressedList.add(throwableProxy);
}
this.suppressed = suppressedList.toArray(new ThrowableProxy[suppressedList.size()]);
}

}

public Throwable getThrowable() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,24 @@ public void multiNested() {
verify(w);
}

// see also https://jira.qos.ch/browse/LOGBACK-1454
@Test
public void nestedLoop1() {
Exception e = new Exception("foo");
Exception e2 = new Exception(e);
e.initCause(e2);
new ThrowableProxy(e);
}

// see also https://jira.qos.ch/browse/LOGBACK-1454
@Test
public void nestedLoop2() {
Exception e = new Exception("foo");
Exception e2 = new Exception(e);
e.addSuppressed(e2);
new ThrowableProxy(e);
}

void someMethod() throws Exception {
throw new Exception("someMethod");
}
Expand Down