From e692e0fb79324c0bb360e6c0afc1e85e6399e07c Mon Sep 17 00:00:00 2001 From: Panu Matilainen Date: Apr 23 2020 14:26:58 +0000 Subject: Fix resource leaks on zstd open error paths If zstd stream initialization fails, the opened fd and the stream itself are leaked. Handle error exit in a central label. --- diff --git a/rpmio/rpmio.c b/rpmio/rpmio.c index e051c98..ce1be4e 100644 --- a/rpmio/rpmio.c +++ b/rpmio/rpmio.c @@ -1128,13 +1128,13 @@ static rpmzstd rpmzstdNew(int fdno, const char *fmode) if ((flags & O_ACCMODE) == O_RDONLY) { /* decompressing */ if ((_stream = (void *) ZSTD_createDStream()) == NULL || ZSTD_isError(ZSTD_initDStream(_stream))) { - return NULL; + goto err; } nb = ZSTD_DStreamInSize(); } else { /* compressing */ if ((_stream = (void *) ZSTD_createCStream()) == NULL || ZSTD_isError(ZSTD_initCStream(_stream, level))) { - return NULL; + goto err; } nb = ZSTD_CStreamOutSize(); } @@ -1149,6 +1149,14 @@ static rpmzstd rpmzstdNew(int fdno, const char *fmode) zstd->b = xmalloc(nb); return zstd; + +err: + fclose(fp); + if ((flags & O_ACCMODE) == O_RDONLY) + ZSTD_freeDStream(_stream); + else + ZSTD_freeCStream(_stream); + return NULL; } static FD_t zstdFdopen(FD_t fd, int fdno, const char * fmode)