summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorEven Rouault <even.rouault@spatialys.com>2017-08-19 15:45:54 +0200
committerEven Rouault <even.rouault@spatialys.com>2017-08-19 15:45:54 +0200
commit3eed024eb45534f86537404e08d08fa572a40782 (patch)
tree038a9641aadb7b779deb5ff3a12f957fbf0f1fee /src
parente5285319229a5d77bf316bb0d3a6cbd3cb8666d9 (diff)
pgxtoimage(): avoid excessive memory allocation attempt (#999)
Diffstat (limited to 'src')
-rw-r--r--src/bin/jp2/convert.c24
1 files changed, 24 insertions, 0 deletions
diff --git a/src/bin/jp2/convert.c b/src/bin/jp2/convert.c
index e606c9be..d7306fd8 100644
--- a/src/bin/jp2/convert.c
+++ b/src/bin/jp2/convert.c
@@ -1163,6 +1163,7 @@ opj_image_t* pgxtoimage(const char *filename, opj_cparameters_t *parameters)
opj_image_cmptparm_t cmptparm; /* maximum of 1 component */
opj_image_t * image = NULL;
int adjustS, ushift, dshift, force8;
+ OPJ_UINT64 expected_file_size;
char endian1, endian2, sign;
char signtmp[32];
@@ -1213,6 +1214,29 @@ opj_image_t* pgxtoimage(const char *filename, opj_cparameters_t *parameters)
return NULL;
}
+ if (w < 1 || h < 1 || prec < 1 || prec > 31) {
+ fclose(f);
+ fprintf(stderr, "Bad pgx header, please check input file\n");
+ return NULL;
+ }
+
+ expected_file_size =
+ (OPJ_UINT64)w * (OPJ_UINT64)h * (prec > 16 ? 4 : prec > 8 ? 2 : 1);
+ if (expected_file_size > 10000000U) {
+ char ch;
+ long curpos = ftell(f);
+ if (expected_file_size > (OPJ_UINT64)INT_MAX) {
+ expected_file_size = (OPJ_UINT64)INT_MAX;
+ }
+ fseek(f, (long)expected_file_size - 1, SEEK_SET);
+ if (fread(&ch, 1, 1, f) != 1) {
+ fprintf(stderr, "File too short\n");
+ fclose(f);
+ return NULL;
+ }
+ fseek(f, curpos, SEEK_SET);
+ }
+
/* initialize image component */
cmptparm.x0 = (OPJ_UINT32)parameters->image_offset_x0;