Skip to content

Commit 1ea5e96

Browse files
author
Shinichiro Oba
committed
Add fwrite() and uncompressed_size validation
1 parent 4a15a78 commit 1ea5e96

File tree

1 file changed

+9
-1
lines changed

1 file changed

+9
-1
lines changed

Zip/Zip.swift

+9-1
Original file line numberDiff line numberDiff line change
@@ -180,12 +180,17 @@ public class Zip {
180180
unzCloseCurrentFile(zip)
181181
ret = unzGoToNextFile(zip)
182182
}
183+
184+
var writeBytes: UInt64 = 0
183185
var filePointer: UnsafeMutablePointer<FILE>?
184186
filePointer = fopen(fullPath, "wb")
185187
while filePointer != nil {
186188
let readBytes = unzReadCurrentFile(zip, &buffer, bufferSize)
187189
if readBytes > 0 {
188-
fwrite(buffer, Int(readBytes), 1, filePointer)
190+
guard fwrite(buffer, Int(readBytes), 1, filePointer) == 1 else {
191+
throw ZipError.unzipFail
192+
}
193+
writeBytes += UInt64(readBytes)
189194
}
190195
else {
191196
break
@@ -197,6 +202,9 @@ public class Zip {
197202
if crc_ret == UNZ_CRCERROR {
198203
throw ZipError.unzipFail
199204
}
205+
guard writeBytes == fileInfo.uncompressed_size else {
206+
throw ZipError.unzipFail
207+
}
200208

201209
//Set file permissions from current fileInfo
202210
if fileInfo.external_fa != 0 {

0 commit comments

Comments
 (0)