@@ -288,6 +288,9 @@ default_decompress_chunk (exr_decode_pipeline_t* decode)
288288 uint64_t sampsize =
289289 (((uint64_t ) decode -> chunk .width ) *
290290 ((uint64_t ) decode -> chunk .height ));
291+
292+ if ((decode -> decode_flags & EXR_DECODE_SAMPLE_COUNTS_AS_INDIVIDUAL ))
293+ sampsize += 1 ;
291294 sampsize *= sizeof (int32_t );
292295
293296 rv = decompress_data (
@@ -340,7 +343,7 @@ unpack_sample_table (
340343 exr_result_t rv = EXR_ERR_SUCCESS ;
341344 int32_t w = decode -> chunk .width ;
342345 int32_t h = decode -> chunk .height ;
343- int32_t totsamp = 0 ;
346+ uint64_t totsamp = 0 ;
344347 int32_t * samptable = decode -> sample_count_table ;
345348 size_t combSampSize = 0 ;
346349
@@ -351,38 +354,44 @@ unpack_sample_table (
351354 {
352355 for (int32_t y = 0 ; y < h ; ++ y )
353356 {
357+ int32_t * cursampline = samptable + y * w ;
354358 int32_t prevsamp = 0 ;
355359 for (int32_t x = 0 ; x < w ; ++ x )
356360 {
357361 int32_t nsamps =
358- (int32_t ) one_to_native32 ((uint32_t ) samptable [y * w + x ]);
359- if (nsamps < 0 ) return EXR_ERR_INVALID_SAMPLE_DATA ;
360- samptable [y * w + x ] = nsamps - prevsamp ;
361- prevsamp = nsamps ;
362+ (int32_t ) one_to_native32 ((uint32_t ) cursampline [x ]);
363+ if (nsamps < prevsamp ) return EXR_ERR_INVALID_SAMPLE_DATA ;
364+
365+ cursampline [x ] = nsamps - prevsamp ;
366+ prevsamp = nsamps ;
362367 }
363- totsamp += prevsamp ;
368+ totsamp += ( uint64_t ) prevsamp ;
364369 }
365- samptable [w * h ] = totsamp ;
370+ if (totsamp >= (uint64_t )INT32_MAX )
371+ return EXR_ERR_INVALID_SAMPLE_DATA ;
372+ samptable [w * h ] = (int32_t )totsamp ;
366373 }
367374 else
368375 {
369376 for (int32_t y = 0 ; y < h ; ++ y )
370377 {
378+ int32_t * cursampline = samptable + y * w ;
371379 int32_t prevsamp = 0 ;
372380 for (int32_t x = 0 ; x < w ; ++ x )
373381 {
374382 int32_t nsamps =
375- (int32_t ) one_to_native32 ((uint32_t ) samptable [y * w + x ]);
376- if (nsamps < 0 ) return EXR_ERR_INVALID_SAMPLE_DATA ;
377- samptable [y * w + x ] = nsamps ;
378- prevsamp = nsamps ;
383+ (int32_t ) one_to_native32 ((uint32_t ) cursampline [x ]);
384+ if (nsamps < prevsamp ) return EXR_ERR_INVALID_SAMPLE_DATA ;
385+
386+ cursampline [x ] = nsamps ;
387+ prevsamp = nsamps ;
379388 }
380- totsamp += prevsamp ;
389+
390+ totsamp += (uint64_t )prevsamp ;
381391 }
382392 }
383393
384- if (totsamp < 0 ||
385- (((uint64_t ) totsamp ) * combSampSize ) > decode -> chunk .unpacked_size )
394+ if ((totsamp * combSampSize ) > decode -> chunk .unpacked_size )
386395 {
387396 rv = pctxt -> report_error (
388397 pctxt , EXR_ERR_INVALID_SAMPLE_DATA , "Corrupt sample count table" );
0 commit comments