Skip to content
Open
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
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog

## Unreleased

### Fixes

- Avoid `StackOverflowError` in `Scopes.isAncestorOf` on deep `Scopes` chains ([#6249](https://github.com/getsentry/sentry-java/pull/6249))

## 8.60.0

### Features
Expand Down
17 changes: 6 additions & 11 deletions sentry/src/main/java/io/sentry/Scopes.java
Original file line number Diff line number Diff line change
Expand Up @@ -96,18 +96,13 @@ private Scopes(
@Override
@ApiStatus.Internal
public boolean isAncestorOf(final @Nullable IScopes otherScopes) {
if (otherScopes == null) {
return false;
}

if (this == otherScopes) {
return true;
}

if (otherScopes.getParentScopes() != null) {
return isAncestorOf(otherScopes.getParentScopes());
@Nullable IScopes candidate = otherScopes;
while (candidate != null) {
if (this == candidate) {
return true;
}
candidate = candidate.getParentScopes();
}

return false;
}

Expand Down
32 changes: 32 additions & 0 deletions sentry/src/test/java/io/sentry/ScopesTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,38 @@ class ScopesTest {
assertEquals("b", cloneScope!!.tags["scopes"])
}

@Test
fun `isAncestorOf does not overflow the stack on a deep chain`() {
val options = SentryOptions()
options.cacheDirPath = file.absolutePath
options.dsn = "https://key@sentry.io/proj"
options.setSerializer(mock())
val root = createScopes(options)
var deepest: IScopes = root
repeat(20_000) { deepest = deepest.forkedCurrentScope("deep") }

val result = AtomicReference<Any>()
val thread =
Thread(
null,
{
result.set(
try {
root.isAncestorOf(deepest)
} catch (e: StackOverflowError) {
e
}
)
},
"small-stack",
256 * 1024,
)
thread.start()
thread.join()

assertEquals(true, result.get())
}

@Test
fun `when scopes is initialized, breadcrumbs are capped as per options`() {
val options = SentryOptions()
Expand Down