Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix resource leaks #1226

Merged
merged 1 commit into from
Apr 16, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions src/bin/jp3d/convert.c
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,7 @@ opj_volume_t* pgxtovolume(char *relpath, opj_cparameters_t *parameters)
if (!sliceno) {
fprintf(stdout,
"[ERROR] No slices with this pattern founded !! Please check input volume name\n");
closedir(dirp);
return NULL;
}
/*if ( maxslice != sliceno) {
Expand Down Expand Up @@ -317,6 +318,7 @@ opj_volume_t* pgxtovolume(char *relpath, opj_cparameters_t *parameters)
} else {
fprintf(stdout, "[ERROR] Bad pgx header, please check input file\n");
fclose(f);
closedir(dirp);
return NULL;
}

Expand Down Expand Up @@ -396,9 +398,7 @@ opj_volume_t* pgxtovolume(char *relpath, opj_cparameters_t *parameters)
fclose(f);
} /* for s --> sliceno*/
comp->bpp = int_floorlog2(maxvalue) + 1;
if (sliceno != 1) {
closedir(dirp);
}
closedir(dirp);
/*dump_volume(stdout, volume);*/
return volume;
}
Expand Down Expand Up @@ -581,6 +581,7 @@ opj_volume_t* bintovolume(char *filename, char *fileimg,
f = fopen(filename, "rb");
if (!f) {
fprintf(stdout, "[ERROR] Failed to open %s for reading !!\n", filename);
opj_free(volume);
return 0;
}

Expand Down Expand Up @@ -918,7 +919,7 @@ opj_volume_t* imgtovolume(char *fileimg, opj_cparameters_t *parameters)
f = fopen(filename, "rb");
if (!f) {
fprintf(stderr, "[ERROR] Failed to open %s for reading !!\n", filename);
fclose(f);
opj_free(volume);
return 0;
}

Expand Down
4 changes: 4 additions & 0 deletions src/bin/jpip/opj_jpip_transcode.c
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,14 @@ static int jpip_to_jp2(char *argv[])
dec = init_jpipdecoder(OPJ_TRUE);

if (!(fread_jpip(argv[1], dec))) {
destroy_jpipdecoder(&dec);
return 1;
}

decode_jpip(dec);

if (!(fwrite_jp2k(argv[2], dec))) {
destroy_jpipdecoder(&dec);
return 1;
}

Expand Down Expand Up @@ -83,12 +85,14 @@ static int jpip_to_j2k(char *argv[])
dec = init_jpipdecoder(OPJ_FALSE);

if (!(fread_jpip(argv[1], dec))) {
destroy_jpipdecoder(&dec);
return 1;
}

decode_jpip(dec);

if (!(fwrite_jp2k(argv[2], dec))) {
destroy_jpipdecoder(&dec);
return 1;
}

Expand Down
10 changes: 10 additions & 0 deletions src/bin/jpwl/convert.c
Original file line number Diff line number Diff line change
Expand Up @@ -465,6 +465,7 @@ int imagetotga(opj_image_t * image, const char *outfile)
|| (image->comps[0].prec != image->comps[i + 1].prec)) {
fprintf(stderr,
"Unable to create a tga file with such J2K image charateristics.");
fclose(fdest);
return 1;
}
}
Expand All @@ -478,6 +479,7 @@ int imagetotga(opj_image_t * image, const char *outfile)
/* Write TGA header */
bpp = write_alpha ? 32 : 24;
if (!tga_writeheader(fdest, bpp, width, height, OPJ_TRUE)) {
fclose(fdest);
return 1;
}

Expand Down Expand Up @@ -510,20 +512,23 @@ int imagetotga(opj_image_t * image, const char *outfile)
res = fwrite(&value, 1, 1, fdest);
if (res < 1) {
fprintf(stderr, "failed to write 1 byte for %s\n", outfile);
fclose(fdest);
return 1;
}

value = (unsigned char)(g * scale);
res = fwrite(&value, 1, 1, fdest);
if (res < 1) {
fprintf(stderr, "failed to write 1 byte for %s\n", outfile);
fclose(fdest);
return 1;
}

value = (unsigned char)(r * scale);
res = fwrite(&value, 1, 1, fdest);
if (res < 1) {
fprintf(stderr, "failed to write 1 byte for %s\n", outfile);
fclose(fdest);
return 1;
}

Expand All @@ -533,12 +538,15 @@ int imagetotga(opj_image_t * image, const char *outfile)
res = fwrite(&value, 1, 1, fdest);
if (res < 1) {
fprintf(stderr, "failed to write 1 byte for %s\n", outfile);
fclose(fdest);
return 1;
}
}
}
}

fclose(fdest);

return 0;
}

Expand Down Expand Up @@ -832,6 +840,7 @@ opj_image_t* bmptoimage(const char *filename, opj_cparameters_t *parameters)
RGB = (unsigned char *) malloc(W * H * sizeof(unsigned char));

