Skip to content

Commit 3e89a2b

Browse files
committed
midi2raw: do not leak outfile upon failure. do not fclose() outfile if it is stdout.
1 parent 16f6e85 commit 3e89a2b

File tree

1 file changed

+12
-7
lines changed

1 file changed

+12
-7
lines changed

tests/midi2raw.c

+12-7
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ main (int argc, char *argv[])
2626
int channels = 2;
2727
int volume = 100;
2828
FILE * output = stdout;
29+
const char *outname = NULL;
2930
char * cfgfile = NULL;
3031
int arg;
3132
MidIStream *stream;
@@ -79,12 +80,7 @@ main (int argc, char *argv[])
7980
else if (!strcmp(argv[arg], "-o"))
8081
{
8182
if (++arg >= argc) break;
82-
output = fopen (argv[arg], "wb");
83-
if (output == NULL)
84-
{
85-
fprintf (stderr, "Could not open output file %s\n", argv[arg]);
86-
return 1;
87-
}
83+
outname = argv[arg];
8884
}
8985
else if (!strcmp(argv[arg], "-cfg"))
9086
{
@@ -150,6 +146,15 @@ main (int argc, char *argv[])
150146
return 1;
151147
}
152148

149+
if (outname && !(output = fopen (outname, "wb")))
150+
{
151+
fprintf (stderr, "Could not open output file %s\n", outname);
152+
mid_song_free (song);
153+
mid_exit ();
154+
free (cfgfile);
155+
return 1;
156+
}
157+
153158
mid_song_set_volume (song, volume);
154159
mid_song_start (song);
155160

@@ -159,7 +164,7 @@ main (int argc, char *argv[])
159164
mid_song_free (song);
160165
mid_exit ();
161166
free (cfgfile);
162-
fclose(output);
167+
if (outname) fclose(output);
163168

164169
return 0;
165170
}

0 commit comments

Comments
 (0)