jp3d/jpwl/mj2/jpip: Fix resource leaks (#1226)

This issues were found by cppcheck and coverity.
This commit is contained in:
Eduardo Barretto 2020-04-16 19:09:40 -03:00 committed by GitHub
parent 221a801a97
commit 040e142288
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
16 changed files with 71 additions and 8 deletions

View File

@ -280,6 +280,7 @@ opj_volume_t* pgxtovolume(char *relpath, opj_cparameters_t *parameters)
if (!sliceno) { if (!sliceno) {
fprintf(stdout, fprintf(stdout,
"[ERROR] No slices with this pattern founded !! Please check input volume name\n"); "[ERROR] No slices with this pattern founded !! Please check input volume name\n");
closedir(dirp);
return NULL; return NULL;
} }
/*if ( maxslice != sliceno) { /*if ( maxslice != sliceno) {
@ -317,6 +318,7 @@ opj_volume_t* pgxtovolume(char *relpath, opj_cparameters_t *parameters)
} else { } else {
fprintf(stdout, "[ERROR] Bad pgx header, please check input file\n"); fprintf(stdout, "[ERROR] Bad pgx header, please check input file\n");
fclose(f); fclose(f);
closedir(dirp);
return NULL; return NULL;
} }
@ -396,9 +398,7 @@ opj_volume_t* pgxtovolume(char *relpath, opj_cparameters_t *parameters)
fclose(f); fclose(f);
} /* for s --> sliceno*/ } /* for s --> sliceno*/
comp->bpp = int_floorlog2(maxvalue) + 1; comp->bpp = int_floorlog2(maxvalue) + 1;
if (sliceno != 1) {
closedir(dirp); closedir(dirp);
}
/*dump_volume(stdout, volume);*/ /*dump_volume(stdout, volume);*/
return volume; return volume;
} }
@ -581,6 +581,7 @@ opj_volume_t* bintovolume(char *filename, char *fileimg,
f = fopen(filename, "rb"); f = fopen(filename, "rb");
if (!f) { if (!f) {
fprintf(stdout, "[ERROR] Failed to open %s for reading !!\n", filename); fprintf(stdout, "[ERROR] Failed to open %s for reading !!\n", filename);
opj_free(volume);
return 0; return 0;
} }
@ -918,7 +919,7 @@ opj_volume_t* imgtovolume(char *fileimg, opj_cparameters_t *parameters)
f = fopen(filename, "rb"); f = fopen(filename, "rb");
if (!f) { if (!f) {
fprintf(stderr, "[ERROR] Failed to open %s for reading !!\n", filename); fprintf(stderr, "[ERROR] Failed to open %s for reading !!\n", filename);
fclose(f); opj_free(volume);
return 0; return 0;
} }

View File

@ -49,12 +49,14 @@ static int jpip_to_jp2(char *argv[])
dec = init_jpipdecoder(OPJ_TRUE); dec = init_jpipdecoder(OPJ_TRUE);
if (!(fread_jpip(argv[1], dec))) { if (!(fread_jpip(argv[1], dec))) {
destroy_jpipdecoder(&dec);
return 1; return 1;
} }
decode_jpip(dec); decode_jpip(dec);
if (!(fwrite_jp2k(argv[2], dec))) { if (!(fwrite_jp2k(argv[2], dec))) {
destroy_jpipdecoder(&dec);
return 1; return 1;
} }
@ -83,12 +85,14 @@ static int jpip_to_j2k(char *argv[])
dec = init_jpipdecoder(OPJ_FALSE); dec = init_jpipdecoder(OPJ_FALSE);
if (!(fread_jpip(argv[1], dec))) { if (!(fread_jpip(argv[1], dec))) {
destroy_jpipdecoder(&dec);
return 1; return 1;
} }
decode_jpip(dec); decode_jpip(dec);
if (!(fwrite_jp2k(argv[2], dec))) { if (!(fwrite_jp2k(argv[2], dec))) {
destroy_jpipdecoder(&dec);
return 1; return 1;
} }

View File

@ -465,6 +465,7 @@ int imagetotga(opj_image_t * image, const char *outfile)
|| (image->comps[0].prec != image->comps[i + 1].prec)) { || (image->comps[0].prec != image->comps[i + 1].prec)) {
fprintf(stderr, fprintf(stderr,
"Unable to create a tga file with such J2K image charateristics."); "Unable to create a tga file with such J2K image charateristics.");
fclose(fdest);
return 1; return 1;
} }
} }
@ -478,6 +479,7 @@ int imagetotga(opj_image_t * image, const char *outfile)
/* Write TGA header */ /* Write TGA header */
bpp = write_alpha ? 32 : 24; bpp = write_alpha ? 32 : 24;
if (!tga_writeheader(fdest, bpp, width, height, OPJ_TRUE)) { if (!tga_writeheader(fdest, bpp, width, height, OPJ_TRUE)) {
fclose(fdest);
return 1; return 1;
} }
@ -510,6 +512,7 @@ int imagetotga(opj_image_t * image, const char *outfile)
res = fwrite(&value, 1, 1, fdest); res = fwrite(&value, 1, 1, fdest);
if (res < 1) { if (res < 1) {
fprintf(stderr, "failed to write 1 byte for %s\n", outfile); fprintf(stderr, "failed to write 1 byte for %s\n", outfile);
fclose(fdest);
return 1; return 1;
} }
@ -517,6 +520,7 @@ int imagetotga(opj_image_t * image, const char *outfile)
res = fwrite(&value, 1, 1, fdest); res = fwrite(&value, 1, 1, fdest);
if (res < 1) { if (res < 1) {
fprintf(stderr, "failed to write 1 byte for %s\n", outfile); fprintf(stderr, "failed to write 1 byte for %s\n", outfile);
fclose(fdest);
return 1; return 1;
} }
@ -524,6 +528,7 @@ int imagetotga(opj_image_t * image, const char *outfile)
res = fwrite(&value, 1, 1, fdest); res = fwrite(&value, 1, 1, fdest);
if (res < 1) { if (res < 1) {
fprintf(stderr, "failed to write 1 byte for %s\n", outfile); fprintf(stderr, "failed to write 1 byte for %s\n", outfile);
fclose(fdest);
return 1; return 1;
} }
@ -533,12 +538,15 @@ int imagetotga(opj_image_t * image, const char *outfile)
res = fwrite(&value, 1, 1, fdest); res = fwrite(&value, 1, 1, fdest);
if (res < 1) { if (res < 1) {
fprintf(stderr, "failed to write 1 byte for %s\n", outfile); fprintf(stderr, "failed to write 1 byte for %s\n", outfile);
fclose(fdest);
return 1; return 1;
} }
} }
} }
} }
fclose(fdest);
return 0; return 0;
} }
@ -832,6 +840,7 @@ opj_image_t* bmptoimage(const char *filename, opj_cparameters_t *parameters)
RGB = (unsigned char *) malloc(W * H * sizeof(unsigned char)); RGB = (unsigned char *) malloc(W * H * sizeof(unsigned char));
if (fread(RGB, sizeof(unsigned char), W * H, IN) != W * H) { if (fread(RGB, sizeof(unsigned char), W * H, IN) != W * H) {
fclose(IN);
free(table_R); free(table_R);
free(table_G); free(table_G);
free(table_B); free(table_B);
@ -1505,6 +1514,7 @@ int imagetopgx(opj_image_t * image, const char *outfile)
fdest = fopen(name, "wb"); fdest = fopen(name, "wb");
if (!fdest) { if (!fdest) {
fprintf(stderr, "ERROR -> failed to open %s for writing\n", name); fprintf(stderr, "ERROR -> failed to open %s for writing\n", name);
free(name);
return 1; return 1;
} }
/* don't need name anymore */ /* don't need name anymore */

View File

@ -1681,8 +1681,15 @@ int main(int argc, char **argv)
if (dirptr) { if (dirptr) {
dirptr->filename_buf = (char*)malloc(num_images * OPJ_PATH_LEN * sizeof( dirptr->filename_buf = (char*)malloc(num_images * OPJ_PATH_LEN * sizeof(
char)); /* Stores at max 10 image file names*/ char)); /* Stores at max 10 image file names*/
dirptr->filename = (char**) malloc(num_images * sizeof(char*));
if (!dirptr->filename_buf) { if (!dirptr->filename_buf) {
free(parameters.cp_comment);
free(dirptr);
return 0;
}
dirptr->filename = (char**) malloc(num_images * sizeof(char*));
if (!dirptr->filename) {
free(parameters.cp_comment);
free(dirptr);
return 0; return 0;
} }
for (i = 0; i < num_images; i++) { for (i = 0; i < num_images; i++) {
@ -1690,9 +1697,13 @@ int main(int argc, char **argv)
} }
} }
if (load_images(dirptr, img_fol.imgdirpath) == 1) { if (load_images(dirptr, img_fol.imgdirpath) == 1) {
free(parameters.cp_comment);
free(dirptr);
return 0; return 0;
} }
if (num_images == 0) { if (num_images == 0) {
free(parameters.cp_comment);
free(dirptr);
fprintf(stdout, "Folder is empty\n"); fprintf(stdout, "Folder is empty\n");
return 0; return 0;
} }
@ -1850,6 +1861,7 @@ int main(int argc, char **argv)
if (res < (size_t)codestream_length) { /* FIXME */ if (res < (size_t)codestream_length) { /* FIXME */
fprintf(stderr, "failed to write %d (%s)\n", codestream_length, fprintf(stderr, "failed to write %d (%s)\n", codestream_length,
parameters.outfile); parameters.outfile);
fclose(f);
return 1; return 1;
} }
fclose(f); fclose(f);
@ -1914,6 +1926,7 @@ int main(int argc, char **argv)
if (res < (size_t)codestream_length) { /* FIXME */ if (res < (size_t)codestream_length) { /* FIXME */
fprintf(stderr, "failed to write %d (%s)\n", codestream_length, fprintf(stderr, "failed to write %d (%s)\n", codestream_length,
parameters.outfile); parameters.outfile);
fclose(f);
return 1; return 1;
} }
fclose(f); fclose(f);

View File

@ -597,11 +597,16 @@ int main(int argc, char **argv)
if (dirptr) { if (dirptr) {
dirptr->filename_buf = (char*)malloc(num_images * OPJ_PATH_LEN * sizeof( dirptr->filename_buf = (char*)malloc(num_images * OPJ_PATH_LEN * sizeof(
char)); /* Stores at max 10 image file names*/ char)); /* Stores at max 10 image file names*/
dirptr->filename = (char**) malloc(num_images * sizeof(char*));
if (!dirptr->filename_buf) { if (!dirptr->filename_buf) {
free(dirptr);
return 1; return 1;
} }
dirptr->filename = (char**) malloc(num_images * sizeof(char*));
if (!dirptr->filename) {
free(dirptr);
return 1;
}
for (i = 0; i < num_images; i++) { for (i = 0; i < num_images; i++) {
dirptr->filename[i] = dirptr->filename_buf + i * OPJ_PATH_LEN; dirptr->filename[i] = dirptr->filename_buf + i * OPJ_PATH_LEN;
} }

View File

@ -304,6 +304,7 @@ int main(int argc, char *argv[])
xmlout = fopen(outfile, "w"); /* was: argv[2] */ xmlout = fopen(outfile, "w"); /* was: argv[2] */
if (!xmlout) { if (!xmlout) {
fprintf(stderr, "Failed to open %s for writing.\n", outfile); /* was: argv[2] */ fprintf(stderr, "Failed to open %s for writing.\n", outfile); /* was: argv[2] */
fclose(file);
return 1; return 1;
} }
// Leave it open // Leave it open

View File

@ -708,6 +708,7 @@ int main(int argc, char **argv)
yuv_num_frames(&movie->tk[0], mj2_parameters.infile); yuv_num_frames(&movie->tk[0], mj2_parameters.infile);
if (movie->tk[0].num_samples == 0) { if (movie->tk[0].num_samples == 0) {
fclose(mj2file);
return 1; return 1;
} }

View File

@ -99,8 +99,9 @@ int main(int argc, char *argv[])
/* Checking output file */ /* Checking output file */
outfile = fopen(argv[2], "w"); outfile = fopen(argv[2], "w");
if (!file) { if (!outfile) {
fprintf(stderr, "failed to open %s for writing\n", argv[2]); fprintf(stderr, "failed to open %s for writing\n", argv[2]);
fclose(file);
return 1; return 1;
} }
fclose(outfile); fclose(outfile);
@ -169,6 +170,7 @@ int main(int argc, char *argv[])
if ((frame_codestream = (unsigned char*) if ((frame_codestream = (unsigned char*)
realloc(frame_codestream, max_codstrm_size)) == NULL) { realloc(frame_codestream, max_codstrm_size)) == NULL) {
printf("Error reallocation memory\n"); printf("Error reallocation memory\n");
free(frame_codestream);
return 1; return 1;
}; };
} }

View File

@ -268,6 +268,7 @@ static int t2_encode_packet(opj_tcd_tile_t * tile, opj_tcp_t * tcp,
if (bio_flush(bio)) { if (bio_flush(bio)) {
bio_destroy(bio);
return -999; /* modified to eliminate longjmp !! */ return -999; /* modified to eliminate longjmp !! */
} }

View File

@ -232,6 +232,9 @@ void * aux_streaming(void *arg)
} }
break; break;
} }
if (close_socket(connected_socket) != 0) {
perror("close");
}
} }
opj_free(chunk); opj_free(chunk);

