Skip site navigation (1)Skip section navigation (2)
Date:      Tue, 24 Jan 2023 22:14:27 GMT
From:      Warner Losh <imp@FreeBSD.org>
To:        src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-branches@FreeBSD.org
Subject:   git: 379f5668e24a - stable/13 - stand/zfs: Fix memory leaking on error cases
Message-ID:  <202301242214.30OMEREf089838@gitrepo.freebsd.org>

next in thread | raw e-mail | index | archive | help
The branch stable/13 has been updated by imp:

URL: https://cgit.FreeBSD.org/src/commit/?id=379f5668e24ac173d7ab4cd865e82bccef6b1540

commit 379f5668e24ac173d7ab4cd865e82bccef6b1540
Author:     Warner Losh <imp@FreeBSD.org>
AuthorDate: 2023-01-13 21:20:00 +0000
Commit:     Warner Losh <imp@FreeBSD.org>
CommitDate: 2023-01-24 21:49:46 +0000

    stand/zfs: Fix memory leaking on error cases
    
    Now that we return an allocated zfs_devdesc, we have to free it. These
    frees were missing from the error cases. In addition, simplify the code
    a bit for the out of memory case.
    
    Sponsored by:           Netflix
    Reviewed by:            kevans
    Differential Revision:  https://reviews.freebsd.org/D38006
    
    (cherry picked from commit 5385c7e13b06cb42a28bbf8c0d910b0c2ffddec7)
---
 stand/libsa/zfs/zfs.c | 42 ++++++++++++++++++++++++++----------------
 1 file changed, 26 insertions(+), 16 deletions(-)

diff --git a/stand/libsa/zfs/zfs.c b/stand/libsa/zfs/zfs.c
index 57fecf2f4d68..3d5a392dd03f 100644
--- a/stand/libsa/zfs/zfs.c
+++ b/stand/libsa/zfs/zfs.c
@@ -379,9 +379,9 @@ zfs_readdir(struct open_file *f, struct dirent *d)
 static int
 zfs_mount(const char *dev, const char *path, void **data)
 {
-	struct zfs_devdesc *zfsdev;
+	struct zfs_devdesc *zfsdev = NULL;
 	spa_t *spa;
-	struct zfsmount *mnt;
+	struct zfsmount *mnt = NULL;
 	int rv;
 
 	errno = 0;
@@ -391,36 +391,46 @@ zfs_mount(const char *dev, const char *path, void **data)
 	}
 
 	spa = spa_find_by_dev(zfsdev);
-	if (spa == NULL)
-		return (ENXIO);
+	if (spa == NULL) {
+		rv = ENXIO;
+		goto err;
+	}
 
 	mnt = calloc(1, sizeof(*mnt));
-	if (mnt != NULL && path != NULL)
+	if (mnt == NULL) {
+		rv = ENOMEM;
+		goto err;
+	}
+
+	if (mnt->path != NULL) {
 		mnt->path = strdup(path);
-	rv = errno;
+		if (mnt->path == NULL) {
+			rv = ENOMEM;
+			goto err;
+		}
+	}
 
-	if (mnt != NULL)
-		rv = zfs_mount_impl(spa, zfsdev->root_guid, mnt);
-	free(zfsdev);
+	rv = zfs_mount_impl(spa, zfsdev->root_guid, mnt);
 
-	if (rv == 0 && mnt != NULL && mnt->objset.os_type != DMU_OST_ZFS) {
+	if (rv == 0 && mnt->objset.os_type != DMU_OST_ZFS) {
 		printf("Unexpected object set type %ju\n",
 		    (uintmax_t)mnt->objset.os_type);
 		rv = EIO;
 	}
-
+err:
 	if (rv != 0) {
 		if (mnt != NULL)
 			free(mnt->path);
 		free(mnt);
+		free(zfsdev);
 		return (rv);
 	}
 
-	if (mnt != NULL) {
-		*data = mnt;
-		if (path != NULL)
-			STAILQ_INSERT_TAIL(&zfsmount, mnt, next);
-	}
+	*data = mnt;
+	if (path != NULL)
+		STAILQ_INSERT_TAIL(&zfsmount, mnt, next);
+
+	free(zfsdev);
 
 	return (rv);
 }



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?202301242214.30OMEREf089838>