From 0171677513f3f4f4852a1d2bf9a62a99b7b2dbef Mon Sep 17 00:00:00 2001 From: Jake Petroules Date: Fri, 22 Aug 2014 23:13:44 -0400 Subject: [PATCH] Fix Clang static analyzer warning. Closes #385 --- Sparkle.xcodeproj/project.pbxproj | 4 ++++ Sparkle/SUBinaryDeltaCommon.m | 25 +++++++++++++++---------- Tests/SUBinaryDeltaTest.m | 28 ++++++++++++++++++++++++++++ 3 files changed, 47 insertions(+), 10 deletions(-) create mode 100644 Tests/SUBinaryDeltaTest.m diff --git a/Sparkle.xcodeproj/project.pbxproj b/Sparkle.xcodeproj/project.pbxproj index 68767214..bab43f38 100644 --- a/Sparkle.xcodeproj/project.pbxproj +++ b/Sparkle.xcodeproj/project.pbxproj @@ -30,6 +30,7 @@ 142E0E0019A6954400E4312B /* Sparkle.framework in Copy Frameworks */ = {isa = PBXBuildFile; fileRef = 8DC2EF5B0486A6940098B216 /* Sparkle.framework */; settings = {ATTRIBUTES = (CodeSignOnCopy, RemoveHeadersOnCopy, ); }; }; 142E0E0219A6A14700E4312B /* Sparkle.framework in CopyFiles */ = {isa = PBXBuildFile; fileRef = 8DC2EF5B0486A6940098B216 /* Sparkle.framework */; settings = {ATTRIBUTES = (CodeSignOnCopy, RemoveHeadersOnCopy, ); }; }; 142E0E0419A6A26F00E4312B /* Autoupdate.app in CopyFiles */ = {isa = PBXBuildFile; fileRef = 55C14BB7136EEF1500649790 /* Autoupdate.app */; settings = {ATTRIBUTES = (RemoveHeadersOnCopy, ); }; }; + 142E0E0919A83AAC00E4312B /* SUBinaryDeltaTest.m in Sources */ = {isa = PBXBuildFile; fileRef = 142E0E0819A83AAC00E4312B /* SUBinaryDeltaTest.m */; }; 14732BC01960F2C200593899 /* test_app_only_dsa_pub.pem in Resources */ = {isa = PBXBuildFile; fileRef = 14732BBF1960F0AC00593899 /* test_app_only_dsa_pub.pem */; }; 14732BD019610A0D00593899 /* Foundation.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 0867D69BFE84028FC02AAC07 /* Foundation.framework */; }; 14732BD119610A1200593899 /* AppKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 0867D6A5FE840307C02AAC07 /* AppKit.framework */; }; @@ -300,6 +301,7 @@ 1420DF4D196247B500203BB0 /* build-docs.sh */ = {isa = PBXFileReference; lastKnownFileType = text.script.sh; path = "build-docs.sh"; sourceTree = ""; }; 1420DF4E196247B500203BB0 /* Doxyfile */ = {isa = PBXFileReference; lastKnownFileType = text; path = Doxyfile; sourceTree = ""; }; 1420DF4F196247F900203BB0 /* Images.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Images.xcassets; sourceTree = ""; }; + 142E0E0819A83AAC00E4312B /* SUBinaryDeltaTest.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = SUBinaryDeltaTest.m; path = Tests/SUBinaryDeltaTest.m; sourceTree = ""; }; 146EC84E19A68CF8004A50C5 /* Sparkle.podspec */ = {isa = PBXFileReference; lastKnownFileType = text; path = Sparkle.podspec; sourceTree = SOURCE_ROOT; }; 14732BB1195FF6B700593899 /* .clang-format */ = {isa = PBXFileReference; lastKnownFileType = text; path = ".clang-format"; sourceTree = SOURCE_ROOT; }; 14732BB91960EEEE00593899 /* SampleAppcast.xml */ = {isa = PBXFileReference; lastKnownFileType = text.xml; path = SampleAppcast.xml; sourceTree = ""; }; @@ -779,6 +781,7 @@ isa = PBXGroup; children = ( 612279DA0DB5470200AB99EA /* SparkleTests-Info.plist */, + 142E0E0819A83AAC00E4312B /* SUBinaryDeltaTest.m */, 5AF9DC3B1981DBEE001EA135 /* SUDSAVerifierTest.m */, 14950074195FDF5900BC5B5B /* SUUpdaterTest.m */, 61227A150DB548B800AB99EA /* SUVersionComparisonTest.m */, @@ -1334,6 +1337,7 @@ buildActionMask = 2147483647; files = ( 5AF9DC3C1981DBEE001EA135 /* SUDSAVerifierTest.m in Sources */, + 142E0E0919A83AAC00E4312B /* SUBinaryDeltaTest.m in Sources */, 14950075195FDF5900BC5B5B /* SUUpdaterTest.m in Sources */, 61227A160DB548B800AB99EA /* SUVersionComparisonTest.m in Sources */, ); diff --git a/Sparkle/SUBinaryDeltaCommon.m b/Sparkle/SUBinaryDeltaCommon.m index 1caf7019..78ba2606 100644 --- a/Sparkle/SUBinaryDeltaCommon.m +++ b/Sparkle/SUBinaryDeltaCommon.m @@ -39,23 +39,28 @@ NSString *pathRelativeToDirectory(NSString *directory, NSString *path) } NSString *stringWithFileSystemRepresentation(const char *input) { - NSFileManager *fm = [NSFileManager defaultManager]; - return [fm stringWithFileSystemRepresentation:input length:strlen(input)]; + return [[NSFileManager defaultManager] stringWithFileSystemRepresentation:input length:strlen(input)]; } NSString *temporaryFilename(NSString *base) { NSString *template = [NSTemporaryDirectory() stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.XXXXXXXXXX", base]]; - const char *fsrepr = [template fileSystemRepresentation]; + NSMutableData *data = [NSMutableData data]; + [data appendBytes:template.fileSystemRepresentation length:strlen(template.fileSystemRepresentation) + 1]; - const size_t buffer_len = strlen(fsrepr) + 1; - char *buffer = (char *)malloc(buffer_len); - strlcpy(buffer, fsrepr, buffer_len); + char *buffer = data.mutableBytes; + int fd = mkstemp(buffer); + if (fd == -1) { + perror("mkstemp"); + return nil; + } - // mkstemp() can't be used, beause it returns a file descriptor, and XAR API requires a filename - NSString *ret = stringWithFileSystemRepresentation(mktemp(buffer)); - free(buffer); - return ret; + if (close(fd) != 0) { + perror("close"); + return nil; + } + + return stringWithFileSystemRepresentation(buffer); } static void _hashOfBuffer(unsigned char *hash, const char* buffer, ssize_t bufferLength) diff --git a/Tests/SUBinaryDeltaTest.m b/Tests/SUBinaryDeltaTest.m new file mode 100644 index 00000000..c4770f7d --- /dev/null +++ b/Tests/SUBinaryDeltaTest.m @@ -0,0 +1,28 @@ +// +// SUBinaryDeltaTest.m +// Sparkle +// +// Created by Jake Petroules on 2014-08-22. +// Copyright (c) 2014 Sparkle Project. All rights reserved. +// + +#import +#import +#import "SUBinaryDeltaCommon.h" + +@interface SUBinaryDeltaTest : XCTestCase + +@end + +@implementation SUBinaryDeltaTest + +- (void)testTemporaryFilename +{ + NSString *tmp1 = temporaryFilename(@"Sparkle"); + NSString *tmp2 = temporaryFilename(@"Sparkle"); + NSLog(@"Temporary filenames: %@, %@", tmp1, tmp2); + XCTAssertNotEqualObjects(tmp1, tmp2); + XCTAssert(YES, @"Pass"); +} + +@end