View File

@ -211,6 +211,7 @@ box_param_t * gene_boxbyType(int fd, OPJ_OFF_T offset, OPJ_SIZE_T length,
} else { } else {
fprintf(FCGI_stderr, "Error: error in gene_boxbyType( %d, %" PRId64 ", %" PRId64 fprintf(FCGI_stderr, "Error: error in gene_boxbyType( %d, %" PRId64 ", %" PRId64
", %s)\n", fd, offset, length, TBox); ", %s)\n", fd, offset, length, TBox);
free(data);
return NULL; return NULL;
} }
} }

View File

@ -270,6 +270,7 @@ OPJ_BOOL check_JP2boxidx(boxlist_param_t *toplev_boxlist)
ni = fetch_DBox1byte(prxy, pos); ni = fetch_DBox1byte(prxy, pos);
if (ni != 1) { if (ni != 1) {
fprintf(FCGI_stderr, "Multiple indexes not supported\n"); fprintf(FCGI_stderr, "Multiple indexes not supported\n");
opj_free(prxy);
return OPJ_FALSE; return OPJ_FALSE;
} }
pos += 1; pos += 1;
@ -361,6 +362,7 @@ OPJ_BOOL set_cidxdata(box_param_t *cidx_box, index_param_t *jp2idx)
if (!search_boxheader("mhix", manf)) { if (!search_boxheader("mhix", manf)) {
fprintf(FCGI_stderr, "Error: mhix box not present in manfbox\n"); fprintf(FCGI_stderr, "Error: mhix box not present in manfbox\n");
opj_free(jp2idx); opj_free(jp2idx);
delete_manfbox(&manf);
return OPJ_FALSE; return OPJ_FALSE;
} }
set_mainmhixdata(cidx_box, codestream, jp2idx); set_mainmhixdata(cidx_box, codestream, jp2idx);
@ -368,6 +370,7 @@ OPJ_BOOL set_cidxdata(box_param_t *cidx_box, index_param_t *jp2idx)
if (!search_boxheader("tpix", manf)) { if (!search_boxheader("tpix", manf)) {
fprintf(FCGI_stderr, "Error: tpix box not present in manfbox\n"); fprintf(FCGI_stderr, "Error: tpix box not present in manfbox\n");
opj_free(jp2idx); opj_free(jp2idx);
delete_manfbox(&manf);
return OPJ_FALSE; return OPJ_FALSE;
} }
set_tpixdata(cidx_box, jp2idx); set_tpixdata(cidx_box, jp2idx);
@ -375,6 +378,7 @@ OPJ_BOOL set_cidxdata(box_param_t *cidx_box, index_param_t *jp2idx)
if (!search_boxheader("thix", manf)) { if (!search_boxheader("thix", manf)) {
fprintf(FCGI_stderr, "Error: thix box not present in manfbox\n"); fprintf(FCGI_stderr, "Error: thix box not present in manfbox\n");
opj_free(jp2idx); opj_free(jp2idx);
delete_manfbox(&manf);
return OPJ_FALSE; return OPJ_FALSE;
} }
set_thixdata(cidx_box, jp2idx); set_thixdata(cidx_box, jp2idx);
@ -382,6 +386,7 @@ OPJ_BOOL set_cidxdata(box_param_t *cidx_box, index_param_t *jp2idx)
if (!search_boxheader("ppix", manf)) { if (!search_boxheader("ppix", manf)) {
fprintf(FCGI_stderr, "Error: ppix box not present in manfbox\n"); fprintf(FCGI_stderr, "Error: ppix box not present in manfbox\n");
opj_free(jp2idx); opj_free(jp2idx);
delete_manfbox(&manf);
return OPJ_FALSE; return OPJ_FALSE;
} }
set_ppixdata(cidx_box, jp2idx); set_ppixdata(cidx_box, jp2idx);
@ -497,6 +502,7 @@ OPJ_BOOL set_tpixdata(box_param_t *cidx_box, index_param_t *jp2idx)
if (!(faix_box = gene_boxbyType(tpix_box->fd, get_DBoxoff(tpix_box), if (!(faix_box = gene_boxbyType(tpix_box->fd, get_DBoxoff(tpix_box),
get_DBoxlen(tpix_box), "faix"))) { get_DBoxlen(tpix_box), "faix"))) {
fprintf(FCGI_stderr, "Error: faix box not present in tpix box\n"); fprintf(FCGI_stderr, "Error: faix box not present in tpix box\n");
opj_free(tpix_box);
return OPJ_FALSE; return OPJ_FALSE;
} }
@ -602,11 +608,13 @@ OPJ_BOOL set_ppixdata(box_param_t *cidx_box, index_param_t *jp2idx)
if (jp2idx->SIZ.Csiz <= comp_idx) { if (jp2idx->SIZ.Csiz <= comp_idx) {
fprintf(FCGI_stderr, fprintf(FCGI_stderr,
"Error: num of faix boxes is not identical to num of components in ppix box\n"); "Error: num of faix boxes is not identical to num of components in ppix box\n");
delete_manfbox(&manf);
return OPJ_FALSE; return OPJ_FALSE;
} }
if (!(faix_box = gene_boxbyOffset(cidx_box->fd, inbox_offset))) { if (!(faix_box = gene_boxbyOffset(cidx_box->fd, inbox_offset))) {
fprintf(FCGI_stderr, "Error: faix box not present in ppix box\n"); fprintf(FCGI_stderr, "Error: faix box not present in ppix box\n");
delete_manfbox(&manf);
return OPJ_FALSE; return OPJ_FALSE;
} }