if (fread(RGB, sizeof(unsigned char), W * H, IN) != W * H) {
fclose(IN);
free(table_R);
free(table_G);
free(table_B);
Expand Down Expand Up @@ -1505,6 +1514,7 @@ int imagetopgx(opj_image_t * image, const char *outfile)
fdest = fopen(name, "wb");
if (!fdest) {
fprintf(stderr, "ERROR -> failed to open %s for writing\n", name);
free(name);
return 1;
}
/* don't need name anymore */
Expand Down
15 changes: 14 additions & 1 deletion src/bin/jpwl/opj_jpwl_compress.c
Original file line number Diff line number Diff line change
Expand Up @@ -1681,18 +1681,29 @@ int main(int argc, char **argv)
if (dirptr) {
dirptr->filename_buf = (char*)malloc(num_images * OPJ_PATH_LEN * sizeof(
char)); /* Stores at max 10 image file names*/
dirptr->filename = (char**) malloc(num_images * sizeof(char*));
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;
}
for (i = 0; i < num_images; i++) {
dirptr->filename[i] = dirptr->filename_buf + i * OPJ_PATH_LEN;
}
}
if (load_images(dirptr, img_fol.imgdirpath) == 1) {
free(parameters.cp_comment);
free(dirptr);
return 0;
}
if (num_images == 0) {
free(parameters.cp_comment);
free(dirptr);
fprintf(stdout, "Folder is empty\n");
return 0;
}
Expand Down Expand Up @@ -1850,6 +1861,7 @@ int main(int argc, char **argv)
if (res < (size_t)codestream_length) { /* FIXME */
fprintf(stderr, "failed to write %d (%s)\n", codestream_length,
parameters.outfile);
fclose(f);
return 1;
}
fclose(f);
Expand Down Expand Up @@ -1914,6 +1926,7 @@ int main(int argc, char **argv)
if (res < (size_t)codestream_length) { /* FIXME */
fprintf(stderr, "failed to write %d (%s)\n", codestream_length,
parameters.outfile);
fclose(f);
return 1;
}
fclose(f);
Expand Down
9 changes: 7 additions & 2 deletions src/bin/jpwl/opj_jpwl_decompress.c
Original file line number Diff line number Diff line change
Expand Up @@ -597,11 +597,16 @@ int main(int argc, char **argv)
if (dirptr) {
dirptr->filename_buf = (char*)malloc(num_images * OPJ_PATH_LEN * sizeof(
char)); /* Stores at max 10 image file names*/
dirptr->filename = (char**) malloc(num_images * sizeof(char*));

if (!dirptr->filename_buf) {
free(dirptr);
return 1;
}
dirptr->filename = (char**) malloc(num_images * sizeof(char*));
if (!dirptr->filename) {
free(dirptr);
return 1;
}

for (i = 0; i < num_images; i++) {
dirptr->filename[i] = dirptr->filename_buf + i * OPJ_PATH_LEN;
}
Expand Down
1 change: 1 addition & 0 deletions src/bin/mj2/mj2_to_metadata.c
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,7 @@ int main(int argc, char *argv[])
xmlout = fopen(outfile, "w"); /* was: argv[2] */
if (!xmlout) {
fprintf(stderr, "Failed to open %s for writing.\n", outfile); /* was: argv[2] */
fclose(file);
return 1;
}
// Leave it open
Expand Down
1 change: 1 addition & 0 deletions src/bin/mj2/opj_mj2_compress.c
Original file line number Diff line number Diff line change
Expand Up @@ -708,6 +708,7 @@ int main(int argc, char **argv)
yuv_num_frames(&movie->tk[0], mj2_parameters.infile);

if (movie->tk[0].num_samples == 0) {
fclose(mj2file);
return 1;
}

Expand Down
4 changes: 3 additions & 1 deletion src/bin/mj2/opj_mj2_decompress.c
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,9 @@ int main(int argc, char *argv[])

/* Checking output file */
outfile = fopen(argv[2], "w");
if (!file) {
if (!outfile) {
fprintf(stderr, "failed to open %s for writing\n", argv[2]);
fclose(file);
return 1;
}
fclose(outfile);
Expand Down Expand Up @@ -169,6 +170,7 @@ int main(int argc, char *argv[])
if ((frame_codestream = (unsigned char*)
realloc(frame_codestream, max_codstrm_size)) == NULL) {
printf("Error reallocation memory\n");
free(frame_codestream);
return 1;
};
}
Expand Down
1 change: 1 addition & 0 deletions src/lib/openjp3d/t2.c
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,7 @@ static int t2_encode_packet(opj_tcd_tile_t * tile, opj_tcp_t * tcp,


if (bio_flush(bio)) {
bio_destroy(bio);
return -999; /* modified to eliminate longjmp !! */
}

Expand Down
3 changes: 3 additions & 0 deletions src/lib/openjpip/auxtrans_manager.c
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,9 @@ void * aux_streaming(void *arg)
}
break;
}
if (close_socket(connected_socket) != 0) {
perror("close");
}
}
opj_free(chunk);

