From f40975e1ce56e6e2e45582013ac356ebcaca0d37 Mon Sep 17 00:00:00 2001 From: Timofey Solomko Date: Wed, 5 Jul 2017 22:15:18 +0300 Subject: [PATCH] Throw error on non-number required tar entry fields Previously, when encountering a required field from tar entry header which cannot be converted to Int SWCompression was silently skipping this problem and proceeding further. Now it throws `fieldIsNotNumber`, consistently with other fields. --- Sources/TarContainer.swift | 32 +++++++++++++------------------- 1 file changed, 13 insertions(+), 19 deletions(-) diff --git a/Sources/TarContainer.swift b/Sources/TarContainer.swift index b94f0000..7c7058ea 100644 --- a/Sources/TarContainer.swift +++ b/Sources/TarContainer.swift @@ -188,31 +188,25 @@ public class TarEntry: ContainerEntry { index += 100 // File mode - if let octalPosixPermissions = Int(try data.nullSpaceEndedAsciiString(index, 8)) { - let posixPermissions = octalToDecimal(octalPosixPermissions) - attributesDict[FileAttributeKey.posixPermissions] = posixPermissions - mode = posixPermissions - } else { - mode = nil - } + guard let octalPosixPermissions = Int(try data.nullSpaceEndedAsciiString(index, 8)) + else { throw TarError.fieldIsNotNumber } + let posixPermissions = octalToDecimal(octalPosixPermissions) + attributesDict[FileAttributeKey.posixPermissions] = posixPermissions + mode = posixPermissions index += 8 // Owner's user ID - if let ownerAccountID = Int(try data.nullSpaceEndedAsciiString(index, 8)) { - attributesDict[FileAttributeKey.ownerAccountID] = ownerAccountID - ownerID = ownerAccountID - } else { - ownerID = nil - } + guard let ownerAccountID = Int(try data.nullSpaceEndedAsciiString(index, 8)) + else { throw TarError.fieldIsNotNumber } + attributesDict[FileAttributeKey.ownerAccountID] = ownerAccountID + ownerID = ownerAccountID index += 8 // Group's user ID - if let groupAccountID = Int(try data.nullSpaceEndedAsciiString(index, 8)) { - attributesDict[FileAttributeKey.groupOwnerAccountID] = groupAccountID - groupID = groupAccountID - } else { - groupID = nil - } + guard let groupAccountID = Int(try data.nullSpaceEndedAsciiString(index, 8)) + else { throw TarError.fieldIsNotNumber } + attributesDict[FileAttributeKey.groupOwnerAccountID] = groupAccountID + groupID = groupAccountID index += 8 // File size