@@ -214,6 +214,7 @@ class PSDInput final : public ImageInput {
214214 std::vector<std::string> m_alpha_names;
215215 // Index of the transparent color, if any (for Indexed color mode only)
216216 int16_t m_transparency_index;
217+ bool m_has_transparency_index;
217218 // Background color
218219 float m_background_color[4 ];
219220 // /< Do not convert unassociated alpha
@@ -912,6 +913,7 @@ PSDInput::init()
912913 m_channels.clear ();
913914 m_alpha_names.clear ();
914915 m_transparency_index = -1 ;
916+ m_has_transparency_index = false ;
915917 m_keep_unassociated_alpha = false ;
916918 m_background_color[0 ] = 1.0 ;
917919 m_background_color[1 ] = 1.0 ;
@@ -1262,15 +1264,23 @@ PSDInput::load_resource_1039(uint32_t length)
12621264
12631265
12641266bool
1265- PSDInput::load_resource_1047 (uint32_t /* length*/ )
1267+ PSDInput::load_resource_1047 (uint32_t length)
12661268{
1269+ if (length != sizeof (m_transparency_index)) {
1270+ errorfmt (" [Image Resource] Transparency index length {} is invalid" ,
1271+ length);
1272+ return false ;
1273+ }
1274+
12671275 if (!read_bige<int16_t >(m_transparency_index))
12681276 return false ;
1269- if (m_transparency_index < 0 || m_transparency_index >= 768 ) {
1277+ if (m_transparency_index < 0 || m_transparency_index >= 256 ) {
12701278 errorfmt (" [Image Resource] Transparency index {} is out of range" ,
12711279 m_transparency_index);
12721280 return false ;
12731281 }
1282+
1283+ m_has_transparency_index = true ;
12741284 return true ;
12751285}
12761286
@@ -1980,7 +1990,7 @@ PSDInput::setup()
19801990 spec_channel_count++;
19811991 raw_channel_count++;
19821992 } else if (m_header.color_mode == ColorMode_Indexed
1983- && m_transparency_index ) {
1993+ && m_has_transparency_index ) {
19841994 spec_channel_count++;
19851995 }
19861996 }
@@ -2144,7 +2154,11 @@ PSDInput::indexed_to_rgb(span<unsigned char> dst, cspan<unsigned char> src,
21442154 OIIO_ASSERT (src.size () && dst.size ());
21452155 // The color table is 768 bytes which is 256 * 3 channels (always RGB)
21462156 const auto & table (m_color_data.data );
2147- if (m_transparency_index >= 0 ) {
2157+ if (src.size () < span_size_t (width))
2158+ return false ;
2159+ if (m_has_transparency_index) {
2160+ if (dst.size () < span_size_t (width) * 4 )
2161+ return false ;
21482162 for (int i = 0 ; i < width; ++i) {
21492163 int index = src[i];
21502164 if (index == m_transparency_index) {
@@ -2160,6 +2174,8 @@ PSDInput::indexed_to_rgb(span<unsigned char> dst, cspan<unsigned char> src,
21602174 }
21612175 }
21622176 } else {
2177+ if (dst.size () < span_size_t (width) * 3 )
2178+ return false ;
21632179 for (int i = 0 ; i < width; ++i) {
21642180 int index = src[i];
21652181 dst[3 * i + 0 ] = table[index]; // R
0 commit comments