Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

fix: broken pnm files with invalid resolution #4561

Merged
merged 1 commit into from
Dec 6, 2024
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
3 changes: 2 additions & 1 deletion src/libutil/strutil.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1669,8 +1669,9 @@ Strutil::stoi(string_view str, size_t* pos, int base)
}
if (c >= base)
break;
acc = acc * base + c;
anydigits = true;
if (OIIO_LIKELY(!overflow))
acc = acc * base + c;
if (OIIO_UNLIKELY(acc > maxval))
overflow = true;
}
Expand Down
1 change: 1 addition & 0 deletions src/libutil/strutil_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -891,6 +891,7 @@ test_numeric_conversion()
OIIO_CHECK_EQUAL(Strutil::stoi("-12345678901234567890"),
std::numeric_limits<int>::min());
OIIO_CHECK_EQUAL(Strutil::stoi("0x100", nullptr, 16), 256); // hex
OIIO_CHECK_EQUAL(Strutil::stoi("25555555555555555551"), 2147483647);

OIIO_CHECK_EQUAL(Strutil::stoui("hi"), 0);
OIIO_CHECK_EQUAL(Strutil::stoui(" "), 0);
Expand Down
3 changes: 3 additions & 0 deletions src/pnm.imageio/pnminput.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,9 @@ PNMInput::open(const std::string& name, ImageSpec& newspec)
if (!read_file_header())
return false;

if (!check_open(m_spec)) // check for apparently invalid values
return false;

newspec = m_spec;
return true;
}
Expand Down
3 changes: 3 additions & 0 deletions testsuite/pnm/ref/out.txt
Original file line number Diff line number Diff line change
Expand Up @@ -69,3 +69,6 @@ Reading ../oiio-images/pnm/test-3.pfm
oiio:ColorSpace: "Rec709"
pnm:bigendian: 1
pnm:binary: 1
oiiotool ERROR: read : "src/bad-4553.pgm": pnm image resolution may not exceed 65535x65535, but the file appears to be 2147483647x255. Possible corrupt input?
Full command line was:
> oiiotool --info -v -a --hash --oiioattrib try_all_readers 0 --printstats src/bad-4553.pgm
7 changes: 7 additions & 0 deletions testsuite/pnm/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
# SPDX-License-Identifier: Apache-2.0
# https://github.com/AcademySoftwareFoundation/OpenImageIO

redirect = ' >> out.txt 2>&1 '

imagedir = OIIO_TESTSUITE_IMAGEDIR + "/pnm"

for f in [ "bw-ascii.pbm", "bw-binary.pbm",
Expand All @@ -16,3 +18,8 @@
for f in files:
command += info_command (imagedir + "/" + f,
safematch=True, hash=True)

# Damaged files
files = [ "src/bad-4553.pgm" ]
for f in files:
command += info_command (f, extraargs="--oiioattrib try_all_readers 0 --printstats", failureok=True)
4 changes: 4 additions & 0 deletions testsuite/pnm/src/bad-4553.pgm
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
P1
25555555555555555551
255
þ
Loading