@@ -120,6 +120,12 @@ SoftimageInput::open(const std::string& name, ImageSpec& spec)
120120 close ();
121121 return false ;
122122 }
123+ // Some validity checking
124+ if (curPacket.size != 8 && curPacket.size != 16 ) {
125+ errorfmt (" Unsupported bits per channel {}" , curPacket.size );
126+ close ();
127+ return false ;
128+ }
123129 m_channel_packets.push_back (curPacket);
124130
125131 // Add the number of channels in this packet to nchannels
@@ -135,6 +141,12 @@ SoftimageInput::open(const std::string& name, ImageSpec& spec)
135141 // Set the details in the ImageSpec
136142 m_spec = ImageSpec (m_pic_header.width , m_pic_header.height , nchannels,
137143 chanType);
144+
145+ if (!check_open (m_spec, { 0 , 65535 , 0 , 65535 , 0 , 1 , 0 , 4 })) {
146+ close ();
147+ return false ;
148+ }
149+
138150 m_spec.attribute (" BitsPerSample" , (int )curPacket.size );
139151
140152 m_spec.attribute (" softimage:compression" , Strutil::join (encodings, " ," ));
@@ -333,6 +345,10 @@ SoftimageInput::read_pixels_pure_run_length(
333345 if (fread (&curCount, 1 , 1 , m_fd) != 1 )
334346 return false ;
335347
348+ // Clamp to avoid writing past the end of the scanline buffer
349+ if (linePixelCount + curCount > m_pic_header.width )
350+ curCount = m_pic_header.width - linePixelCount;
351+
336352 if (data) {
337353 // data pointer is set so we're supposed to write data there
338354 size_t pixelSize = pixelChannelSize * channels.size ();
@@ -456,6 +472,10 @@ SoftimageInput::read_pixels_mixed_run_length(
456472 longCount = curCount - 127 ;
457473 }
458474
475+ // Clamp to avoid writing past the end of the scanline buffer
476+ if (linePixelCount + longCount > m_pic_header.width )
477+ longCount = m_pic_header.width - linePixelCount;
478+
459479 if (data) {
460480 // data pointer is set so we're supposed to write data there
461481 size_t pixelSize = pixelChannelSize * channels.size ();
0 commit comments