Skip to content

[5.9] Fix two memory corruption bugs in SwiftSyntax #1719

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
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 @@ -221,7 +221,7 @@ func syntaxNode(emitKind: String) -> SourceFileSyntax {
AccessorDeclSyntax(
"""
set(value) {
self = \(raw: node.name)(data.replacingChild(at: \(raw: index), with: value\(raw: child.isOptional ? "?" : "").raw, arena: SyntaxArena()))
self = \(raw: node.name)(data.replacingChild(at: \(raw: index), with: value\(raw: child.isOptional ? "?" : "").data, arena: SyntaxArena()))
}
"""
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -342,8 +342,9 @@ let syntaxRewriterFile = SourceFileSyntax(leadingTrivia: copyrightHeader) {
/// a parent if `node` had one.
public func rewrite(_ node: Syntax) -> Syntax {
let rewritten = self.visit(node)
let arena = SyntaxArena()
return Syntax(node.data.replacingSelf(rewritten.raw, arena: arena))
return withExtendedLifetime(rewritten) {
return Syntax(node.data.replacingSelf(rewritten.raw, arena: rewritten.raw.arena))
}
}
"""
)
Expand Down
15 changes: 13 additions & 2 deletions Sources/SwiftSyntax/SyntaxData.swift
Original file line number Diff line number Diff line change
Expand Up @@ -311,8 +311,10 @@ struct SyntaxData {
let newParent = Syntax(parentData)
return SyntaxData(absoluteRaw.replacingSelf(newRaw, newRootId: parentData.nodeId.rootId), parent: newParent)
} else {
// Otherwise, we're already the root, so return the new root data.
return .forRoot(newRaw)
return withExtendedLifetime(arena) {
// Otherwise, we're already the root, so return the new root data.
return .forRoot(newRaw)
}
}
}

Expand All @@ -332,6 +334,15 @@ struct SyntaxData {
return replacingSelf(newRaw, arena: arena)
}

/// Identical to `replacingChild(at: Int, with: RawSyntax?, arena: SyntaxArena)`
/// that ensures that the arena of`newChild` doesn’t get de-allocated before
/// `newChild` has been addded to the result.
func replacingChild(at index: Int, with newChild: SyntaxData?, arena: SyntaxArena) -> SyntaxData {
return withExtendedLifetime(newChild) {
return replacingChild(at: index, with: newChild?.raw, arena: arena)
}
}

func withLeadingTrivia(_ leadingTrivia: Trivia, arena: SyntaxArena) -> SyntaxData {
if let raw = raw.withLeadingTrivia(leadingTrivia, arena: arena) {
return replacingSelf(raw, arena: arena)
Expand Down
5 changes: 3 additions & 2 deletions Sources/SwiftSyntax/generated/SyntaxRewriter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6996,7 +6996,8 @@ open class SyntaxRewriter {
/// a parent if `node` had one.
public func rewrite(_ node: Syntax) -> Syntax {
let rewritten = self.visit(node)
let arena = SyntaxArena()
return Syntax(node.data.replacingSelf(rewritten.raw, arena: arena))
return withExtendedLifetime(rewritten) {
return Syntax(node.data.replacingSelf(rewritten.raw, arena: rewritten.raw.arena))
}
}
}
Loading