imagetopnm(): make sure the alpha component has same dimension as other components to avoid read heap buffer overflow (#970)

This commit is contained in:
Even Rouault 2017-07-29 17:28:55 +02:00
parent db9ef99f6d
commit 2fa0fc61f2
1 changed files with 16 additions and 9 deletions

View File

@ -1890,6 +1890,21 @@ opj_image_t* pnmtoimage(const char *filename, opj_cparameters_t *parameters)
return image; return image;
}/* pnmtoimage() */ }/* pnmtoimage() */
static int are_comps_similar(opj_image_t * image)
{
unsigned int i;
for (i = 1; i < image->numcomps; i++) {
if (image->comps[0].dx != image->comps[i].dx ||
image->comps[0].dy != image->comps[i].dy ||
image->comps[0].prec != image->comps[i].prec ||
image->comps[0].sgnd != image->comps[i].sgnd) {
return OPJ_FALSE;
}
}
return OPJ_TRUE;
}
int imagetopnm(opj_image_t * image, const char *outfile, int force_split) int imagetopnm(opj_image_t * image, const char *outfile, int force_split)
{ {
int *red, *green, *blue, *alpha; int *red, *green, *blue, *alpha;
@ -1926,15 +1941,7 @@ int imagetopnm(opj_image_t * image, const char *outfile, int force_split)
} }
if ((force_split == 0) && if ((force_split == 0) &&
(ncomp == 2 /* GRAYA */ are_comps_similar(image)) {
|| (ncomp > 2 /* RGB, RGBA */
&& image->comps[0].dx == image->comps[1].dx
&& image->comps[1].dx == image->comps[2].dx
&& image->comps[0].dy == image->comps[1].dy
&& image->comps[1].dy == image->comps[2].dy
&& image->comps[0].prec == image->comps[1].prec
&& image->comps[1].prec == image->comps[2].prec
))) {
fdest = fopen(outfile, "wb"); fdest = fopen(outfile, "wb");
if (!fdest) { if (!fdest) {