]> granicus.if.org Git - graphviz/commitdiff
fix resource leakage in acyclic
authorMatthew Fernandez <matthew.fernandez@gmail.com>
Sun, 13 Sep 2020 03:08:30 +0000 (20:08 -0700)
committerMatthew Fernandez <matthew.fernandez@gmail.com>
Sat, 19 Sep 2020 16:42:59 +0000 (09:42 -0700)
This addresses the following Coverity warning:

  Error: RESOURCE_LEAK (CWE-772): [#def47]
  graphviz-2.40.1/cmd/tools/acyclic.c:151: alloc_fn: Storage is returned from allocation function "openFile".
  graphviz-2.40.1/cmd/tools/acyclic.c:129:5: alloc_fn: Storage is returned from allocation function "fopen".
  graphviz-2.40.1/cmd/tools/acyclic.c:129:5: var_assign: Assigning: "fp" = "fopen(name, mode)".
  graphviz-2.40.1/cmd/tools/acyclic.c:139:5: return_alloc: Returning allocated memory "fp".
  graphviz-2.40.1/cmd/tools/acyclic.c:151: var_assign: Assigning: "outFile" = storage returned from "openFile(optarg, "w")".
  graphviz-2.40.1/cmd/tools/acyclic.c:151: overwrite_var: Overwriting "outFile" in "outFile = openFile(optarg, "w")" leaks the storage that "outFile" points to.
  #  149|     switch (c) {
  #  150|     case 'o':
  #  151|->       outFile = openFile(optarg, "w");
  #  152|         break;
  #  153|     case 'n':

Related to #1464.

cmd/tools/acyclic.c

index 55a567789e515e450c993de11cf2ddc5777f1360..e0864dee3be3f509b32ce693da98e4f5fe80731e 100644 (file)
@@ -138,6 +138,8 @@ static void init(int argc, char *argv[])
     while ((c = getopt(argc, argv, ":vno:")) != -1)
        switch (c) {
        case 'o':
+           if (outFile != NULL)
+               fclose(outFile);
            outFile = openFile(optarg, "w");
            break;
        case 'n':