Skip to content

Commit bce5eeb

Browse files
committed
[ASTGen] Null-terminate error messsages from ASTGen
Always null-terminate `BridgedString` by `allocateBridgedString()`. It had `nullTerminated: Bool` option, but allocating one extra byte doesn't harm anything. Always null-terminate the string just for client's convenience. rdar://117205829
1 parent 251becb commit bce5eeb

File tree

2 files changed

+9
-13
lines changed

2 files changed

+9
-13
lines changed

lib/ASTGen/Sources/ASTGen/Bridge.swift

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -40,24 +40,21 @@ extension String {
4040
}
4141
}
4242

43-
/// Allocate a copy of the given string as a UTF-8 string.
43+
/// Allocate a copy of the given string as a null-terminated UTF-8 string.
4444
func allocateBridgedString(
45-
_ string: String,
46-
nullTerminated: Bool = false
45+
_ string: String
4746
) -> BridgedString {
4847
var string = string
4948
return string.withUTF8 { utf8 in
50-
let capacity = utf8.count + (nullTerminated ? 1 : 0)
5149
let ptr = UnsafeMutablePointer<UInt8>.allocate(
52-
capacity: capacity
50+
capacity: utf8.count + 1
5351
)
5452
if let baseAddress = utf8.baseAddress {
5553
ptr.initialize(from: baseAddress, count: utf8.count)
5654
}
5755

58-
if nullTerminated {
59-
ptr[utf8.count] = 0
60-
}
56+
// null terminate, for client's convenience.
57+
ptr[utf8.count] = 0
6158

6259
return BridgedString(data: ptr, length: utf8.count)
6360
}

lib/ASTGen/Sources/ASTGen/Macros.swift

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,7 @@ func checkMacroDefinition(
265265

266266
// Form the "ModuleName.TypeName" result string.
267267
externalMacroOutPtr.pointee =
268-
allocateBridgedString("\(module).\(type)", nullTerminated: true)
268+
allocateBridgedString("\(module).\(type)")
269269

270270
// Translate this into a use of #externalMacro.
271271
let expansionSourceSyntax: ExprSyntax =
@@ -329,14 +329,13 @@ func checkMacroDefinition(
329329

330330
// Form the "ModuleName.TypeName" result string.
331331
externalMacroOutPtr.pointee =
332-
allocateBridgedString("\(module).\(type)", nullTerminated: true)
332+
allocateBridgedString("\(module).\(type)")
333333
return Int(BridgedMacroDefinitionKind.externalMacro.rawValue)
334334

335335
case let .expansion(expansionSyntax, replacements: replacements):
336336
// Provide the expansion syntax.
337337
externalMacroOutPtr.pointee =
338-
allocateBridgedString(expansionSyntax.trimmedDescription,
339-
nullTerminated: true)
338+
allocateBridgedString(expansionSyntax.trimmedDescription)
340339

341340

342341
// If there are no replacements, we're done.
@@ -395,7 +394,7 @@ func makeExpansionOutputResult(
395394
outputPointer.pointee = BridgedString()
396395
return -1
397396
}
398-
outputPointer.pointee = allocateBridgedString(expandedSource, nullTerminated: true)
397+
outputPointer.pointee = allocateBridgedString(expandedSource)
399398
return 0
400399
}
401400

0 commit comments

Comments
 (0)