Merge pull request #1163 from nforro/memory-and-resource-leaks

Fix several memory and resource leaks
This commit is contained in:
Even Rouault 2018-10-31 20:41:52 +01:00 committed by GitHub
commit e52909f4c7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 48 additions and 34 deletions

View File

@ -1739,6 +1739,8 @@ int main(int argc, char **argv)
img_fol_t img_fol; img_fol_t img_fol;
dircnt_t *dirptr = NULL; dircnt_t *dirptr = NULL;
int ret = 0;
OPJ_BOOL bSuccess; OPJ_BOOL bSuccess;
OPJ_BOOL bUseTiles = OPJ_FALSE; /* OPJ_TRUE */ OPJ_BOOL bUseTiles = OPJ_FALSE; /* OPJ_TRUE */
OPJ_UINT32 l_nb_tiles = 4; OPJ_UINT32 l_nb_tiles = 4;
@ -1764,7 +1766,8 @@ int main(int argc, char **argv)
255; /* This will be set later according to the input image or the provided option */ 255; /* This will be set later according to the input image or the provided option */
if (parse_cmdline_encoder(argc, argv, &parameters, &img_fol, &raw_cp, if (parse_cmdline_encoder(argc, argv, &parameters, &img_fol, &raw_cp,
indexfilename, sizeof(indexfilename)) == 1) { indexfilename, sizeof(indexfilename)) == 1) {
goto fails; ret = 1;
goto fin;
} }
/* Read directory if necessary */ /* Read directory if necessary */
@ -1776,18 +1779,21 @@ int main(int argc, char **argv)
char)); /* Stores at max 10 image file names*/ char)); /* Stores at max 10 image file names*/
dirptr->filename = (char**) malloc(num_images * sizeof(char*)); dirptr->filename = (char**) malloc(num_images * sizeof(char*));
if (!dirptr->filename_buf) { if (!dirptr->filename_buf) {
return 0; ret = 0;
goto fin;
} }
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;
} }
} }
if (load_images(dirptr, img_fol.imgdirpath) == 1) { if (load_images(dirptr, img_fol.imgdirpath) == 1) {
return 0; ret = 0;
goto fin;
} }
if (num_images == 0) { if (num_images == 0) {
fprintf(stdout, "Folder is empty\n"); fprintf(stdout, "Folder is empty\n");
return 0; ret = 0;
goto fin;
} }
} else { } else {
num_images = 1; num_images = 1;
@ -1833,7 +1839,8 @@ int main(int argc, char **argv)
image = pgxtoimage(parameters.infile, &parameters); image = pgxtoimage(parameters.infile, &parameters);
if (!image) { if (!image) {
fprintf(stderr, "Unable to load pgx file\n"); fprintf(stderr, "Unable to load pgx file\n");
return 1; ret = 1;
goto fin;
} }
break; break;
@ -1841,7 +1848,8 @@ int main(int argc, char **argv)
image = pnmtoimage(parameters.infile, &parameters); image = pnmtoimage(parameters.infile, &parameters);
if (!image) { if (!image) {
fprintf(stderr, "Unable to load pnm file\n"); fprintf(stderr, "Unable to load pnm file\n");
return 1; ret = 1;
goto fin;
} }
break; break;
@ -1849,7 +1857,8 @@ int main(int argc, char **argv)
image = bmptoimage(parameters.infile, &parameters); image = bmptoimage(parameters.infile, &parameters);
if (!image) { if (!image) {
fprintf(stderr, "Unable to load bmp file\n"); fprintf(stderr, "Unable to load bmp file\n");
return 1; ret = 1;
goto fin;
} }
break; break;
@ -1858,7 +1867,8 @@ int main(int argc, char **argv)
image = tiftoimage(parameters.infile, &parameters); image = tiftoimage(parameters.infile, &parameters);
if (!image) { if (!image) {
fprintf(stderr, "Unable to load tiff file\n"); fprintf(stderr, "Unable to load tiff file\n");
return 1; ret = 1;
goto fin;
} }
break; break;
#endif /* OPJ_HAVE_LIBTIFF */ #endif /* OPJ_HAVE_LIBTIFF */
@ -1867,7 +1877,8 @@ int main(int argc, char **argv)
image = rawtoimage(parameters.infile, &parameters, &raw_cp); image = rawtoimage(parameters.infile, &parameters, &raw_cp);
if (!image) { if (!image) {
fprintf(stderr, "Unable to load raw file\n"); fprintf(stderr, "Unable to load raw file\n");
return 1; ret = 1;
goto fin;
} }
break; break;
@ -1875,7 +1886,8 @@ int main(int argc, char **argv)
image = rawltoimage(parameters.infile, &parameters, &raw_cp); image = rawltoimage(parameters.infile, &parameters, &raw_cp);
if (!image) { if (!image) {
fprintf(stderr, "Unable to load raw file\n"); fprintf(stderr, "Unable to load raw file\n");
return 1; ret = 1;
goto fin;
} }
break; break;
@ -1883,7 +1895,8 @@ int main(int argc, char **argv)
image = tgatoimage(parameters.infile, &parameters); image = tgatoimage(parameters.infile, &parameters);
if (!image) { if (!image) {
fprintf(stderr, "Unable to load tga file\n"); fprintf(stderr, "Unable to load tga file\n");
return 1; ret = 1;
goto fin;
} }
break; break;
@ -1892,7 +1905,8 @@ int main(int argc, char **argv)
image = pngtoimage(parameters.infile, &parameters); image = pngtoimage(parameters.infile, &parameters);
if (!image) { if (!image) {
fprintf(stderr, "Unable to load png file\n"); fprintf(stderr, "Unable to load png file\n");
return 1; ret = 1;
goto fin;
} }
break; break;
#endif /* OPJ_HAVE_LIBPNG */ #endif /* OPJ_HAVE_LIBPNG */
@ -1903,7 +1917,8 @@ int main(int argc, char **argv)
*/ */
if (!image) { if (!image) {
fprintf(stderr, "Unable to load file: got no image\n"); fprintf(stderr, "Unable to load file: got no image\n");
return 1; ret = 1;
goto fin;
} }
/* Decide if MCT should be used */ /* Decide if MCT should be used */
@ -1914,12 +1929,14 @@ int main(int argc, char **argv)
if ((parameters.tcp_mct == 1) && (image->numcomps < 3)) { if ((parameters.tcp_mct == 1) && (image->numcomps < 3)) {
fprintf(stderr, "RGB->YCC conversion cannot be used:\n"); fprintf(stderr, "RGB->YCC conversion cannot be used:\n");
fprintf(stderr, "Input image has less than 3 components\n"); fprintf(stderr, "Input image has less than 3 components\n");
return 1; ret = 1;
goto fin;
} }
if ((parameters.tcp_mct == 2) && (!parameters.mct_data)) { if ((parameters.tcp_mct == 2) && (!parameters.mct_data)) {
fprintf(stderr, "Custom MCT has been set but no array-based MCT\n"); fprintf(stderr, "Custom MCT has been set but no array-based MCT\n");
fprintf(stderr, "has been provided. Aborting.\n"); fprintf(stderr, "has been provided. Aborting.\n");
return 1; ret = 1;
goto fin;
} }
} }
@ -1959,13 +1976,15 @@ int main(int argc, char **argv)
fprintf(stderr, "failed to encode image: opj_setup_encoder\n"); fprintf(stderr, "failed to encode image: opj_setup_encoder\n");
opj_destroy_codec(l_codec); opj_destroy_codec(l_codec);
opj_image_destroy(image); opj_image_destroy(image);
return 1; ret = 1;
goto fin;
} }
/* open a byte stream for writing and allocate memory for all tiles */ /* open a byte stream for writing and allocate memory for all tiles */
l_stream = opj_stream_create_default_file_stream(parameters.outfile, OPJ_FALSE); l_stream = opj_stream_create_default_file_stream(parameters.outfile, OPJ_FALSE);
if (! l_stream) { if (! l_stream) {
return 1; ret = 1;
goto fin;
} }
/* encode the image */ /* encode the image */
@ -1978,7 +1997,8 @@ int main(int argc, char **argv)
OPJ_UINT32 l_data_size = 512 * 512 * 3; OPJ_UINT32 l_data_size = 512 * 512 * 3;
l_data = (OPJ_BYTE*) calloc(1, l_data_size); l_data = (OPJ_BYTE*) calloc(1, l_data_size);
if (l_data == NULL) { if (l_data == NULL) {
goto fails; ret = 1;
goto fin;
} }
for (i = 0; i < l_nb_tiles; ++i) { for (i = 0; i < l_nb_tiles; ++i) {
if (! opj_write_tile(l_codec, i, l_data, l_data_size, l_stream)) { if (! opj_write_tile(l_codec, i, l_data, l_data_size, l_stream)) {
@ -1987,7 +2007,8 @@ int main(int argc, char **argv)
opj_stream_destroy(l_stream); opj_stream_destroy(l_stream);
opj_destroy_codec(l_codec); opj_destroy_codec(l_codec);
opj_image_destroy(image); opj_image_destroy(image);
return 1; ret = 1;
goto fin;
} }
} }
free(l_data); free(l_data);
@ -2008,7 +2029,8 @@ int main(int argc, char **argv)
opj_image_destroy(image); opj_image_destroy(image);
fprintf(stderr, "failed to encode image\n"); fprintf(stderr, "failed to encode image\n");
remove(parameters.outfile); remove(parameters.outfile);
return 1; ret = 1;
goto fin;
} }
num_compressed_files++; num_compressed_files++;
@ -2024,26 +2046,15 @@ int main(int argc, char **argv)
} }
/* free user parameters structure */
if (parameters.cp_comment) {
free(parameters.cp_comment);
}
if (parameters.cp_matrice) {
free(parameters.cp_matrice);
}
if (raw_cp.rawComps) {
free(raw_cp.rawComps);
}
t = opj_clock() - t; t = opj_clock() - t;
if (num_compressed_files) { if (num_compressed_files) {
fprintf(stdout, "encode time: %d ms \n", fprintf(stdout, "encode time: %d ms \n",
(int)((t * 1000.0) / (OPJ_FLOAT64)num_compressed_files)); (int)((t * 1000.0) / (OPJ_FLOAT64)num_compressed_files));
} }
return 0; ret = 0;
fails: fin:
if (parameters.cp_comment) { if (parameters.cp_comment) {
free(parameters.cp_comment); free(parameters.cp_comment);
} }
@ -2065,5 +2076,5 @@ fails:
} }
free(dirptr); free(dirptr);
} }
return 1; return ret;
} }

