Skip to content

Commit

Permalink
Don't attempt deflation on empty but marked as 'inflated' entries
Browse files Browse the repository at this point in the history
  • Loading branch information
Ant1-Provot committed Jul 29, 2024
1 parent 72080af commit f5d62ef
Showing 1 changed file with 28 additions and 23 deletions.
51 changes: 28 additions & 23 deletions zip.ml
Original file line number Diff line number Diff line change
Expand Up @@ -401,29 +401,34 @@ let read_entry ifile e =
| Deflated ->
let in_avail = ref e.compressed_size in
let out_pos = ref 0 in
begin try
Zlib.uncompress ~header:false
(fun buf ->
let read = input ifile.if_channel buf 0
(min !in_avail (Bytes.length buf)) in
in_avail := !in_avail - read;
read)
(fun buf len ->
if !out_pos + len > Bytes.length res then
raise (Error(ifile.if_filename, e.filename,
"wrong size for deflated entry (too much data)"));
Bytes.blit buf 0 res !out_pos len;
out_pos := !out_pos + len)
with Zlib.Error(_, _) ->
raise (Error(ifile.if_filename, e.filename, "decompression error"))
end;
if !out_pos <> Bytes.length res then
raise (Error(ifile.if_filename, e.filename,
"wrong size for deflated entry (not enough data)"));
let crc = Zlib.update_crc Int32.zero res 0 (Bytes.length res) in
if crc <> e.crc then
raise (Error(ifile.if_filename, e.filename, "CRC mismatch"));
Bytes.unsafe_to_string res
if e.uncompressed_size = 0 then
(* Empty zip entries may be marked as deflated *)
String.empty
else (
begin try
Zlib.uncompress ~header:false
(fun buf ->
let read = input ifile.if_channel buf 0
(min !in_avail (Bytes.length buf)) in
in_avail := !in_avail - read;
read)
(fun buf len ->
if !out_pos + len > Bytes.length res then
raise (Error(ifile.if_filename, e.filename,
"wrong size for deflated entry (too much data)"));
Bytes.blit buf 0 res !out_pos len;
out_pos := !out_pos + len)
with Zlib.Error(_, _) ->
raise (Error(ifile.if_filename, e.filename, "decompression error"))
end;
if !out_pos <> Bytes.length res then
raise (Error(ifile.if_filename, e.filename,
"wrong size for deflated entry (not enough data)"));
let crc = Zlib.update_crc Int32.zero res 0 (Bytes.length res) in
if crc <> e.crc then
raise (Error(ifile.if_filename, e.filename, "CRC mismatch"));
Bytes.unsafe_to_string res
)
with End_of_file ->
raise (Error(ifile.if_filename, e.filename, "truncated data"))

Expand Down

0 comments on commit f5d62ef

Please sign in to comment.