View File

@ -76,6 +76,8 @@ Byte_t * j2k_to_pnm(const char *fn, ihdrbox_param_t **ihdrbox)
/* setup the decoder decoding parameters using user parameters */ /* setup the decoder decoding parameters using user parameters */
if (!opj_setup_decoder(l_codec, &parameters)) { if (!opj_setup_decoder(l_codec, &parameters)) {
fprintf(stderr, "ERROR -> j2k_dump: failed to setup the decoder\n"); fprintf(stderr, "ERROR -> j2k_dump: failed to setup the decoder\n");
opj_stream_destroy(l_stream);
opj_destroy_codec(l_codec);
return NULL; return NULL;
} }

View File

@ -357,6 +357,7 @@ OPJ_BOOL OPJ_CALLCONV fread_jpip(const char fname[], jpip_dec_param_t *dec)
} }
if (!(dec->jpiplen = (Byte8_t)get_filesize(infd))) { if (!(dec->jpiplen = (Byte8_t)get_filesize(infd))) {
close(infd);
return OPJ_FALSE; return OPJ_FALSE;
} }
@ -365,6 +366,7 @@ OPJ_BOOL OPJ_CALLCONV fread_jpip(const char fname[], jpip_dec_param_t *dec)
if (read(infd, dec->jpipstream, dec->jpiplen) != (int)dec->jpiplen) { if (read(infd, dec->jpipstream, dec->jpiplen) != (int)dec->jpiplen) {
fprintf(stderr, "file reading error\n"); fprintf(stderr, "file reading error\n");
opj_free(dec->jpipstream); opj_free(dec->jpipstream);
close(infd);
return OPJ_FALSE; return OPJ_FALSE;
} }

