Closed memory leaks with demo/demo -iso_ms and updated.

Inspired by Coverity CID 12559 and 12560.
This commit is contained in:
Thomas Schmitt 2015-10-12 19:45:46 +02:00
parent 4838cd59a7
commit 93e1fc52d0
1 changed files with 38 additions and 20 deletions

View File

@ -799,14 +799,14 @@ void iso_ms_usage(char **argv)
int gesture_iso_ms(int argc, char **argv) int gesture_iso_ms(int argc, char **argv)
{ {
int result; int result, return_val = 1, initialized = 0;
IsoImage *image; IsoImage *image = NULL;
IsoDataSource *src; IsoDataSource *src = NULL;
struct burn_source *burn_src; struct burn_source *burn_src = NULL;
unsigned char buf[2048]; unsigned char buf[2048];
FILE *fp = NULL; FILE *fp = NULL;
IsoWriteOpts *opts; IsoWriteOpts *opts = NULL;
IsoReadOpts *ropts; IsoReadOpts *ropts = NULL;
uint32_t ms_block; uint32_t ms_block;
if (argc < 6) { if (argc < 6) {
@ -826,20 +826,26 @@ int gesture_iso_ms(int argc, char **argv)
goto ex; goto ex;
} }
iso_init(); result = iso_init();
if (result < 0) {
demo_report_iso_err(result, "Cannot init libisofs");
goto ex;
}
initialized = 1;
iso_set_msgs_severities("NEVER", "ALL", ""); iso_set_msgs_severities("NEVER", "ALL", "");
/* create the data source to accesss previous image */ /* create the data source to accesss previous image */
result = iso_data_source_new_from_file(argv[3], &src); result = iso_data_source_new_from_file(argv[3], &src);
if (result < 0) { if (result < 0) {
printf ("Error creating data source\n"); demo_report_iso_err(result, "Error creating data source");
goto ex; goto ex;
} }
/* create the image context */ /* create the image context */
result = iso_image_new("volume_id", &image); result = iso_image_new("volume_id", &image);
if (result < 0) { if (result < 0) {
printf ("Error creating image\n"); demo_report_iso_err(result, "Error creating image");
goto ex; goto ex;
} }
iso_tree_set_follow_symlinks(image, 0); iso_tree_set_follow_symlinks(image, 0);
@ -854,23 +860,25 @@ int gesture_iso_ms(int argc, char **argv)
iso_read_opts_set_start_block(ropts, atoi(argv[1])); iso_read_opts_set_start_block(ropts, atoi(argv[1]));
result = iso_image_import(image, src, ropts, NULL); result = iso_image_import(image, src, ropts, NULL);
iso_read_opts_free(ropts); iso_read_opts_free(ropts);
ropts = NULL;
iso_data_source_unref(src); iso_data_source_unref(src);
src = NULL;
if (result < 0) { if (result < 0) {
printf ("Error importing previous session %d\n", result); demo_report_iso_err(result, "Error importing previous session");
goto ex; goto ex;
} }
/* add new dir */ /* add new dir */
result = iso_tree_add_dir_rec(image, iso_image_get_root(image), argv[4]); result = iso_tree_add_dir_rec(image, iso_image_get_root(image), argv[4]);
if (result < 0) { if (result < 0) {
printf ("Error adding directory %d\n", result); demo_report_iso_err(result, "Error adding directory");
goto ex; goto ex;
} }
/* generate a multisession image with new contents */ /* generate a multisession image with new contents */
result = iso_write_opts_new(&opts, 1); result = iso_write_opts_new(&opts, 1);
if (result < 0) { if (result < 0) {
printf("Cant create write opts, error %d\n", result); demo_report_iso_err(result, "Cannot create write opts");
goto ex; goto ex;
} }
@ -885,6 +893,7 @@ int gesture_iso_ms(int argc, char **argv)
goto ex; goto ex;
} }
iso_write_opts_free(opts); iso_write_opts_free(opts);
opts = NULL;
while (burn_src->read_xt(burn_src, buf, 2048) == 2048) { while (burn_src->read_xt(burn_src, buf, 2048) == 2048) {
result = fwrite(buf, 1, 2048, fp); result = fwrite(buf, 1, 2048, fp);
@ -893,17 +902,26 @@ int gesture_iso_ms(int argc, char **argv)
goto ex; goto ex;
} }
} }
fclose(fp);
burn_src->free_data(burn_src); return_val = 0;
free(burn_src);
iso_image_unref(image);
iso_finish();
return 0;
ex:; ex:;
if (burn_src != NULL) {
burn_src->free_data(burn_src);
free(burn_src);
}
if (opts != NULL)
iso_write_opts_free(opts);
if (image)
iso_image_unref(image);
if (ropts != NULL)
iso_read_opts_free(ropts);
if (src != NULL)
iso_data_source_unref(src);
if (initialized)
iso_finish();
if (fp != NULL) if (fp != NULL)
fclose(fp); fclose(fp);
return 1; return return_val;
} }