View File

@ -2696,17 +2696,20 @@ OPJ_BOOL opj_dwt_decode_partial_97(opj_tcd_tilecomp_t* OPJ_RESTRICT tilec,
/* overflow check */ /* overflow check */
if (l_data_size > (SIZE_MAX - 5U)) { if (l_data_size > (SIZE_MAX - 5U)) {
/* FIXME event manager error callback */ /* FIXME event manager error callback */
opj_sparse_array_int32_free(sa);
return OPJ_FALSE; return OPJ_FALSE;
} }
l_data_size += 5U; l_data_size += 5U;
/* overflow check */ /* overflow check */
if (l_data_size > (SIZE_MAX / sizeof(opj_v4_t))) { if (l_data_size > (SIZE_MAX / sizeof(opj_v4_t))) {
/* FIXME event manager error callback */ /* FIXME event manager error callback */
opj_sparse_array_int32_free(sa);
return OPJ_FALSE; return OPJ_FALSE;
} }
h.wavelet = (opj_v4_t*) opj_aligned_malloc(l_data_size * sizeof(opj_v4_t)); h.wavelet = (opj_v4_t*) opj_aligned_malloc(l_data_size * sizeof(opj_v4_t));
if (!h.wavelet) { if (!h.wavelet) {
/* FIXME event manager error callback */ /* FIXME event manager error callback */
opj_sparse_array_int32_free(sa);
return OPJ_FALSE; return OPJ_FALSE;
} }
v.wavelet = h.wavelet; v.wavelet = h.wavelet;