git: 2f6799782529 - stable/12 - loader: zfs.c is missing malloc checks, fix it

From: Kyle Evans <kevans_at_FreeBSD.org>
Date: Fri, 08 Oct 2021 01:16:23 UTC
The branch stable/12 has been updated by kevans:

URL: https://cgit.FreeBSD.org/src/commit/?id=2f6799782529d18e13eb9bb102b942338a2829c8

commit 2f6799782529d18e13eb9bb102b942338a2829c8
Author:     Toomas Soome <tsoome@FreeBSD.org>
AuthorDate: 2019-11-03 14:36:16 +0000
Commit:     Kyle Evans <kevans@FreeBSD.org>
CommitDate: 2021-10-08 01:15:58 +0000

    loader: zfs.c is missing malloc checks, fix it
    
    malloc() can return NULL, we need to check the return value.
    
    (cherry picked from commit 21da9f14f6144429874a286bfe21a16cbb0dbabd)
---
 stand/libsa/zfs/zfs.c | 20 +++++++++++---------
 1 file changed, 11 insertions(+), 9 deletions(-)

diff --git a/stand/libsa/zfs/zfs.c b/stand/libsa/zfs/zfs.c
index 9386dd7b3729..a8a02c5697cb 100644
--- a/stand/libsa/zfs/zfs.c
+++ b/stand/libsa/zfs/zfs.c
@@ -110,9 +110,10 @@ zfs_open(const char *upath, struct open_file *f)
 		return (EINVAL);
 
 	/* allocate file system specific data structure */
-	fp = malloc(sizeof(struct file));
-	bzero(fp, sizeof(struct file));
-	f->f_fsdata = (void *)fp;
+	fp = calloc(1, sizeof(struct file));
+	if (fp == NULL)
+		return (ENOMEM);
+	f->f_fsdata = fp;
 
 	rc = zfs_lookup(mount, upath, &fp->f_dnode);
 	fp->f_seekp = 0;
@@ -129,9 +130,7 @@ zfs_close(struct open_file *f)
 	struct file *fp = (struct file *)f->f_fsdata;
 
 	dnode_cache_obj = NULL;
-	f->f_fsdata = (void *)0;
-	if (fp == (struct file *)0)
-		return (0);
+	f->f_fsdata = NULL;
 
 	free(fp);
 	return (0);
@@ -250,7 +249,9 @@ zfs_readdir(struct open_file *f, struct dirent *d)
 				return (rc);
 
 			fp->f_seekp = bsize;
-			fp->f_zap_leaf = (zap_leaf_phys_t *)malloc(bsize);
+			fp->f_zap_leaf = malloc(bsize);
+			if (fp->f_zap_leaf == NULL)
+				return (ENOMEM);
 			rc = dnode_read(spa, &fp->f_dnode,
 					fp->f_seekp,
 					fp->f_zap_leaf,
@@ -684,8 +685,9 @@ zfs_dev_open(struct open_file *f, ...)
 		return (ENXIO);
 	mount = malloc(sizeof(*mount));
 	if (mount == NULL)
-		return (ENOMEM);
-	rv = zfs_mount(spa, dev->root_guid, mount);
+		rv = ENOMEM;
+	else
+		rv = zfs_mount(spa, dev->root_guid, mount);
 	if (rv != 0) {
 		free(mount);
 		return (rv);