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