Skip to content

Fix: handle invalid dataWindow to prevent Python crash (#2023) #2024

New issue

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

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

Already on GitHub? Sign in to your account

Closed
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
31 changes: 28 additions & 3 deletions src/wrappers/python/PyOpenEXR.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1040,9 +1040,34 @@ PyFile::write(const char* outfilename)

for (auto a : P.header)
{
auto name = py::str(a.first);
py::object second = py::cast<py::object>(a.second);
insertAttribute(header, name, second);
std::string name = a.first.cast<std::string> ();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think these if statements are all unnecessary, since they are each cases handled by insertAttribute(), which also handles the various cases like conversion from tuples.

You also seem to have lost the crux of the original fix, which is the comparison of the dataWindow to the part shape.

py::object second = py::cast<py::object> (a.second);

if (name == "compression")
{
header.compression () = second.cast<Compression> ();
}
else if (name == "type")
{
header.setType (second.cast<std::string> ());
}
else if (name == "lineOrder")
{
header.lineOrder () = second.cast<LineOrder> ();
}
else if (name == "tiles")
{
header.setTileDescription (second.cast<TileDescription> ());
}
else if (name == "dataWindow")
{
header.dataWindow () = second.cast<Box2i> ();
}
else
{
// Handle other attributes
insertAttribute (header, name, second);
}
}

//
Expand Down
15 changes: 15 additions & 0 deletions src/wrappers/python/tests/test_exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,21 @@ def test_Part(self):
self.assertEqual(p.type(), OpenEXR.tiledimage)
self.assertEqual(p.compression(), OpenEXR.NO_COMPRESSION)

def test_InvalidDataWindow(self):
"""Test that an invalid dataWindow raises an exception."""

invalid_header = {
"dataWindow": (-5, -5, -1, -1), # Invalid rectangle
"compression": OpenEXR.ZIP_COMPRESSION,
"type": OpenEXR.scanlineimage
}

RGB = np.random.rand(10, 10, 3).astype(np.float32) # Valid shape for data

with self.assertRaises(Exception):
with OpenEXR.File(invalid_header, {"RGB": RGB}) as outfile:
outfile.write("invalid_output.exr")

def test_Channel(self):

with self.assertRaises(Exception):
Expand Down
21 changes: 11 additions & 10 deletions src/wrappers/python/tests/test_readme.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ def test_write_RGB():

# Generate a 3D NumPy array for RGB channels with random values
height, width = (20, 10)
RGB = np.random.rand(height, width, 3).astype('f')

RGB = np.random.rand(height, width, 3).astype(np.float32)
channels = { "RGB" : RGB }
header = { "compression" : OpenEXR.ZIP_COMPRESSION,
"type" : OpenEXR.scanlineimage }
Expand Down Expand Up @@ -122,27 +122,28 @@ def test_multipart_write():

height, width = (20, 10)

Z0 = np.zeros((height, width), dtype='f')
Z0 = np.zeros((height, width), dtype=np.float32)
P0 = OpenEXR.Part(header={"type" : OpenEXR.scanlineimage },
channels={"Z" : Z0 })

Z1 = np.ones((height, width), dtype='f')
Z1 = np.ones((height, width), dtype=np.float32)
P1 = OpenEXR.Part(header={"type" : OpenEXR.scanlineimage },
channels={"Z" : Z1 })

f = OpenEXR.File(parts=[P0, P1])
f.write("readme_2part.exr")
with OpenEXR.File(parts=[P0, P1]) as f:
f.write("readme_2part.exr")

with OpenEXR.File("readme_2part.exr") as o:
assert o.parts[0].name() == "Part0"
assert o.parts[0].type() == OpenEXR.scanlineimage
assert o.parts[0].width() == 10
assert o.parts[0].height() == 20
assert o.parts[0].width() == width
assert o.parts[0].height() == height
assert np.array_equal(o.parts[0].channels["Z"].pixels, Z0)

assert o.parts[1].name() == "Part1"
assert o.parts[1].type() == OpenEXR.scanlineimage
assert o.parts[1].width() == 10
assert o.parts[1].height() == 20
assert o.parts[1].width() == width
assert o.parts[1].height() == height
assert np.array_equal(o.parts[1].channels["Z"].pixels, Z1)

print("ok")
Expand Down
Loading