Skip to content

Commit c562730

Browse files
successfully avoided bad pointer dereference when using closures
1 parent 78b9cfe commit c562730

File tree

7 files changed

+20
-15
lines changed

7 files changed

+20
-15
lines changed

Sources/HTMLKitMacros/EscapeHTML.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ enum EscapeHTML : ExpressionMacro {
1414
static func expansion(of node: some FreestandingMacroExpansionSyntax, in context: some MacroExpansionContext) throws -> ExprSyntax {
1515
var c = HTMLExpansionContext(
1616
context: context,
17-
expansion: node.as(ExprSyntax.self)!.macroExpansion!,
17+
expansion: node,
1818
ignoresCompilerWarnings: false,
1919
encoding: .string,
2020
key: "",

Sources/HTMLKitMacros/HTMLElement.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,6 @@ import SwiftSyntaxMacros
1414
enum HTMLElementMacro : ExpressionMacro {
1515
static func expansion(of node: some FreestandingMacroExpansionSyntax, in context: some MacroExpansionContext) throws -> ExprSyntax {
1616
let ignoresCompilerWarnings:Bool = node.macroName.text == "uncheckedHTML"
17-
return try HTMLKitUtilities.expandHTMLMacro(context: HTMLExpansionContext(context: context, expansion: node.as(ExprSyntax.self)!.macroExpansion!, ignoresCompilerWarnings: ignoresCompilerWarnings, encoding: .string, key: "", arguments: node.arguments))
17+
return try HTMLKitUtilities.expandHTMLMacro(context: HTMLExpansionContext(context: context, expansion: node, ignoresCompilerWarnings: ignoresCompilerWarnings, encoding: .string, key: "", arguments: node.arguments))
1818
}
1919
}

Sources/HTMLKitMacros/RawHTML.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ enum RawHTML : ExpressionMacro {
1414
static func expansion(of node: some FreestandingMacroExpansionSyntax, in context: some MacroExpansionContext) throws -> ExprSyntax {
1515
var c = HTMLExpansionContext(
1616
context: context,
17-
expansion: node.as(ExprSyntax.self)!.macroExpansion!,
17+
expansion: node,
1818
ignoresCompilerWarnings: false,
1919
encoding: .string,
2020
key: "",

Sources/HTMLKitParse/ParseData.swift

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ extension HTMLKitUtilities {
6464
let (string, encoding):(String, HTMLEncoding) = expandMacro(context: context)
6565
return "\(raw: encodingResult(context: context, node: context.expansion, string: string, for: encoding))"
6666
}
67-
private static func encodingResult(context: HTMLExpansionContext, node: MacroExpansionExprSyntax, string: String, for encoding: HTMLEncoding) -> String {
67+
private static func encodingResult(context: HTMLExpansionContext, node: FreestandingMacroExpansionSyntax, string: String, for encoding: HTMLEncoding) -> String {
6868
func hasNoInterpolation() -> Bool {
6969
let hasInterpolation:Bool = !string.ranges(of: try! Regex("\\((.*)\\)")).isEmpty
7070
guard !hasInterpolation else {
@@ -152,10 +152,12 @@ extension HTMLKitUtilities {
152152
}
153153
}
154154
if let statements = context.expansion.trailingClosure?.statements {
155+
var c = context
156+
c.expansion.trailingClosure = nil
155157
for statement in statements {
156158
switch statement.item {
157159
case .expr(let expr):
158-
if let inner_html = parseInnerHTML(context: context, expr: expr) {
160+
if let inner_html = parseInnerHTML(context: c, expr: expr) {
159161
innerHTML.append(inner_html)
160162
}
161163
default:

Sources/HTMLKitParse/extensions/HTMLElementValueType.swift

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,19 @@ extension HTMLElementValueType {
1919
context: HTMLExpansionContext,
2020
_ function: FunctionCallExprSyntax
2121
) -> HTMLElement? {
22-
var c = context
2322
let calledExpression = function.calledExpression
2423
let key:String
25-
if let member = calledExpression.memberAccess, member.base?.declRef?.baseName.text == "HTMLKit" {
24+
switch calledExpression.kind {
25+
case .memberAccessExpr:
26+
let member = calledExpression.memberAccess!
27+
guard member.base?.declRef?.baseName.text == "HTMLKit" else { return nil }
2628
key = member.declName.baseName.text
27-
} else if let ref = calledExpression.declRef {
28-
key = ref.baseName.text
29-
} else {
29+
case .declReferenceExpr:
30+
key = calledExpression.declRef!.baseName.text
31+
default:
3032
return nil
3133
}
34+
var c = context
3235
c.arguments = function.arguments
3336
switch key {
3437
case "a": return get(c, a.self)

Sources/HTMLKitUtilities/HTMLExpansionContext.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import SwiftSyntaxMacros
1414
public struct HTMLExpansionContext : @unchecked Sendable {
1515
#if canImport(SwiftSyntax) && canImport(SwiftSyntaxMacros)
1616
public let context:MacroExpansionContext
17-
public let expansion:MacroExpansionExprSyntax
17+
public package(set) var expansion:FreestandingMacroExpansionSyntax
1818
public var arguments:LabeledExprListSyntax
1919
#endif
2020

@@ -35,7 +35,7 @@ public struct HTMLExpansionContext : @unchecked Sendable {
3535

3636
public init(
3737
context: MacroExpansionContext,
38-
expansion: MacroExpansionExprSyntax,
38+
expansion: FreestandingMacroExpansionSyntax,
3939
ignoresCompilerWarnings: Bool,
4040
encoding: HTMLEncoding,
4141
key: String,

Tests/HTMLKitTests/ElementTests.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,17 @@ struct ElementTests {
1414
// MARK: html
1515
@Test func elementHTML() {
1616
var expected:String = "<!DOCTYPE html><html></html>"
17-
var string:StaticString = #html(html())
17+
var string:String = #html(html())
1818
#expect(string == expected)
1919

2020
expected = "<!DOCTYPE html><html xmlns=\"test\"></html>"
2121
string = #html(html(xmlns: "test"))
2222
#expect(string == expected)
2323

24-
/*string = #html {
24+
string = #html {
2525
html(xmlns: "test")
2626
}
27-
#expect(string == expected)*/
27+
#expect(string == expected)
2828
}
2929

3030
// MARK: HTMLKit.<element>

0 commit comments

Comments
 (0)