]> granicus.if.org Git - graphviz/commitdiff
fix resource leakage in gvpack
authorMatthew Fernandez <matthew.fernandez@gmail.com>
Sun, 13 Sep 2020 03:20:53 +0000 (20:20 -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): [#def60]
  graphviz-2.40.1/cmd/tools/gvpack.c:239: alloc_fn: Storage is returned from allocation function "openFile".
  graphviz-2.40.1/cmd/tools/gvpack.c:124:5: alloc_fn: Storage is returned from allocation function "fopen".
  graphviz-2.40.1/cmd/tools/gvpack.c:124:5: var_assign: Assigning: "fp" = "fopen(name, mode)".
  graphviz-2.40.1/cmd/tools/gvpack.c:134:5: return_alloc: Returning allocated memory "fp".
  graphviz-2.40.1/cmd/tools/gvpack.c:239: var_assign: Assigning: "outfp" = storage returned from "openFile(optarg, "w")".
  graphviz-2.40.1/cmd/tools/gvpack.c:239: overwrite_var: Overwriting "outfp" in "outfp = openFile(optarg, "w")" leaks the storage that "outfp" points to.
  #  237|         break;
  #  238|     case 'o':
  #  239|->       outfp = openFile(optarg, "w");
  #  240|         break;
  #  241|     case 'u':

Related to #1464.

cmd/tools/gvpack.c

index f2cd008b535b653db3bf0be06c7e4c34f4b21398..598d838bf9e5f99184111ffceb1074893a51d897 100644 (file)
@@ -221,6 +221,8 @@ static void init(int argc, char *argv[], pack_info* pinfo)
            setUInt(&pinfo->margin, optarg);
            break;
        case 'o':
+           if (outfp != NULL)
+               fclose(outfp);
            outfp = openFile(optarg, "w");
            break;
        case 'u':