View File

@ -2822,16 +2822,24 @@ int OPJ_CALLCONV mj2_read_struct(FILE *file, opj_mj2_t *movie)
fseek(file, foffset, SEEK_SET); fseek(file, foffset, SEEK_SET);
src = (unsigned char*)opj_realloc(src, box.length); src = (unsigned char*)opj_realloc(src, box.length);
if (src == NULL) {
opj_event_msg(cio->cinfo, EVT_ERROR, "Error reallocation memory\n");
opj_free(src);
return 1;
}
fsresult = fread(src, box.length, 1, file); fsresult = fread(src, box.length, 1, file);
if (fsresult != 1) { if (fsresult != 1) {
opj_event_msg(cio->cinfo, EVT_ERROR, opj_event_msg(cio->cinfo, EVT_ERROR,
"End of file reached while trying to read MOOV box\n"); "End of file reached while trying to read MOOV box\n");
opj_free(src);
return 1; return 1;
} }
cio = opj_cio_open((opj_common_ptr)movie->cinfo, src, box.length); cio = opj_cio_open((opj_common_ptr)movie->cinfo, src, box.length);
if (mj2_read_moov(movie, &img, cio)) { if (mj2_read_moov(movie, &img, cio)) {
opj_free(src);
return 1; return 1;
} }

View File

@ -62,6 +62,7 @@ unsigned int OPJ_CALLCONV yuv_num_frames(mj2_tk_t * tk, char *infile)
fprintf(stderr, fprintf(stderr,
"YUV does not contains any frame of %d x %d size\n", tk->w, "YUV does not contains any frame of %d x %d size\n", tk->w,
tk->h); tk->h);
fclose(f);
return 0; return 0;
} }
fclose(f); fclose(f);