Skip to content

Commit 54c0a8a

Browse files
committed
Fix a memory corruption bug when modifying SwiftSyntax nodes
When modifying SwiftSyntax nodes that calls `replacingSelf`, we could get into a situation where `arena` gets deallocated before it can be retained by the `forRoot` call. Throw in a `withExtendedLifetime` call to make sure we don’t loose the arena. rdar://109860357
1 parent 3d059c4 commit 54c0a8a

File tree

1 file changed

+5
-2
lines changed

1 file changed

+5
-2
lines changed

Sources/SwiftSyntax/SyntaxData.swift

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -311,8 +311,11 @@ struct SyntaxData {
311311
let newParent = Syntax(parentData)
312312
return SyntaxData(absoluteRaw.replacingSelf(newRaw, newRootId: parentData.nodeId.rootId), parent: newParent)
313313
} else {
314-
// Otherwise, we're already the root, so return the new root data.
315-
return .forRoot(newRaw)
314+
// Keep `arena` alive until it is retained by `SyntaxData` produced by `forRoot`.
315+
return withExtendedLifetime(arena) { _ in
316+
// Otherwise, we're already the root, so return the new root data.
317+
return .forRoot(newRaw)
318+
}
316319
}
317320
}
318321

0 commit comments

Comments
 (0)