Skip to content

Commit 38ce15c

Browse files
acts-1631TurboGit
authored andcommitted
imageio: validate RGBE dimensions before decoding
Reject invalid dimensions and products that cannot be represented by the decoder int pixel count or the allocation size. This prevents crafted headers from wrapping the temporary buffer while retaining a positive decode count.
1 parent abc1029 commit 38ce15c

1 file changed

Lines changed: 17 additions & 0 deletions

File tree

‎src/imageio/imageio_rgbe.c‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@
2121
#include "imageio/imageio_rgbe.h"
2222

2323
#include <ctype.h>
24+
#include <limits.h>
2425
#include <math.h>
26+
#include <stdint.h>
2527
#include <stdio.h>
2628
#include <stdlib.h>
2729
#include <string.h>
@@ -448,8 +450,23 @@ dt_imageio_retval_t dt_imageio_open_rgbe(dt_image_t *img,
448450
return DT_IMAGEIO_LOAD_FAILED;
449451
}
450452

453+
if(img->width <= 0 || img->height <= 0
454+
|| (size_t)img->width > SIZE_MAX / (size_t)img->height)
455+
{
456+
fclose(f);
457+
return DT_IMAGEIO_FILE_CORRUPTED;
458+
}
459+
451460
const size_t npixels = (size_t)img->width * img->height;
452461

462+
// RGBE_ReadPixels() uses an int for its pixel count. Also ensure that the
463+
// temporary three-channel float allocation below cannot overflow.
464+
if(npixels > INT_MAX || npixels > SIZE_MAX / (3 * sizeof(float)))
465+
{
466+
fclose(f);
467+
return DT_IMAGEIO_FILE_CORRUPTED;
468+
}
469+
453470
// The decoder writes three RGB channels to rgbe_buf, so size = number of pixels * 3
454471
float *rgbe_buf = dt_alloc_align_float(npixels * 3);
455472
if(!rgbe_buf)

0 commit comments

Comments
 (0)