Skip to content

Commit

Permalink
opj_decompress: add sanity checks to avoid segfault in case of decodi…
Browse files Browse the repository at this point in the history
…ng error

Prevent crashes like:
opj_decompress -i 0722_5-1_2019.jp2 -o out.ppm -r 4 -t 0

where 0722_5-1_2019.jp2 is
https://drive.google.com/file/d/1ZxOUZg2-FKjYwa257VFLMpTXRWxEoP0a/view?usp=sharing
  • Loading branch information
rouault committed Apr 1, 2020
1 parent 563ecfb commit dc0d563
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 0 deletions.
16 changes: 16 additions & 0 deletions src/bin/jp2/convert.c
Original file line number Diff line number Diff line change
Expand Up @@ -2067,10 +2067,26 @@ int imagetopnm(opj_image_t * image, const char *outfile, int force_split)
has_alpha = (ncomp == 4 || ncomp == 2);

red = image->comps[0].data;
if (red == NULL) {
fprintf(stderr,
"imagetopnm: planes[%d] == NULL.\n", 0);
fprintf(stderr, "\tAborting\n");
fclose(fdest);
return fails;
}

if (triple) {
green = image->comps[1].data;
blue = image->comps[2].data;
for (i = 1; i <= 2; i++) {
if (image->comps[i].data == NULL) {
fprintf(stderr,
"imagetopnm: planes[%d] == NULL.\n", i);
fprintf(stderr, "\tAborting\n");
fclose(fdest);
return fails;
}
}
} else {
green = blue = NULL;
}
Expand Down
6 changes: 6 additions & 0 deletions src/bin/jp2/convertpng.c
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,12 @@ int imagetopng(opj_image_t * image, const char *write_idf)
break;
}
planes[i] = image->comps[i].data;
if (planes[i] == NULL) {
fprintf(stderr,
"imagetopng: planes[%d] == NULL.\n", i);
fprintf(stderr, "\tAborting\n");
return 1;
}
}
if (i != nr_comp) {
fprintf(stderr,
Expand Down
6 changes: 6 additions & 0 deletions src/bin/jp2/converttif.c
Original file line number Diff line number Diff line change
Expand Up @@ -616,6 +616,12 @@ int imagetotif(opj_image_t * image, const char *outfile)
break;
}
planes[i] = image->comps[i].data;
if (planes[i] == NULL) {
fprintf(stderr,
"imagetotif: planes[%d] == NULL.\n", i);
fprintf(stderr, "\tAborting\n");
return 1;
}
}
if (i != numcomps) {
fprintf(stderr,
Expand Down

0 comments on commit dc0d563

Please sign in to comment.