diff options
| author | Yukihiro Matsumoto <[email protected]> | 2012-10-23 19:01:37 +0900 |
|---|---|---|
| committer | Yukihiro Matsumoto <[email protected]> | 2012-10-23 19:01:37 +0900 |
| commit | 52d39e6224382e669b959cf020164d6bb90b9844 (patch) | |
| tree | 84157acbd5e9a57b02462e8bb82e81446f1ca0da /tools | |
| parent | f5b6f03c9e802702c6f5f74bc3e778f3c4d8baca (diff) | |
| download | mruby-52d39e6224382e669b959cf020164d6bb90b9844.tar.gz mruby-52d39e6224382e669b959cf020164d6bb90b9844.zip | |
prevent resource leak (outfile)
Diffstat (limited to 'tools')
| -rw-r--r-- | tools/mrbc/mrbc.c | 51 |
1 files changed, 28 insertions, 23 deletions
diff --git a/tools/mrbc/mrbc.c b/tools/mrbc/mrbc.c index 3c6352f80..2ecb00623 100644 --- a/tools/mrbc/mrbc.c +++ b/tools/mrbc/mrbc.c @@ -70,6 +70,7 @@ parse_args(mrb_state *mrb, int argc, char **argv, struct _args *args) char *infile = NULL; char *outfile = NULL; char **origargv = argv; + int result = 0; static const struct _args args_zero = { 0 }; *args = args_zero; @@ -93,7 +94,8 @@ parse_args(mrb_state *mrb, int argc, char **argv, struct _args *args) args->initname = (*argv) + 2; if (*args->initname == '\0') { printf("%s: Function name is not specified.\n", *origargv); - return -2; + result = -2; + goto exit; } args->dump_type = ((*argv)[1] == 'B') ? DUMP_TYPE_BIN : DUMP_TYPE_CODE; break; @@ -117,8 +119,8 @@ parse_args(mrb_state *mrb, int argc, char **argv, struct _args *args) mrb_show_copyright(mrb); exit(0); } - else return -3; - return 0; + result = -3; + goto exit; default: break; } @@ -127,33 +129,36 @@ parse_args(mrb_state *mrb, int argc, char **argv, struct _args *args) args->filename = infile = *argv; if ((args->rfp = fopen(infile, "r")) == NULL) { printf("%s: Cannot open program file. (%s)\n", *origargv, infile); - return 0; + goto exit; } } } - if (infile == NULL) - return -4; - if (args->check_syntax) - return 0; - - if (outfile == NULL) { - if (strcmp("-", infile) == 0) { - outfile = infile; + if (infile == NULL) { + result = -4; + goto exit; + } + if (!args->check_syntax) { + if (outfile == NULL) { + if (strcmp("-", infile) == 0) { + outfile = infile; + } + else { + outfile = get_outfilename(infile, args->ext); + } } - else { - outfile = get_outfilename(infile, args->ext); + if (strcmp("-", outfile) == 0) { + args->wfp = stdout; + } + else if ((args->wfp = fopen(outfile, "wb")) == NULL) { + printf("%s: Cannot open output file. (%s)\n", *origargv, outfile); + result = -1; + goto exit; } } - if (strcmp("-", outfile) == 0) { - args->wfp = stdout; - } - else if ((args->wfp = fopen(outfile, "wb")) == NULL) { - printf("%s: Cannot open output file. (%s)\n", *origargv, outfile); - return 0; - } - - return 0; + exit: + if (outfile && infile != outfile) free(outfile); + return result; } static void |
