Skip to content

Commit 4596d57

Browse files
andrewjl-muxcjpillsbury
authored andcommitted
Fix potential crash in ChunkedFile (#38)
Convert forced type cast to a conditional type cast and throw error when the latter fails
1 parent 14df53e commit 4596d57

File tree

1 file changed

+3
-6
lines changed

1 file changed

+3
-6
lines changed

Sources/MuxUploadSDK/InternalUtilities/ChunkedFile.swift

+3-6
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,9 @@ class ChunkedFile {
4646
public func openFile(fileURL: URL) throws {
4747
if fileHandle == nil {
4848
do {
49-
let fileSize = try readFileSize(url: fileURL)
49+
guard let fileSize = try FileManager.default.attributesOfItem(atPath: fileURL.path)[FileAttributeKey.size] as? UInt64 else {
50+
throw ChunkedFileError.invalidState("Cannot retrieve file size")
51+
}
5052
self._fileSize = fileSize
5153

5254
let handle = try FileHandle(forReadingFrom: fileURL)
@@ -103,11 +105,6 @@ class ChunkedFile {
103105
return chunk
104106
}
105107

106-
private func readFileSize(url: URL) throws -> UInt64 {
107-
let fileAttr = try FileManager.default.attributesOfItem(atPath: url.path)
108-
return fileAttr[FileAttributeKey.size] as! UInt64
109-
}
110-
111108
/// Creates a ``ChunkedFile`` that wraps the file given by the URL. The file will be opened after calling ``openFile()``
112109
init(chunkSize: Int) {
113110
self.chunkSize = chunkSize

0 commit comments

Comments
 (0)