diff options
| author | julienmalik <julienmalik@users.noreply.github.com> | 2016-04-29 23:49:17 +0200 |
|---|---|---|
| committer | Matthieu Darbois <mayeut@users.noreply.github.com> | 2016-04-29 23:49:17 +0200 |
| commit | 319fc971fef8a1e1c1c543506c26805873e3f258 (patch) | |
| tree | 9c17957f208bffbf04276bdd012cd8428209e377 /src/bin/jp2 | |
| parent | e166e4a209d9a3e4b583e4b2cdcbab2c57967eb1 (diff) | |
cppcheck fix for openjp2 (#740)
Diffstat (limited to 'src/bin/jp2')
| -rw-r--r-- | src/bin/jp2/convert.c | 11 | ||||
| -rw-r--r-- | src/bin/jp2/convertbmp.c | 8 | ||||
| -rw-r--r-- | src/bin/jp2/convertpng.c | 14 | ||||
| -rw-r--r-- | src/bin/jp2/opj_compress.c | 29 | ||||
| -rw-r--r-- | src/bin/jp2/opj_decompress.c | 73 | ||||
| -rw-r--r-- | src/bin/jp2/opj_dump.c | 49 |
6 files changed, 125 insertions, 59 deletions
diff --git a/src/bin/jp2/convert.c b/src/bin/jp2/convert.c index 13713204..940ac2fa 100644 --- a/src/bin/jp2/convert.c +++ b/src/bin/jp2/convert.c @@ -611,6 +611,10 @@ static int tga_readheader(FILE *fp, unsigned int *bits_per_pixel, if (id_len) { unsigned char *id = (unsigned char *) malloc(id_len); + if(id == 0){ + fprintf(stderr, "tga_readheader: memory out\n"); + return 0; + } if ( !fread(id, id_len, 1, fp) ) { fprintf(stderr, "\nError: fread return a number of element different from the expected.\n"); @@ -1249,6 +1253,7 @@ int imagetopgx(opj_image_t * image, const char *outfile) { name = (char*)malloc(total+1); if (name == NULL) { + fprintf(stderr, "imagetopgx: memory out\n"); goto fin; } } @@ -1906,7 +1911,11 @@ if(v > 65535) v = 65535; else if(v < 0) v = 0; fprintf(stderr," is written to the file\n"); } destname = (char*)malloc(strlen(outfile) + 8); - + if(destname == NULL){ + fprintf(stderr, "imagetopnm: memory out\n"); + fclose(fdest); + return 1; + } for (compno = 0; compno < ncomp; compno++) { if (ncomp > 1) diff --git a/src/bin/jp2/convertbmp.c b/src/bin/jp2/convertbmp.c index 910574b8..a09c0ae5 100644 --- a/src/bin/jp2/convertbmp.c +++ b/src/bin/jp2/convertbmp.c @@ -181,7 +181,7 @@ static void bmpmask32toimage(const OPJ_UINT8* pData, OPJ_UINT32 stride, opj_imag OPJ_UINT32 width, height; OPJ_UINT32 x, y; const OPJ_UINT8 *pSrc = NULL; - OPJ_BOOL hasAlpha = OPJ_FALSE; + OPJ_BOOL hasAlpha; OPJ_UINT32 redShift, redPrec; OPJ_UINT32 greenShift, greenPrec; OPJ_UINT32 blueShift, bluePrec; @@ -239,7 +239,7 @@ static void bmpmask16toimage(const OPJ_UINT8* pData, OPJ_UINT32 stride, opj_imag OPJ_UINT32 width, height; OPJ_UINT32 x, y; const OPJ_UINT8 *pSrc = NULL; - OPJ_BOOL hasAlpha = OPJ_FALSE; + OPJ_BOOL hasAlpha; OPJ_UINT32 redShift, redPrec; OPJ_UINT32 greenShift, greenPrec; OPJ_UINT32 blueShift, bluePrec; @@ -891,7 +891,7 @@ int imagetobmp(opj_image_t * image, const char *outfile) { fprintf(fdest, "%c%c%c", bc, gc, rc); if ((i + 1) % w == 0) { - for (pad = (3 * w) % 4 ? 4 - (3 * w) % 4 : 0; pad > 0; pad--) /* ADD */ + for (pad = ((3 * w) % 4) ? (4 - (3 * w) % 4) : 0; pad > 0; pad--) /* ADD */ fprintf(fdest, "%c", 0); } } @@ -967,7 +967,7 @@ int imagetobmp(opj_image_t * image, const char *outfile) { fprintf(fdest, "%c", (OPJ_UINT8)r); if ((i + 1) % w == 0) { - for (pad = w % 4 ? 4 - w % 4 : 0; pad > 0; pad--) /* ADD */ + for ((pad = w % 4) ? (4 - w % 4) : 0; pad > 0; pad--) /* ADD */ fprintf(fdest, "%c", 0); } } diff --git a/src/bin/jp2/convertpng.c b/src/bin/jp2/convertpng.c index 8d117412..5635c7d0 100644 --- a/src/bin/jp2/convertpng.c +++ b/src/bin/jp2/convertpng.c @@ -185,9 +185,17 @@ opj_image_t *pngtoimage(const char *read_idf, opj_cparameters_t * params) rows = (OPJ_BYTE**)calloc(height+1, sizeof(OPJ_BYTE*)); - for(i = 0; i < height; ++i) + if(rows == NULL){ + fprintf(stderr, "pngtoimage: memory out\n"); + goto fin; + } + for(i = 0; i < height; ++i){ rows[i] = (OPJ_BYTE*)malloc(png_get_rowbytes(png,info)); - + if(rows[i] == NULL){ + fprintf(stderr,"pngtoimage: memory out\n"); + goto fin; + } + } png_read_image(png, rows); /* Create image */ @@ -235,7 +243,7 @@ fin: if(rows) { for(i = 0; i < height; ++i) - free(rows[i]); + if(rows[i]) free(rows[i]); free(rows); } if (row32s) { diff --git a/src/bin/jp2/opj_compress.c b/src/bin/jp2/opj_compress.c index 9d690a56..5a63a9d6 100644 --- a/src/bin/jp2/opj_compress.c +++ b/src/bin/jp2/opj_compress.c @@ -647,6 +647,10 @@ static int parse_cmdline_encoder(int argc, char **argv, opj_cparameters_t *param raw_cp->rawBitDepth = bitdepth; raw_cp->rawSigned = raw_signed; raw_cp->rawComps = (raw_comp_cparameters_t*) malloc(((OPJ_UINT32)(ncomp))*sizeof(raw_comp_cparameters_t)); + if(raw_cp->rawComps == NULL){ + free(substr1); + return 1; + } for (compno = 0; compno < ncomp && !wrong; compno++) { if (substr2 == NULL) { raw_cp->rawComps[compno].dx = lastdx; @@ -725,6 +729,9 @@ static int parse_cmdline_encoder(int argc, char **argv, opj_cparameters_t *param numresolution = (OPJ_UINT32)parameters->numresolution; matrix_width = numresolution * 3; parameters->cp_matrice = (int *) malloc(numlayers * matrix_width * sizeof(int)); + if(parameters->cp_matrice == NULL){ + return 1; + } s = s + 2; for (i = 0; i < numlayers; i++) { @@ -995,6 +1002,9 @@ static int parse_cmdline_encoder(int argc, char **argv, opj_cparameters_t *param case 'z': /* Image Directory path */ { img_fol->imgdirpath = (char*)malloc(strlen(opj_optarg) + 1); + if(img_fol->imgdirpath == NULL){ + return 1; + } strcpy(img_fol->imgdirpath,opj_optarg); img_fol->set_imgdir=1; } @@ -1540,6 +1550,7 @@ static int parse_cmdline_encoder(int argc, char **argv, opj_cparameters_t *param } return 0; + } /* -------------------------------------------------------------------------- */ @@ -1635,7 +1646,7 @@ int main(int argc, char **argv) { /* parse input and get user encoding parameters */ parameters.tcp_mct = (char) 255; /* This will be set later according to the input image or the provided option */ if(parse_cmdline_encoder(argc, argv, ¶meters,&img_fol, &raw_cp, indexfilename, sizeof(indexfilename)) == 1) { - return 1; + goto fails; } /* Read directory if necessary */ @@ -1848,7 +1859,9 @@ int main(int argc, char **argv) { OPJ_BYTE *l_data; OPJ_UINT32 l_data_size = 512*512*3; l_data = (OPJ_BYTE*) calloc( 1,l_data_size); - assert( l_data ); + if(l_data == NULL){ + goto fails; + } for (i=0;i<l_nb_tiles;++i) { if (! opj_write_tile(l_codec,i,l_data,l_data_size,l_stream)) { fprintf(stderr, "ERROR -> test_tile_encoder: failed to write the tile %d!\n",i); @@ -1904,4 +1917,16 @@ int main(int argc, char **argv) { } return 0; + +fails: + if(parameters.cp_comment) free(parameters.cp_comment); + if(parameters.cp_matrice) free(parameters.cp_matrice); + if(raw_cp.rawComps) free(raw_cp.rawComps); + if(img_fol.imgdirpath) free(img_fol.imgdirpath); + if(dirptr){ + if(dirptr->filename_buf) free(dirptr->filename_buf); + if(dirptr->filename) free(dirptr->filename); + free(dirptr); + } + return 1; } diff --git a/src/bin/jp2/opj_decompress.c b/src/bin/jp2/opj_decompress.c index f3b1cd5c..ab7ff04a 100644 --- a/src/bin/jp2/opj_decompress.c +++ b/src/bin/jp2/opj_decompress.c @@ -680,6 +680,9 @@ int parse_cmdline_decoder(int argc, char **argv, opj_decompress_parameters *para case 'y': /* Image Directory path */ { img_fol->imgdirpath = (char*)malloc(strlen(opj_optarg) + 1); + if(img_fol->imgdirpath == NULL){ + return 1; + } strcpy(img_fol->imgdirpath,opj_optarg); img_fol->set_imgdir=1; } @@ -1200,8 +1203,7 @@ int main(int argc, char **argv) /* parse input and get user encoding parameters */ if(parse_cmdline_decoder(argc, argv, ¶meters,&img_fol) == 1) { - destroy_parameters(¶meters); - return EXIT_FAILURE; + failed = 1; goto fin; } /* Initialize reading of directory */ @@ -1210,26 +1212,30 @@ int main(int argc, char **argv) num_images=get_num_images(img_fol.imgdirpath); dirptr=(dircnt_t*)malloc(sizeof(dircnt_t)); - if(dirptr){ - dirptr->filename_buf = (char*)malloc((size_t)num_images*OPJ_PATH_LEN*sizeof(char)); /* Stores at max 10 image file names*/ - dirptr->filename = (char**) malloc((size_t)num_images*sizeof(char*)); + if(!dirptr){ + destroy_parameters(¶meters); + return EXIT_FAILURE; + } + dirptr->filename_buf = (char*)malloc((size_t)num_images*OPJ_PATH_LEN*sizeof(char)); /* Stores at max 10 image file names*/ + if(!dirptr->filename_buf){ + failed = 1; goto fin; + } + + dirptr->filename = (char**) malloc((size_t)num_images*sizeof(char*)); - if(!dirptr->filename_buf){ - destroy_parameters(¶meters); - return EXIT_FAILURE; - } - for(it_image=0;it_image<num_images;it_image++){ - dirptr->filename[it_image] = dirptr->filename_buf + it_image*OPJ_PATH_LEN; - } + if(!dirptr->filename){ + failed = 1; goto fin; + } + for(it_image=0;it_image<num_images;it_image++){ + dirptr->filename[it_image] = dirptr->filename_buf + it_image*OPJ_PATH_LEN; } + if(load_images(dirptr,img_fol.imgdirpath)==1){ - destroy_parameters(¶meters); - return EXIT_FAILURE; + failed = 1; goto fin; } if (num_images==0){ fprintf(stdout,"Folder is empty\n"); - destroy_parameters(¶meters); - return EXIT_FAILURE; + failed = 1; goto fin; } }else{ num_images=1; @@ -1254,8 +1260,7 @@ int main(int argc, char **argv) l_stream = opj_stream_create_default_file_stream(parameters.infile,1); if (!l_stream){ fprintf(stderr, "ERROR -> failed to create the stream from the file %s\n", parameters.infile); - destroy_parameters(¶meters); - return EXIT_FAILURE; + failed = 1; goto fin; } /* decode the JPEG2000 stream */ @@ -1297,21 +1302,19 @@ int main(int argc, char **argv) /* Setup the decoder decoding parameters using user parameters */ if ( !opj_setup_decoder(l_codec, &(parameters.core)) ){ fprintf(stderr, "ERROR -> opj_decompress: failed to setup the decoder\n"); - destroy_parameters(¶meters); opj_stream_destroy(l_stream); opj_destroy_codec(l_codec); - return EXIT_FAILURE; + failed = 1; goto fin; } /* Read the main header of the codestream and if necessary the JP2 boxes*/ if(! opj_read_header(l_stream, l_codec, &image)){ fprintf(stderr, "ERROR -> opj_decompress: failed to read the header\n"); - destroy_parameters(¶meters); opj_stream_destroy(l_stream); opj_destroy_codec(l_codec); opj_image_destroy(image); - return EXIT_FAILURE; + failed = 1; goto fin; } if (!parameters.nb_tile_to_decode) { @@ -1319,21 +1322,19 @@ int main(int argc, char **argv) if (!opj_set_decode_area(l_codec, image, (OPJ_INT32)parameters.DA_x0, (OPJ_INT32)parameters.DA_y0, (OPJ_INT32)parameters.DA_x1, (OPJ_INT32)parameters.DA_y1)){ fprintf(stderr, "ERROR -> opj_decompress: failed to set the decoded area\n"); - destroy_parameters(¶meters); opj_stream_destroy(l_stream); opj_destroy_codec(l_codec); opj_image_destroy(image); - return EXIT_FAILURE; + failed = 1; goto fin; } /* Get the decoded image */ if (!(opj_decode(l_codec, l_stream, image) && opj_end_decompress(l_codec, l_stream))) { fprintf(stderr,"ERROR -> opj_decompress: failed to decode image!\n"); - destroy_parameters(¶meters); opj_destroy_codec(l_codec); opj_stream_destroy(l_stream); opj_image_destroy(image); - return EXIT_FAILURE; + failed = 1; goto fin; } } else { @@ -1344,16 +1345,15 @@ int main(int argc, char **argv) opj_destroy_codec(l_codec); opj_stream_destroy(l_stream); opj_image_destroy(image); - return EXIT_FAILURE; + failed = 1; goto fin; }*/ if (!opj_get_decoded_tile(l_codec, l_stream, image, parameters.tile_index)) { fprintf(stderr, "ERROR -> opj_decompress: failed to decode tile!\n"); - destroy_parameters(¶meters); opj_destroy_codec(l_codec); opj_stream_destroy(l_stream); opj_image_destroy(image); - return EXIT_FAILURE; + failed = 1; goto fin; } fprintf(stdout, "tile %d is decoded!\n\n", parameters.tile_index); } @@ -1432,9 +1432,8 @@ int main(int argc, char **argv) image = upsample_image_components(image); if (image == NULL) { fprintf(stderr, "ERROR -> opj_decompress: failed to upsample image components!\n"); - destroy_parameters(¶meters); opj_destroy_codec(l_codec); - return EXIT_FAILURE; + failed = 1; goto fin; } } @@ -1456,9 +1455,8 @@ int main(int argc, char **argv) } if (image == NULL) { fprintf(stderr, "ERROR -> opj_decompress: failed to convert to RGB image!\n"); - destroy_parameters(¶meters); opj_destroy_codec(l_codec); - return EXIT_FAILURE; + failed = 1; goto fin; } } @@ -1567,10 +1565,17 @@ int main(int argc, char **argv) if(failed) (void)remove(parameters.outfile); /* ignore return value */ } +fin: destroy_parameters(¶meters); + if(failed && img_fol.imgdirpath) free(img_fol.imgdirpath); + if(dirptr){ + if(dirptr->filename) free(dirptr->filename); + if(dirptr->filename_buf) free(dirptr->filename_buf); + free(dirptr); + } if (numDecompressedImages) { fprintf(stdout, "decode time: %d ms\n", (int)( (tCumulative * 1000.0) / (OPJ_FLOAT64)numDecompressedImages)); } return failed ? EXIT_FAILURE : EXIT_SUCCESS; } -/*end main*/ +/*end main()*/ diff --git a/src/bin/jp2/opj_dump.c b/src/bin/jp2/opj_dump.c index bd608c2b..d62eea14 100644 --- a/src/bin/jp2/opj_dump.c +++ b/src/bin/jp2/opj_dump.c @@ -341,6 +341,9 @@ static int parse_cmdline_decoder(int argc, char **argv, opj_dparameters_t *param case 'y': /* Image Directory path */ { img_fol->imgdirpath = (char*)malloc(strlen(opj_optarg) + 1); + if(img_fol->imgdirpath == NULL){ + return 1; + } strcpy(img_fol->imgdirpath,opj_optarg); img_fol->set_imgdir=1; } @@ -442,6 +445,8 @@ int main(int argc, char *argv[]) /* Parse input and get user encoding parameters */ if(parse_cmdline_decoder(argc, argv, ¶meters,&img_fol) == 1) { + if(img_fol.imgdirpath) free(img_fol.imgdirpath); + return EXIT_FAILURE; } @@ -451,25 +456,31 @@ int main(int argc, char *argv[]) num_images=get_num_images(img_fol.imgdirpath); dirptr=(dircnt_t*)malloc(sizeof(dircnt_t)); - if(dirptr){ - dirptr->filename_buf = (char*)malloc((size_t)num_images*OPJ_PATH_LEN*sizeof(char)); /* Stores at max 10 image file names*/ - dirptr->filename = (char**) malloc((size_t)num_images*sizeof(char*)); + if(!dirptr){ + return EXIT_FAILURE; + } + dirptr->filename_buf = (char*)malloc((size_t)num_images*OPJ_PATH_LEN*sizeof(char)); /* Stores at max 10 image file names*/ + if(!dirptr->filename_buf){ + free(dirptr); + return EXIT_FAILURE; + } + dirptr->filename = (char**) malloc((size_t)num_images*sizeof(char*)); - if(!dirptr->filename_buf){ - return EXIT_FAILURE; - } + if(!dirptr->filename){ + goto fails; + } - for(it_image=0;it_image<num_images;it_image++){ - dirptr->filename[it_image] = dirptr->filename_buf + it_image*OPJ_PATH_LEN; - } + for(it_image=0;it_image<num_images;it_image++){ + dirptr->filename[it_image] = dirptr->filename_buf + it_image*OPJ_PATH_LEN; } + if(load_images(dirptr,img_fol.imgdirpath)==1){ - return EXIT_FAILURE; + goto fails; } if (num_images==0){ fprintf(stdout,"Folder is empty\n"); - return EXIT_FAILURE; + goto fails; } }else{ num_images=1; @@ -480,7 +491,7 @@ int main(int argc, char *argv[]) fout = fopen(parameters.outfile,"w"); if (!fout){ fprintf(stderr, "ERROR -> failed to open %s for writing\n", parameters.outfile); - return EXIT_FAILURE; + goto fails; } } else @@ -504,7 +515,7 @@ int main(int argc, char *argv[]) l_stream = opj_stream_create_default_file_stream(parameters.infile,1); if (!l_stream){ fprintf(stderr, "ERROR -> failed to create the stream from the file %s\n",parameters.infile); - return EXIT_FAILURE; + goto fails; } /* Read the JPEG2000 stream */ @@ -546,7 +557,7 @@ int main(int argc, char *argv[]) opj_stream_destroy(l_stream); opj_destroy_codec(l_codec); fclose(fout); - return EXIT_FAILURE; + goto fails; } /* Read the main header of the codestream and if necessary the JP2 boxes*/ @@ -556,7 +567,7 @@ int main(int argc, char *argv[]) opj_destroy_codec(l_codec); opj_image_destroy(image); fclose(fout); - return EXIT_FAILURE; + goto fails; } opj_dump_codec(l_codec, img_fol.flag, fout ); @@ -588,4 +599,12 @@ int main(int argc, char *argv[]) fclose(fout); return EXIT_SUCCESS; + +fails: + if(dirptr){ + if(dirptr->filename) free(dirptr->filename); + if(dirptr->filename_buf) free(dirptr->filename_buf); + free(dirptr); + } + return EXIT_FAILURE; } |
