From 14e511f9018ea87a47e58971481d3d82f72d5462 Mon Sep 17 00:00:00 2001 From: Zorg Date: Sat, 9 Jul 2022 12:22:36 -0700 Subject: [PATCH] Fix memory leaks when using generate_appcast (#2193) We fix one memory leak when calculating sha's, one memory leak in the xar delta applying path, and avoid creating Bundles for reading the Sparkle framework version. There are some other small leaks with the xar delta creation/applying code but since it's a legacy implementation and the APIs are undocumented I don't want to touch it too much. The biggest culprit here is the leak when calculating sha's I believe, if you have many archive items. --- Autoupdate/SPUXarDeltaArchive.m | 6 +++++- generate_appcast/ArchiveItem.swift | 24 +++++++++++++++--------- generate_appcast/URL+Hashing.swift | 4 ++++ 3 files changed, 24 insertions(+), 10 deletions(-) diff --git a/Autoupdate/SPUXarDeltaArchive.m b/Autoupdate/SPUXarDeltaArchive.m index b842f0d5..0e949226 100644 --- a/Autoupdate/SPUXarDeltaArchive.m +++ b/Autoupdate/SPUXarDeltaArchive.m @@ -348,7 +348,11 @@ static xar_file_t _xarAddFile(NSMutableDictionary *fileTa continue; } - NSString *relativePath = @(pathCString); + NSString *relativePath = [[NSString alloc] initWithBytesNoCopy:pathCString length:strlen(pathCString) encoding:NSUTF8StringEncoding freeWhenDone:YES]; + if (relativePath == nil) { + free(pathCString); + continue; + } SPUDeltaItemCommands commands = 0; { diff --git a/generate_appcast/ArchiveItem.swift b/generate_appcast/ArchiveItem.swift index 70440ebd..a9911734 100644 --- a/generate_appcast/ArchiveItem.swift +++ b/generate_appcast/ArchiveItem.swift @@ -138,18 +138,24 @@ class ArchiveItem: CustomStringConvertible { } var frameworkVersion: String? = nil - if let appBundle = Bundle(url: appPath), let frameworksURL = appBundle.privateFrameworksURL { - let sparkleBundleURL = frameworksURL.appendingPathComponent("Sparkle").appendingPathExtension("framework") + do { + let canonicalFrameworksURL = appPath.appendingPathComponent("Contents/Frameworks/Sparkle.framework") - if let sparkleBundle = Bundle(url: sparkleBundleURL), let infoDictionary = sparkleBundle.infoDictionary { - frameworkVersion = infoDictionary[kCFBundleVersionKey as String] as? String - } else { + let frameworksURL: URL? + if !FileManager.default.fileExists(atPath: canonicalFrameworksURL.path) { // Try legacy SparkleCore framework that was shipping in early 2.0 betas - let sparkleCoreBundleURL = frameworksURL.appendingPathComponent("SparkleCore").appendingPathExtension("framework") - - if let sparkleBundle = Bundle(url: sparkleCoreBundleURL), let infoDictionary = sparkleBundle.infoDictionary { - frameworkVersion = infoDictionary[kCFBundleVersionKey as String] as? String + let sparkleCoreFrameworksURL = appPath.appendingPathComponent("Contents/Frameworks/SparkleCore.framework") + if FileManager.default.fileExists(atPath: sparkleCoreFrameworksURL.path) { + frameworksURL = sparkleCoreFrameworksURL + } else { + frameworksURL = nil } + } else { + frameworksURL = canonicalFrameworksURL + } + + if let frameworksURL = frameworksURL, let frameworkInfoPlist = NSDictionary(contentsOf: frameworksURL.appendingPathComponent("Resources/Info.plist")) { + frameworkVersion = frameworkInfoPlist[kCFBundleVersionKey as String] as? String } } diff --git a/generate_appcast/URL+Hashing.swift b/generate_appcast/URL+Hashing.swift index d3bcc18e..e087348b 100644 --- a/generate_appcast/URL+Hashing.swift +++ b/generate_appcast/URL+Hashing.swift @@ -33,6 +33,10 @@ extension FileHandle { CC_SHA256_Final(hash.baseAddress, &context) + defer { + hash.deallocate() + } + return hash.reduce("") { $0 + String(format: "%02x", $1) } }