From d556b37cec83e87e990bc4434dfb4662b51b389d Mon Sep 17 00:00:00 2001 From: Bram Stolk Date: Sun, 29 Sep 2024 08:11:21 -0700 Subject: [PATCH] [image output] Initialize pixels of partial tile conversion buffer. When writing a partial tile, the unused pixels still go through float conversion. This means, that floating point operations are done on uninitialized data. This can easily lead to NaN and to floating point exceptions, if those were to be enabled. This change will set the shared buffer used for all partial tiles to all zero pixels. Tested by running valgrind before and after the change FIXES: #4461 Signed-off-by: Bram Stolk --- src/libOpenImageIO/imageoutput.cpp | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/libOpenImageIO/imageoutput.cpp b/src/libOpenImageIO/imageoutput.cpp index f233b395b8..cddc4e8a9a 100644 --- a/src/libOpenImageIO/imageoutput.cpp +++ b/src/libOpenImageIO/imageoutput.cpp @@ -177,8 +177,13 @@ ImageOutput::write_tiles(int xbegin, int xend, int ybegin, int yend, int zbegin, ok &= write_tile(x, y, z, format, tilestart, xstride, ystride, zstride); } else { - if (!buf.get()) - buf.reset(new char[pixelsize * m_spec.tile_pixels()]); + if (!buf.get()) { + const size_t sz = pixelsize * m_spec.tile_pixels(); + buf.reset(new char[sz]); + // Not all pixels will be initialized, so we set them to zero here. + // This will avoid generation of NaN, FPEs and valgrind errors. + memset(buf.get(), 0, sz); + } OIIO::copy_image(m_spec.nchannels, xw, yh, zd, tilestart, pixelsize, xstride, ystride, zstride, &buf[0], pixelsize,