git: a35c681995e4 - main - zfsbootcfg: fix some error handling bugs in setting/deleting keys

From: Kyle Evans <kevans_at_FreeBSD.org>
Date: Thu, 26 Nov 2026 03:58:19 UTC
The branch main has been updated by kevans:

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

commit a35c681995e49dbd3e2bf73fc832fb7d7eb0b2b7
Author:     Kyle Evans <kevans@FreeBSD.org>
AuthorDate: 2026-11-26 03:57:58 +0000
Commit:     Kyle Evans <kevans@FreeBSD.org>
CommitDate: 2026-11-26 03:57:58 +0000

    zfsbootcfg: fix some error handling bugs in setting/deleting keys
    
    A few bugs dating back to the original bootenv support:
    
     - delete_pair() freed an uninitialized nvlist if it couldn't fetch the
       bootenv, e.g., with a nonexistent pool.
     - A missing else meant that "yes" and "no" were always rejected for
       DATA_TYPE_BOOLEAN_VALUE; only "true", "false" and numbers worked.
     - With -p, the result of printing the bootenv replaced the result of
       setting or deleting a key, so a failed set or delete exited 0.
     - errno was never cleared before strto*(), so a stale errno left behind
       by libzfs could make a valid numeric value fail.
    
    Values for the narrower integer types are still silently truncated
    (e.g., 300 for DATA_TYPE_UINT8 sets 44).  Rejecting them would also
    reject values like -1 for the unsigned types that scripts may rely on,
    so I'm punting for now.
    
    Reported by:    llm-postcr
    Reviewed by:    imp, rmoeller
    Feedback from:  avg
    Fixes:          e307eb94ae52 ("loader: zfs should support [...]")
    Sponsored by:   The FreeBSD Foundation
    Sponsored by:   Klara, Inc.
    Differential Revision:  https://reviews.freebsd.org/D61849
---
 sbin/zfsbootcfg/zfsbootcfg.c | 11 ++++++-----
 1 file changed, 6 insertions(+), 5 deletions(-)

diff --git a/sbin/zfsbootcfg/zfsbootcfg.c b/sbin/zfsbootcfg/zfsbootcfg.c
index 7102b6355ec0..7a4fb5748fc1 100644
--- a/sbin/zfsbootcfg/zfsbootcfg.c
+++ b/sbin/zfsbootcfg/zfsbootcfg.c
@@ -60,6 +60,7 @@ add_pair(const char *name, const char *nvlist, const char *key,
 
 	data = NULL;
 	rv = EINVAL;
+	errno = 0;
 	if (strcmp(type, "DATA_TYPE_STRING") == 0) {
 		data = __DECONST(void *, value);
 		size = strlen(data) + 1;
@@ -145,7 +146,7 @@ add_pair(const char *name, const char *nvlist, const char *key,
 				v = 1;
 			else if (strcasecmp(value, "NO") == 0)
 				v = 0;
-			if (strcasecmp(value, "true") == 0)
+			else if (strcasecmp(value, "true") == 0)
 				v = 1;
 			else if (strcasecmp(value, "false") == 0)
 				v = 0;
@@ -166,13 +167,12 @@ done:
 static int
 delete_pair(const char *name, const char *nvlist, const char *key)
 {
-	void *nv;
+	void *nv = NULL;
 	int rv;
 
 	rv = lzbe_nvlist_get(name, nvlist, &nv);
-	if (rv == 0) {
+	if (rv == 0)
 		rv = lzbe_remove_pair(nv, key);
-	}
 	if (rv == 0)
 		rv = lzbe_nvlist_set(name, nvlist, nv);
 
@@ -297,7 +297,8 @@ main(int argc, char * const *argv)
 	}
 
 	if (print) {
-		rv = lzbe_bootenv_print(name, nvlist, stdout);
+		if (lzbe_bootenv_print(name, nvlist, stdout) != 0)
+			rv = 1;
 	}
 
 	exit(rv != 0);