richardphibel / rpms / rpm

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