Changes in pnmtoimage if image data are missing

This commit is contained in:
szukw000 2018-08-31 16:24:41 +02:00
parent 9d1a9dc20d
commit 98363e244e
1 changed files with 33 additions and 13 deletions

View File

@ -1812,6 +1812,13 @@ opj_image_t* pnmtoimage(const char *filename, opj_cparameters_t *parameters)
return NULL; return NULL;
} }
if (header_info.width == 0
|| header_info.height == 0
|| (header_info.format == 7 && header_info.depth == 0)) {
fclose(fp);
return NULL;
}
/* This limitation could be removed by making sure to use size_t below */ /* This limitation could be removed by making sure to use size_t below */
if (header_info.height != 0 && if (header_info.height != 0 &&
header_info.width > INT_MAX / header_info.height) { header_info.width > INT_MAX / header_info.height) {
@ -1897,8 +1904,10 @@ opj_image_t* pnmtoimage(const char *filename, opj_cparameters_t *parameters)
for (compno = 0; compno < numcomps; compno++) { for (compno = 0; compno < numcomps; compno++) {
index = 0; index = 0;
if (fscanf(fp, "%u", &index) != 1) { if (fscanf(fp, "%u", &index) != 1) {
fprintf(stderr, fprintf(stderr, "Missing data. Quitting.\n");
"\nWARNING: fscanf return a number of element different from the expected.\n"); opj_image_destroy(image);
fclose(fp);
return NULL;
} }
image->comps[compno].data[i] = (OPJ_INT32)(index * 255) / header_info.maxval; image->comps[compno].data[i] = (OPJ_INT32)(index * 255) / header_info.maxval;
@ -1916,8 +1925,7 @@ opj_image_t* pnmtoimage(const char *filename, opj_cparameters_t *parameters)
for (i = 0; i < w * h; i++) { for (i = 0; i < w * h; i++) {
for (compno = 0; compno < numcomps; compno++) { for (compno = 0; compno < numcomps; compno++) {
if (!fread(&c0, 1, 1, fp)) { if (!fread(&c0, 1, 1, fp)) {
fprintf(stderr, fprintf(stderr, "Missing data. Quitting.\n");
"\nError: fread return a number of element different from the expected.\n");
opj_image_destroy(image); opj_image_destroy(image);
fclose(fp); fclose(fp);
return NULL; return NULL;
@ -1926,8 +1934,10 @@ opj_image_t* pnmtoimage(const char *filename, opj_cparameters_t *parameters)
image->comps[compno].data[i] = c0; image->comps[compno].data[i] = c0;
} else { } else {
if (!fread(&c1, 1, 1, fp)) { if (!fread(&c1, 1, 1, fp)) {
fprintf(stderr, fprintf(stderr, "Missing data. Quitting.\n");
"\nError: fread return a number of element different from the expected.\n"); opj_image_destroy(image);
fclose(fp);
return NULL;
} }
/* netpbm: */ /* netpbm: */
image->comps[compno].data[i] = ((c0 << 8) | c1); image->comps[compno].data[i] = ((c0 << 8) | c1);
@ -1939,15 +1949,17 @@ opj_image_t* pnmtoimage(const char *filename, opj_cparameters_t *parameters)
unsigned int index; unsigned int index;
if (fscanf(fp, "%u", &index) != 1) { if (fscanf(fp, "%u", &index) != 1) {
fprintf(stderr, fprintf(stderr, "Missing data. Quitting.\n");
"\nWARNING: fscanf return a number of element different from the expected.\n"); opj_image_destroy(image);
fclose(fp);
return NULL;
} }
image->comps[0].data[i] = (index ? 0 : 255); image->comps[0].data[i] = (index ? 0 : 255);
} }
} else if (format == 4) { } else if (format == 4) {
int x, y, bit; int x, y, bit;
unsigned char uc; int uc;
i = 0; i = 0;
for (y = 0; y < h; ++y) { for (y = 0; y < h; ++y) {
@ -1957,9 +1969,15 @@ opj_image_t* pnmtoimage(const char *filename, opj_cparameters_t *parameters)
for (x = 0; x < w; ++x) { for (x = 0; x < w; ++x) {
if (bit == -1) { if (bit == -1) {
bit = 7; bit = 7;
uc = (unsigned char)getc(fp); uc = getc(fp);
if (uc == EOF) {
fprintf(stderr, "Missing data. Quitting.\n");
opj_image_destroy(image);
fclose(fp);
return NULL;
} }
image->comps[0].data[i] = (((uc >> bit) & 1) ? 0 : 255); }
image->comps[0].data[i] = ((((unsigned char)uc >> bit) & 1) ? 0 : 255);
--bit; --bit;
++i; ++i;
} }
@ -1969,8 +1987,10 @@ opj_image_t* pnmtoimage(const char *filename, opj_cparameters_t *parameters)
for (i = 0; i < w * h; ++i) { for (i = 0; i < w * h; ++i) {
if (!fread(&uc, 1, 1, fp)) { if (!fread(&uc, 1, 1, fp)) {
fprintf(stderr, fprintf(stderr, "Missing data. Quitting.\n");
"\nError: fread return a number of element different from the expected.\n"); opj_image_destroy(image);
fclose(fp);
return NULL;
} }
image->comps[0].data[i] = (uc & 1) ? 0 : 255; image->comps[0].data[i] = (uc & 1) ? 0 : 255;
} }