From: Matthew Fernandez Date: Sun, 13 Sep 2020 02:14:50 +0000 (-0700) Subject: fix file handle leakage X-Git-Tag: 2.46.0~20^2^2~73^2~9 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=8b5a3a1c2fe3bb74d8c5ab686684e6728530a050;p=graphviz fix file handle leakage This addresses the following Coverity warnings: Error: RESOURCE_LEAK (CWE-772): [#def30] graphviz-2.40.1/cmd/lefty/lefty.c:469: alloc_fn: Storage is returned from allocation function "fopen". graphviz-2.40.1/cmd/lefty/lefty.c:469: var_assign: Assigning: "fp" = storage returned from "fopen(argv[0], "r")". graphviz-2.40.1/cmd/lefty/lefty.c:464: overwrite_var: Overwriting "fp" in "fp = stdin" leaks the storage that "fp" points to. # 462| usage(0); # 463| else if (strcmp (argv[0], "-") == 0) # 464|-> fp = stdin; # 465| else if (argv[0][0] == '-') { # 466| fprintf (stderr, "option %s unrecognized - ignored\n", argv[0]); Error: RESOURCE_LEAK (CWE-772): [#def31] graphviz-2.40.1/cmd/lefty/lefty.c:469: alloc_fn: Storage is returned from allocation function "fopen". graphviz-2.40.1/cmd/lefty/lefty.c:469: var_assign: Assigning: "fp" = storage returned from "fopen(argv[0], "r")". graphviz-2.40.1/cmd/lefty/lefty.c:469: overwrite_var: Overwriting "fp" in "fp = fopen(argv[0], "r")" leaks the storage that "fp" points to. # 467| } # 468| else { # 469|-> if ((fp = fopen (argv[0], "r")) == NULL) { # 470| fprintf (stderr, "cannot open input file: %s\n", argv[0]); # 471| exit(2); Related to #1464. --- diff --git a/cmd/lefty/lefty.c b/cmd/lefty/lefty.c index dadfc1cc7..e49eaa7ac 100644 --- a/cmd/lefty/lefty.c +++ b/cmd/lefty/lefty.c @@ -466,12 +466,16 @@ static void processargs (int argc, char *argv[]) { } else if (strcmp (argv[0], "-?") == 0) usage(0); - else if (strcmp (argv[0], "-") == 0) + else if (strcmp (argv[0], "-") == 0) { + if (fp != NULL && fp != stdin) + fclose(fp); fp = stdin; - else if (argv[0][0] == '-') { + } else if (argv[0][0] == '-') { fprintf (stderr, "option %s unrecognized - ignored\n", argv[0]); } else { + if (fp != NULL && fp != stdin) + fclose(fp); if ((fp = fopen (argv[0], "r")) == NULL) { fprintf (stderr, "cannot open input file: %s\n", argv[0]); exit(2);