Skip to content

Commit

Permalink
Merge pull request #2391 from CruGlobal/GT-fuzi-crash-fix-master
Browse files Browse the repository at this point in the history
GT-2478 Fuzi crash fix for next release.
  • Loading branch information
levieggertcru authored Jan 8, 2025
2 parents c8fb73d + e51f23c commit 1297089
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 8 deletions.
16 changes: 11 additions & 5 deletions godtools/App/Share/Data/WebArchiveQueue/HTMLDocumentWrapper.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,28 @@ import Fuzi
import libxml2

// NOTE: Wrapper for addressing bad access starting in Xcode 16.2 Fuzi version 3.1.3. (https://github.com/cezheng/Fuzi/issues/130)
// Will fix by implementing change from open PR (https://github.com/cezheng/Fuzi/pull/131).
// TODO: GT-2492 Once merged we can remove this wrapper. ~Levi

class HTMLDocumentWrapper {

let htmlDocument: HTMLDocument

init(string: String, encoding: String.Encoding = String.Encoding.utf8) throws {

guard let cChars = string.cString(using: encoding) else {
throw XMLError.invalidData
}

let buffer = cChars.withUnsafeBufferPointer { buffer in
UnsafeBufferPointer(rebasing: buffer[0..<buffer.count])
let mutablebuffer = UnsafeMutableBufferPointer<CChar>.allocate(capacity: cChars.count)
_ = mutablebuffer.initialize(from: cChars)

defer {
mutablebuffer.deallocate()
}

// NOTE: Seems to solve bad access at line 130 in Document.swift Fuzi version 3.1.3. "guard let document = type(of: self).parse(buffer: buffer, options: options)" ~Levi

let buffer = UnsafeBufferPointer(mutablebuffer)

let htmlDocument = try HTMLDocument(buffer: buffer)

self.htmlDocument = htmlDocument
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -225,9 +225,12 @@ class WebArchiveOperation: Operation, @unchecked Sendable {
do {
let webArchiveResource: WebArchiveResource = WebArchiveResource(url: url, data: data, mimeType: mimeType)
let mainResource: WebArchiveMainResource = WebArchiveMainResource(baseResource: webArchiveResource)
//let htmlDocument: HTMLDocument = try HTMLDocument(string: htmlString, encoding: .utf8) // NOTE: Was getting a bad access starting in Xcode 16.2 (https://github.com/cezheng/Fuzi/issues/130). ~Levi
let htmlDocumentWrapper = try HTMLDocumentWrapper(string: htmlString, encoding: .utf8)
let resourceUrls: [String] = htmlDocumentWrapper.htmlDocument.getHTMLReferences(host: host, includeJavascript: includeJavascript)

// NOTE: Using HTMLDocumentWrapper for now with open PR fix until merged and Fuzi is updated. ~Levi
// TODO: GT-2492 Remove HTMLDocumentWrapper once PR is merged. ~Levi
//let htmlDocument: HTMLDocument = try HTMLDocument(string: htmlString, encoding: .utf8)
let htmlDocument: HTMLDocument = try HTMLDocumentWrapper(string: htmlString, encoding: .utf8).htmlDocument
let resourceUrls: [String] = htmlDocument.getHTMLReferences(host: host, includeJavascript: includeJavascript)
complete(.success(HTMLDocumentData(mainResource: mainResource, resourceUrls: resourceUrls)))
}
catch let parseHtmlDocumentError {
Expand Down

0 comments on commit 1297089

Please sign in to comment.