Skip to content

Don't use recursion in TrimmedThrowableData to avoid stack overflow #5310

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

Merged
merged 1 commit into from
Sep 11, 2023
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 @@ -14,6 +14,7 @@

package com.google.firebase.crashlytics.internal.stacktrace;

import static com.google.firebase.crashlytics.internal.stacktrace.TrimmedThrowableData.makeTrimmedThrowableData;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.mock;

Expand Down Expand Up @@ -53,7 +54,7 @@ public void testStackTraceIsTrimmed() {
doReturn(mockStackTrace(3)).when(mockException).getStackTrace();

final StackTraceTrimmingStrategy trimmingStrategy = new TruncateStrategy(1);
final TrimmedThrowableData t = new TrimmedThrowableData(mockException, trimmingStrategy);
final TrimmedThrowableData t = makeTrimmedThrowableData(mockException, trimmingStrategy);

assertEquals(1, t.stacktrace.length);
assertEquals(mockException.getStackTrace()[0], t.stacktrace[0]);
Expand All @@ -65,7 +66,7 @@ public void testCauseStackTraceIsTrimmed() {
doReturn(mockCause).when(mockException).getCause();

final StackTraceTrimmingStrategy trimmingStrategy = new TruncateStrategy(1);
final TrimmedThrowableData t = new TrimmedThrowableData(mockException, trimmingStrategy);
final TrimmedThrowableData t = makeTrimmedThrowableData(mockException, trimmingStrategy);

assertEquals(1, t.stacktrace.length);
assertEquals(mockException.getStackTrace()[0], t.stacktrace[0]);
Expand All @@ -78,7 +79,7 @@ public void testStackTraceIsNotModifiedIfSmallEnough() {
doReturn(mockStackTrace(3)).when(mockException).getStackTrace();

final StackTraceTrimmingStrategy trimmingStrategy = new TruncateStrategy(5);
final TrimmedThrowableData t = new TrimmedThrowableData(mockException, trimmingStrategy);
final TrimmedThrowableData t = makeTrimmedThrowableData(mockException, trimmingStrategy);

assertEquals(3, t.stacktrace.length);
assertTrue(Arrays.equals(mockException.getStackTrace(), t.stacktrace));
Expand All @@ -90,7 +91,7 @@ public void testCauseStackTraceIsNotModifiedIfSmallEnough() {
doReturn(mockCause).when(mockException).getCause();

final StackTraceTrimmingStrategy trimmingStrategy = new TruncateStrategy(5);
final TrimmedThrowableData t = new TrimmedThrowableData(mockException, trimmingStrategy);
final TrimmedThrowableData t = makeTrimmedThrowableData(mockException, trimmingStrategy);

assertEquals(3, t.stacktrace.length);
assertTrue(Arrays.equals(mockException.getStackTrace(), t.stacktrace));
Expand All @@ -105,7 +106,7 @@ public void testOnlyCauseStackTraceIsTrimmed() {
doReturn(mockCause).when(mockException).getCause();

final StackTraceTrimmingStrategy trimmingStrategy = new TruncateStrategy(4);
final TrimmedThrowableData t = new TrimmedThrowableData(mockException, trimmingStrategy);
final TrimmedThrowableData t = makeTrimmedThrowableData(mockException, trimmingStrategy);

assertEquals(3, t.stacktrace.length);
assertTrue(Arrays.equals(mockException.getStackTrace(), t.stacktrace));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@

package com.google.firebase.crashlytics.internal.common;

import static com.google.firebase.crashlytics.internal.stacktrace.TrimmedThrowableData.makeTrimmedThrowableData;

import android.app.ActivityManager;
import android.app.ActivityManager.RunningAppProcessInfo;
import android.content.Context;
Expand Down Expand Up @@ -97,7 +99,7 @@ public Event captureEventData(
boolean includeAllThreads) {
final int orientation = context.getResources().getConfiguration().orientation;
final TrimmedThrowableData trimmedEvent =
new TrimmedThrowableData(event, stackTraceTrimmingStrategy);
makeTrimmedThrowableData(event, stackTraceTrimmingStrategy);

return Event.builder()
.setType(type)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@

package com.google.firebase.crashlytics.internal.stacktrace;

import androidx.annotation.Nullable;
import java.util.Stack;

/**
* Decorator class that exposes the appropriate APIs for Crashlytics to write crash data, but which
* pre-processes the stack trace to remove unnecessary frames based on the passed-in
Expand All @@ -23,14 +26,39 @@ public class TrimmedThrowableData {
public final String localizedMessage;
public final String className;
public final StackTraceElement[] stacktrace;
public final TrimmedThrowableData cause;
@Nullable public final TrimmedThrowableData cause;

private TrimmedThrowableData(
String localizedMessage,
String className,
StackTraceElement[] stacktrace,
@Nullable TrimmedThrowableData cause) {
this.localizedMessage = localizedMessage;
this.className = className;
this.stacktrace = stacktrace;
this.cause = cause;
}

public static TrimmedThrowableData makeTrimmedThrowableData(
Throwable ex, StackTraceTrimmingStrategy trimmingStrategy) {
Stack<Throwable> throwableStack = new Stack<>();
Throwable exCause = ex;
while (exCause != null) {
throwableStack.push(exCause);
exCause = exCause.getCause();
}

public TrimmedThrowableData(Throwable ex, StackTraceTrimmingStrategy trimmingStrategy) {
this.localizedMessage = ex.getLocalizedMessage();
this.className = ex.getClass().getName();
this.stacktrace = trimmingStrategy.getTrimmedStackTrace(ex.getStackTrace());
TrimmedThrowableData trimmedThrowableData = null;
while (!throwableStack.isEmpty()) {
Throwable throwable = throwableStack.pop();
trimmedThrowableData =
new TrimmedThrowableData(
throwable.getLocalizedMessage(),
throwable.getClass().getName(),
trimmingStrategy.getTrimmedStackTrace(throwable.getStackTrace()),
trimmedThrowableData);
}

final Throwable exCause = ex.getCause();
this.cause = (exCause != null) ? new TrimmedThrowableData(exCause, trimmingStrategy) : null;
return trimmedThrowableData;
}
}