f6faf3
From ed6c5573c09611ff9522ed290ef9d1ba717d8019 Mon Sep 17 00:00:00 2001
f6faf3
Message-Id: <ed6c5573c09611ff9522ed290ef9d1ba717d8019.1574331915.git.pmatilai@redhat.com>
f6faf3
From: Panu Matilainen <pmatilai@redhat.com>
f6faf3
Date: Thu, 21 Nov 2019 12:22:45 +0200
f6faf3
Subject: [PATCH] Fix resource leaks on zstd open error paths
f6faf3
f6faf3
If zstd stream initialization fails, the opened fd and the stream
f6faf3
itself are leaked. Handle error exit in a central label.
f6faf3
---
f6faf3
 rpmio/rpmio.c | 12 ++++++++++--
f6faf3
 1 file changed, 10 insertions(+), 2 deletions(-)
f6faf3
f6faf3
diff --git a/rpmio/rpmio.c b/rpmio/rpmio.c
f6faf3
index 243942411..10ba20cd6 100644
f6faf3
--- a/rpmio/rpmio.c
f6faf3
+++ b/rpmio/rpmio.c
f6faf3
@@ -1128,13 +1128,13 @@ static rpmzstd rpmzstdNew(int fdno, const char *fmode)
f6faf3
     if ((flags & O_ACCMODE) == O_RDONLY) {	/* decompressing */
f6faf3
 	if ((_stream = (void *) ZSTD_createDStream()) == NULL
f6faf3
 	 || ZSTD_isError(ZSTD_initDStream(_stream))) {
f6faf3
-	    return NULL;
f6faf3
+	    goto err;
f6faf3
 	}
f6faf3
 	nb = ZSTD_DStreamInSize();
f6faf3
     } else {					/* compressing */
f6faf3
 	if ((_stream = (void *) ZSTD_createCStream()) == NULL
f6faf3
 	 || ZSTD_isError(ZSTD_initCStream(_stream, level))) {
f6faf3
-	    return NULL;
f6faf3
+	    goto err;
f6faf3
 	}
f6faf3
 	nb = ZSTD_CStreamOutSize();
f6faf3
     }
f6faf3
@@ -1149,6 +1149,14 @@ static rpmzstd rpmzstdNew(int fdno, const char *fmode)
f6faf3
     zstd->b = xmalloc(nb);
f6faf3
 
f6faf3
     return zstd;
f6faf3
+
f6faf3
+err:
f6faf3
+    fclose(fp);
f6faf3
+    if ((flags & O_ACCMODE) == O_RDONLY)
f6faf3
+	ZSTD_freeDStream(_stream);
f6faf3
+    else
f6faf3
+	ZSTD_freeCStream(_stream);
f6faf3
+    return NULL;
f6faf3
 }
f6faf3
 
f6faf3
 static FD_t zstdFdopen(FD_t fd, int fdno, const char * fmode)
f6faf3
-- 
f6faf3
2.23.0
f6faf3