Skip to content

Commit 3c65c6c

Browse files
committed
Insert free()'s to prevent mem leaks
1 parent 472d271 commit 3c65c6c

2 files changed

Lines changed: 30 additions & 6 deletions

File tree

mcstas-comps/samples/PowderN.comp

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -304,13 +304,25 @@ struct line_info_struct
304304
&& strcasecmp(ext, "res")) return infile;
305305

306306
OUTFILE = malloc(1024);
307-
if (!OUTFILE) return infile;
307+
if (!OUTFILE) {
308+
free(OUTFILE);
309+
return infile;
310+
}
308311
inpath = malloc(1024);
309-
if (!inpath) return infile;
312+
if (!inpath) {
313+
free(OUTFILE);
314+
free(inpath);
315+
return infile;
316+
}
310317

311318
// get input file path from read-table:Open_File
312319
FILE *f_infile = Open_File(infile, "r", inpath);
313-
if (!f_infile) return infile;
320+
if (!f_infile) {
321+
free(OUTFILE);
322+
free(inpath);
323+
free(f_infile);
324+
return infile;
325+
}
314326
fclose(f_infile);
315327

316328
strncpy(OUTFILE, tmpnam(NULL), 1024); // create an output temporary file name

mcstas-comps/samples/Single_crystal.comp

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -413,13 +413,25 @@ struct hkl_data
413413
&& strcasecmp(ext, "res")) return infile;
414414

415415
OUTFILE = malloc(1024);
416-
if (!OUTFILE) return infile;
416+
if (!OUTFILE) {
417+
free(OUTFILE);
418+
return infile;
419+
}
417420
inpath = malloc(1024);
418-
if (!inpath) return infile;
421+
if (!inpath) {
422+
free(OUTFILE);
423+
free(inpath);
424+
return infile;
425+
}
419426

420427
// get input file path from read-table:Open_File
421428
FILE *f_infile = Open_File(infile, "r", inpath);
422-
if (!f_infile) return infile;
429+
if (!f_infile) {
430+
free(OUTFILE);
431+
free(inpath);
432+
free(f_infile);
433+
return infile;
434+
}
423435
fclose(f_infile);
424436

425437
strncpy(OUTFILE, tmpnam(NULL), 1024); // create an output temporary file name

0 commit comments

Comments
 (0)