Skip to content

Commit

Permalink
Cap max number of stack frames to 100 to not exceed payload size limit (
Browse files Browse the repository at this point in the history
  • Loading branch information
romtsn committed Oct 23, 2023
1 parent 3548754 commit bc4be3b
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
### Fixes

- Fix crash when HTTP connection error message contains formatting symbols ([#3002](https://github.com/getsentry/sentry-java/pull/3002))
- Cap max number of stack frames to 100 to not exceed payload size limit ([#3009](https://github.com/getsentry/sentry-java/pull/3009))
- This will ensure we report errors with a big number of frames such as `StackOverflowError`

## 6.32.0

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,8 @@ protected void onCreate(Bundle savedInstanceState) {
latch.countDown();
});

binding.stackOverflow.setOnClickListener(view -> stackOverflow());

binding.nativeCrash.setOnClickListener(view -> NativeSample.crash());

binding.nativeCapture.setOnClickListener(view -> NativeSample.message());
Expand Down Expand Up @@ -252,6 +254,10 @@ public void run() {
setContentView(binding.getRoot());
}

private void stackOverflow() {
stackOverflow();
}

@Override
protected void onResume() {
super.onResume();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,12 @@
android:layout_height="wrap_content"
android:text="@string/out_of_memory" />

<Button
android:id="@+id/stack_overflow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/stack_overflow" />

<Button
android:id="@+id/native_crash"
android:layout_width="wrap_content"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
<string name="app_name">Sentry sample</string>
<string name="crash_from_java">Crash from Java (UncaughtException)</string>
<string name="out_of_memory">Out of Memory (Mulithreaded)</string>
<string name="stack_overflow">Stack Overflow</string>
<string name="send_message">Send Message</string>
<string name="send_message_from_inner_fragment">Send Message from inner fragment</string>
<string name="add_attachment">Add Attachment</string>
Expand Down
6 changes: 6 additions & 0 deletions sentry/src/main/java/io/sentry/SentryStackTraceFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
@ApiStatus.Internal
public final class SentryStackTraceFactory {

private static final int STACKTRACE_FRAME_LIMIT = 100;
private final @NotNull SentryOptions options;

public SentryStackTraceFactory(final @NotNull SentryOptions options) {
Expand Down Expand Up @@ -55,6 +56,11 @@ public List<SentryStackFrame> getStackFrames(@Nullable final StackTraceElement[]
}
sentryStackFrame.setNative(item.isNativeMethod());
sentryStackFrames.add(sentryStackFrame);

// hard cap to not exceed payload size limit
if (sentryStackFrames.size() >= STACKTRACE_FRAME_LIMIT) {
break;
}
}
}
Collections.reverse(sentryStackFrames);
Expand Down
11 changes: 11 additions & 0 deletions sentry/src/test/java/io/sentry/SentryStackTraceFactoryTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,17 @@ class SentryStackTraceFactoryTest {
assertEquals("com.example.myapp.MainActivity", callStack[1].module)
}

@Test
fun `caps number of stack frames to STACKTRACE_FRAME_LIMIT to not exceed payload size limit`() {
val exception = Exception()
exception.stackTrace = arrayOf()
repeat(120) { exception.stackTrace += generateStackTrace("com.me.stackoverflow") }

val sut = SentryStackTraceFactory(SentryOptions())
val sentryFrames = sut.getStackFrames(exception.stackTrace)

assertEquals(100, sentryFrames!!.size)
}
private fun generateStackTrace(className: String?) =
StackTraceElement(className, "method", "fileName", 10)
}

0 comments on commit bc4be3b

Please sign in to comment.