X-Git-Url: https://main.carlh.net/gitweb/?p=openjpeg.git;a=blobdiff_plain;f=src%2Fbin%2Fjpwl%2Fconvert.c;h=e6feea50124a7dc3f1cd848350729faa70a61ede;hp=f3bb670b0a14c230d721862bc447dbf29179ac06;hb=9701b3305db58d35e4446946309f88937e2f5342;hpb=10d22ec26d864e93ad92b4570f044258c6c5bdf9 diff --git a/src/bin/jpwl/convert.c b/src/bin/jpwl/convert.c index f3bb670b..e6feea50 100644 --- a/src/bin/jpwl/convert.c +++ b/src/bin/jpwl/convert.c @@ -41,6 +41,7 @@ #include #include #include +#include #ifdef OPJ_HAVE_LIBTIFF #include @@ -444,7 +445,7 @@ int imagetotga(opj_image_t * image, const char *outfile) { int width, height, bpp, x, y; opj_bool write_alpha; - int i, adjustR, adjustG, adjustB; + int i, adjustR, adjustG = 0, adjustB = 0; unsigned int alpha_channel; float r, g, b, a; unsigned char value; @@ -485,8 +486,10 @@ int imagetotga(opj_image_t * image, const char *outfile) scale = 255.0f / (float)((1 << image->comps[0].prec) - 1); adjustR = (image->comps[0].sgnd ? 1 << (image->comps[0].prec - 1) : 0); - adjustG = (image->comps[1].sgnd ? 1 << (image->comps[1].prec - 1) : 0); - adjustB = (image->comps[2].sgnd ? 1 << (image->comps[2].prec - 1) : 0); + if (image->numcomps >= 3) { + adjustG = (image->comps[1].sgnd ? 1 << (image->comps[1].prec - 1) : 0); + adjustB = (image->comps[2].sgnd ? 1 << (image->comps[2].prec - 1) : 0); + } for (y = 0; y < height; y++) { unsigned int index = y * width; @@ -1349,7 +1352,7 @@ opj_image_t* pgxtoimage(const char *filename, opj_cparameters_t *parameters) } fseek(f, 0, SEEK_SET); - if (fscanf(f, "PG%[ \t]%c%c%[ \t+-]%d%[ \t]%d%[ \t]%d", temp, &endian1, + if (fscanf(f, "PG%31[ \t]%c%c%31[ \t+-]%d%31[ \t]%d%31[ \t]%d", temp, &endian1, &endian2, signtmp, &prec, temp, &w, temp, &h) != 9) { fprintf(stderr, "ERROR: Failed to read the right number of element from the fscanf() function!\n"); @@ -1485,7 +1488,7 @@ int imagetopgx(opj_image_t * image, const char *outfile) const size_t olen = strlen(outfile); const size_t dotpos = olen - 4; const size_t total = dotpos + 1 + 1 + 4; /* '-' + '[1-3]' + '.pgx' */ - if (outfile[dotpos] != '.') { + if (olen < 4 || outfile[dotpos] != '.') { /* `pgx` was recognized but there is no dot at expected position */ fprintf(stderr, "ERROR -> Impossible happen."); return 1; @@ -1862,6 +1865,15 @@ opj_image_t* pnmtoimage(const char *filename, opj_cparameters_t *parameters) return NULL; } + /* This limitation could be removed by making sure to use size_t below */ + if (header_info.height != 0 && + header_info.width > INT_MAX / header_info.height) { + fprintf(stderr, "pnmtoimage:Image %dx%d too big!\n", + header_info.width, header_info.height); + fclose(fp); + return NULL; + } + format = header_info.format; switch (format) {