Skip to content

Commit ab4b276

Browse files
committed
Fix an access to freed memory when modifying token nodes
When a token node is modified, we would not add its arena as a child to the new arena. This is unsafe because the newly created token might still have its text stored in the original arena.
1 parent cc9fc6b commit ab4b276

File tree

1 file changed

+3
-0
lines changed

1 file changed

+3
-0
lines changed

Sources/SwiftSyntax/Raw/RawSyntaxTokenView.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,7 @@ public struct RawSyntaxTokenView {
174174
/// kind changed to `newValue`.
175175
@_spi(RawSyntax)
176176
public func withKind(_ newValue: TokenKind, arena: SyntaxArena) -> RawSyntax {
177+
arena.addChild(self.raw.arenaReference)
177178
switch raw.rawData.payload {
178179
case .parsedToken(_):
179180
// The wholeText can't be continuous anymore. Make a materialized token.
@@ -200,6 +201,7 @@ public struct RawSyntaxTokenView {
200201
/// Returns a `RawSyntax` node with the presence changed to `newValue`.
201202
@_spi(RawSyntax)
202203
public func withPresence(_ newValue: SourcePresence, arena: SyntaxArena) -> RawSyntax {
204+
arena.addChild(self.raw.arenaReference)
203205
switch raw.rawData.payload {
204206
case .parsedToken(var payload):
205207
payload.presence = newValue
@@ -269,6 +271,7 @@ public struct RawSyntaxTokenView {
269271

270272
@_spi(RawSyntax)
271273
public func withTokenDiagnostic(tokenDiagnostic: TokenDiagnostic?, arena: SyntaxArena) -> RawSyntax {
274+
arena.addChild(self.raw.arenaReference)
272275
switch raw.rawData.payload {
273276
case .parsedToken(var dat):
274277
dat.tokenDiagnostic = tokenDiagnostic

0 commit comments

Comments
 (0)