Skip to content

Commit bc4be3b

Browse files
authored
Cap max number of stack frames to 100 to not exceed payload size limit (#3009)
1 parent 3548754 commit bc4be3b

File tree

6 files changed

+32
-0
lines changed

6 files changed

+32
-0
lines changed

CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
### Fixes
66

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

911
## 6.32.0
1012

sentry-samples/sentry-samples-android/src/main/java/io/sentry/samples/android/MainActivity.java

+6
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,8 @@ protected void onCreate(Bundle savedInstanceState) {
164164
latch.countDown();
165165
});
166166

167+
binding.stackOverflow.setOnClickListener(view -> stackOverflow());
168+
167169
binding.nativeCrash.setOnClickListener(view -> NativeSample.crash());
168170

169171
binding.nativeCapture.setOnClickListener(view -> NativeSample.message());
@@ -252,6 +254,10 @@ public void run() {
252254
setContentView(binding.getRoot());
253255
}
254256

257+
private void stackOverflow() {
258+
stackOverflow();
259+
}
260+
255261
@Override
256262
protected void onResume() {
257263
super.onResume();

sentry-samples/sentry-samples-android/src/main/res/layout/activity_main.xml

+6
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,12 @@
7070
android:layout_height="wrap_content"
7171
android:text="@string/out_of_memory" />
7272

73+
<Button
74+
android:id="@+id/stack_overflow"
75+
android:layout_width="wrap_content"
76+
android:layout_height="wrap_content"
77+
android:text="@string/stack_overflow" />
78+
7379
<Button
7480
android:id="@+id/native_crash"
7581
android:layout_width="wrap_content"

sentry-samples/sentry-samples-android/src/main/res/values/strings.xml

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
<string name="app_name">Sentry sample</string>
33
<string name="crash_from_java">Crash from Java (UncaughtException)</string>
44
<string name="out_of_memory">Out of Memory (Mulithreaded)</string>
5+
<string name="stack_overflow">Stack Overflow</string>
56
<string name="send_message">Send Message</string>
67
<string name="send_message_from_inner_fragment">Send Message from inner fragment</string>
78
<string name="add_attachment">Add Attachment</string>

sentry/src/main/java/io/sentry/SentryStackTraceFactory.java

+6
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
@ApiStatus.Internal
1414
public final class SentryStackTraceFactory {
1515

16+
private static final int STACKTRACE_FRAME_LIMIT = 100;
1617
private final @NotNull SentryOptions options;
1718

1819
public SentryStackTraceFactory(final @NotNull SentryOptions options) {
@@ -55,6 +56,11 @@ public List<SentryStackFrame> getStackFrames(@Nullable final StackTraceElement[]
5556
}
5657
sentryStackFrame.setNative(item.isNativeMethod());
5758
sentryStackFrames.add(sentryStackFrame);
59+
60+
// hard cap to not exceed payload size limit
61+
if (sentryStackFrames.size() >= STACKTRACE_FRAME_LIMIT) {
62+
break;
63+
}
5864
}
5965
}
6066
Collections.reverse(sentryStackFrames);

sentry/src/test/java/io/sentry/SentryStackTraceFactoryTest.kt

+11
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,17 @@ class SentryStackTraceFactoryTest {
276276
assertEquals("com.example.myapp.MainActivity", callStack[1].module)
277277
}
278278

279+
@Test
280+
fun `caps number of stack frames to STACKTRACE_FRAME_LIMIT to not exceed payload size limit`() {
281+
val exception = Exception()
282+
exception.stackTrace = arrayOf()
283+
repeat(120) { exception.stackTrace += generateStackTrace("com.me.stackoverflow") }
284+
285+
val sut = SentryStackTraceFactory(SentryOptions())
286+
val sentryFrames = sut.getStackFrames(exception.stackTrace)
287+
288+
assertEquals(100, sentryFrames!!.size)
289+
}
279290
private fun generateStackTrace(className: String?) =
280291
StackTraceElement(className, "method", "fileName", 10)
281292
}

0 commit comments

Comments
 (0)