git: 0148ccb95a8d - stable/14 - loader: we can only env_discard() existing variable

From: Toomas Soome <tsoome_at_FreeBSD.org>
Date: Mon, 11 Mar 2024 14:13:33 UTC
The branch stable/14 has been updated by tsoome:

URL: https://cgit.FreeBSD.org/src/commit/?id=0148ccb95a8df13d8a1234083490655d40550527

commit 0148ccb95a8df13d8a1234083490655d40550527
Author:     Toomas Soome <tsoome@FreeBSD.org>
AuthorDate: 2024-02-25 22:34:00 +0000
Commit:     Toomas Soome <tsoome@FreeBSD.org>
CommitDate: 2024-03-11 14:08:50 +0000

    loader: we can only env_discard() existing variable
    
    While dropping nvpair from nvstore, we also remove the corresponding
    environment variable. By doing so, we should be careful not to try
    to unset non-existing variable.
    
    Reviewed by:    imp
    MFC after:      2 week
    Differential revision:  https://reviews.freebsd.org/D44083
    
    (cherry picked from commit 1b3f4ccb7dd8600d010fc6a09b09ee7d74872809)
---
 stand/libsa/zfs/zfs.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)

diff --git a/stand/libsa/zfs/zfs.c b/stand/libsa/zfs/zfs.c
index af938fbe1b2b..f3cf60786a23 100644
--- a/stand/libsa/zfs/zfs.c
+++ b/stand/libsa/zfs/zfs.c
@@ -1265,8 +1265,12 @@ zfs_nvstore_unset_impl(void *vdev, const char *name, bool unset_env)
 			rv = zfs_set_bootenv(vdev, spa->spa_bootenv);
 	}
 
-	if (unset_env)
-		env_discard(env_getenv(name));
+	if (unset_env) {
+		struct env_var *ev = env_getenv(name);
+
+		if (ev != NULL)
+			env_discard(ev);
+	}
 	return (rv);
 }