Expand Down
1 change: 1 addition & 0 deletions src/lib/openjpip/box_manager.c
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,7 @@ box_param_t * gene_boxbyType(int fd, OPJ_OFF_T offset, OPJ_SIZE_T length,
} else {
fprintf(FCGI_stderr, "Error: error in gene_boxbyType( %d, %" PRId64 ", %" PRId64
", %s)\n", fd, offset, length, TBox);
free(data);
return NULL;
}
}
Expand Down
8 changes: 8 additions & 0 deletions src/lib/openjpip/index_manager.c
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,7 @@ OPJ_BOOL check_JP2boxidx(boxlist_param_t *toplev_boxlist)
ni = fetch_DBox1byte(prxy, pos);
if (ni != 1) {
fprintf(FCGI_stderr, "Multiple indexes not supported\n");
opj_free(prxy);
return OPJ_FALSE;
}
pos += 1;
Expand Down Expand Up @@ -361,27 +362,31 @@ OPJ_BOOL set_cidxdata(box_param_t *cidx_box, index_param_t *jp2idx)
if (!search_boxheader("mhix", manf)) {
fprintf(FCGI_stderr, "Error: mhix box not present in manfbox\n");
opj_free(jp2idx);
delete_manfbox(&manf);
return OPJ_FALSE;
}
set_mainmhixdata(cidx_box, codestream, jp2idx);

if (!search_boxheader("tpix", manf)) {
fprintf(FCGI_stderr, "Error: tpix box not present in manfbox\n");
opj_free(jp2idx);
delete_manfbox(&manf);
return OPJ_FALSE;
}
set_tpixdata(cidx_box, jp2idx);

if (!search_boxheader("thix", manf)) {
fprintf(FCGI_stderr, "Error: thix box not present in manfbox\n");
opj_free(jp2idx);
delete_manfbox(&manf);
return OPJ_FALSE;
}
set_thixdata(cidx_box, jp2idx);

if (!search_boxheader("ppix", manf)) {
fprintf(FCGI_stderr, "Error: ppix box not present in manfbox\n");
opj_free(jp2idx);
delete_manfbox(&manf);
return OPJ_FALSE;
}
set_ppixdata(cidx_box, jp2idx);
Expand Down Expand Up @@ -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),
get_DBoxlen(tpix_box), "faix"))) {
fprintf(FCGI_stderr, "Error: faix box not present in tpix box\n");
opj_free(tpix_box);
return OPJ_FALSE;
}

Expand Down Expand Up @@ -602,11 +608,13 @@ OPJ_BOOL set_ppixdata(box_param_t *cidx_box, index_param_t *jp2idx)
if (jp2idx->SIZ.Csiz <= comp_idx) {
fprintf(FCGI_stderr,
"Error: num of faix boxes is not identical to num of components in ppix box\n");
delete_manfbox(&manf);
return OPJ_FALSE;
}

if (!(faix_box = gene_boxbyOffset(cidx_box->fd, inbox_offset))) {
fprintf(FCGI_stderr, "Error: faix box not present in ppix box\n");
delete_manfbox(&manf);
return OPJ_FALSE;
}

Expand Down
2 changes: 2 additions & 0 deletions src/lib/openjpip/jp2k_decoder.c
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ Byte_t * j2k_to_pnm(const char *fn, ihdrbox_param_t **ihdrbox)
/* setup the decoder decoding parameters using user parameters */
if (!opj_setup_decoder(l_codec, &parameters)) {
fprintf(stderr, "ERROR -> j2k_dump: failed to setup the decoder\n");
opj_stream_destroy(l_stream);
opj_destroy_codec(l_codec);
return NULL;
}

Expand Down
2 changes: 2 additions & 0 deletions src/lib/openjpip/openjpip.c
Original file line number Diff line number Diff line change
Expand Up @@ -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))) {
close(infd);
return OPJ_FALSE;
}

Expand All @@ -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) {
fprintf(stderr, "file reading error\n");
opj_free(dec->jpipstream);
close(infd);
return OPJ_FALSE;
}

Expand Down
8 changes: 8 additions & 0 deletions src/lib/openmj2/mj2.c
Original file line number Diff line number Diff line change
Expand Up @@ -2822,16 +2822,24 @@ int OPJ_CALLCONV mj2_read_struct(FILE *file, opj_mj2_t *movie)

fseek(file, foffset, SEEK_SET);
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);
if (fsresult != 1) {
opj_event_msg(cio->cinfo, EVT_ERROR,
"End of file reached while trying to read MOOV box\n");
opj_free(src);
return 1;
}

cio = opj_cio_open((opj_common_ptr)movie->cinfo, src, box.length);

if (mj2_read_moov(movie, &img, cio)) {
opj_free(src);
return 1;
}

Expand Down
1 change: 1 addition & 0 deletions src/lib/openmj2/mj2_convert.c
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ unsigned int OPJ_CALLCONV yuv_num_frames(mj2_tk_t * tk, char *infile)
fprintf(stderr,
"YUV does not contains any frame of %d x %d size\n", tk->w,
tk->h);
fclose(f);
return 0;
}
fclose(f);
Expand Down