From 1ea5e961b121f52015a219ce5dba071260c1af3f Mon Sep 17 00:00:00 2001 From: Shinichiro Oba Date: Fri, 14 Jul 2017 08:02:00 +0100 Subject: [PATCH] Add fwrite() and uncompressed_size validation --- Zip/Zip.swift | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/Zip/Zip.swift b/Zip/Zip.swift index 80722dc..c0172b4 100644 --- a/Zip/Zip.swift +++ b/Zip/Zip.swift @@ -180,12 +180,17 @@ public class Zip { unzCloseCurrentFile(zip) ret = unzGoToNextFile(zip) } + + var writeBytes: UInt64 = 0 var filePointer: UnsafeMutablePointer? filePointer = fopen(fullPath, "wb") while filePointer != nil { let readBytes = unzReadCurrentFile(zip, &buffer, bufferSize) if readBytes > 0 { - fwrite(buffer, Int(readBytes), 1, filePointer) + guard fwrite(buffer, Int(readBytes), 1, filePointer) == 1 else { + throw ZipError.unzipFail + } + writeBytes += UInt64(readBytes) } else { break @@ -197,6 +202,9 @@ public class Zip { if crc_ret == UNZ_CRCERROR { throw ZipError.unzipFail } + guard writeBytes == fileInfo.uncompressed_size else { + throw ZipError.unzipFail + } //Set file permissions from current fileInfo if fileInfo.external_fa != 0 { -- 2.26.2