Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions src/zfile.imageio/zfile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,10 @@ ZfileInput::open(const std::string& name, ImageSpec& newspec)

ZfileHeader header;
static_assert(sizeof(header) == 136, "header size does not match");
gzread(m_gz, &header, sizeof(header));
if (gzread(m_gz, &header, sizeof(header)) != int(sizeof(header))) {
errorfmt("Not a valid Zfile (truncated header)");
return false;
}

if (header.magic != zfile_magic && header.magic != zfile_magic_endian) {
errorfmt("Not a valid Zfile");
Expand Down Expand Up @@ -245,7 +248,12 @@ ZfileInput::read_native_scanline(int subimage, int miplevel, int y, int /*z*/,
}
while (m_next_scanline <= y) {
// Keep reading until we're read the scanline we really need
gzread(m_gz, data, m_spec.width * sizeof(float));
int want = int(m_spec.width * sizeof(float));
if (gzread(m_gz, data, want) != want) {
errorfmt("Error reading zfile scanline {} (corrupt or truncated)",
m_next_scanline);
return false;
}
++m_next_scanline;
}
if (m_swab)
Expand Down
6 changes: 6 additions & 0 deletions testsuite/zfile/ref/out-macarm.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,11 @@ out.zfile : 64 x 64, 1 channel, float zfile
Stats FiniteCount: 4096
Constant: No
Monochrome: Yes
oiiotool ERROR: read : "src/bomb-32767.zfile": zfile header claims a 4095 MB image from a 136 byte file; probably a corrupt or malicious header
Full command line was:
> oiiotool -nostderr -info src/bomb-32767.zfile
oiiotool ERROR: read : "src/truncated.zfile": Error reading zfile scanline 0 (corrupt or truncated)
Full command line was:
> oiiotool -nostderr src/truncated.zfile -o truncated.exr
Comparing "out.zfile" and "ref/out.zfile"
PASS
6 changes: 6 additions & 0 deletions testsuite/zfile/ref/out.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,11 @@ out.zfile : 64 x 64, 1 channel, float zfile
Stats FiniteCount: 4096
Constant: No
Monochrome: Yes
oiiotool ERROR: read : "src/bomb-32767.zfile": zfile header claims a 4095 MB image from a 136 byte file; probably a corrupt or malicious header
Full command line was:
> oiiotool -nostderr -info src/bomb-32767.zfile
oiiotool ERROR: read : "src/truncated.zfile": Error reading zfile scanline 0 (corrupt or truncated)
Full command line was:
> oiiotool -nostderr src/truncated.zfile -o truncated.exr
Comparing "out.zfile" and "ref/out.zfile"
PASS
7 changes: 7 additions & 0 deletions testsuite/zfile/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,11 @@
command += oiiotool ("-pattern fill:topleft=0.1:topright=0.5:bottomleft=1.0:bottomright=0.3 64x64 1 -chnames Z -d float -o out.zfile")
command += info_command ("out.zfile", extraargs="-stats")

# A 136-byte header declaring a 32767x32767 (~4 GB) image: the
# compression-ratio guard must reject it before the caller allocates.
command += oiiotool ("-nostderr -info src/bomb-32767.zfile", failureok=True)
# Truncated gzip pixel data: the scanline read must hard-error rather than
# return an uninitialized buffer.
command += oiiotool ("-nostderr src/truncated.zfile -o truncated.exr", failureok=True)

outputs = [ "out.zfile", "out.txt" ]
Binary file added testsuite/zfile/src/bomb-32767.zfile
Binary file not shown.
Binary file added testsuite/zfile/src/truncated.zfile
Binary file not shown.
Loading