git: 230990991965 - main - bsdinstall: Handle errors from geom_gettree.

From: John Baldwin <jhb_at_FreeBSD.org>
Date: Wed, 28 Jun 2023 18:11:15 UTC
The branch main has been updated by jhb:

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

commit 23099099196548550461ba427dcf09dcfb01878d
Author:     John Baldwin <jhb@FreeBSD.org>
AuthorDate: 2023-06-28 18:11:00 +0000
Commit:     John Baldwin <jhb@FreeBSD.org>
CommitDate: 2023-06-28 18:11:00 +0000

    bsdinstall: Handle errors from geom_gettree.
    
    geom_gettree probably never fails, and if it does there isn't much of
    a fallback other than aborting partitioning.  However, a few places
    were checking the return value and not doing anything with it
    triggering a unused-but-set-variable warning.  Checking the errors
    resolves the warning.
    
    While here, check for errors in other places that weren't checking for
    them at all, remove a spurious double call (the second call overwrote
    the mesh structure leaking all the pointers from the first), and close
    a few resource leaks on error paths.
    
    Reviewed by:    imp, emaste
    Differential Revision:  https://reviews.freebsd.org/D40779
---
 usr.sbin/bsdinstall/partedit/part_wizard.c | 40 +++++++++++++++++++-----------
 usr.sbin/bsdinstall/partedit/partedit.c    | 22 ++++++++--------
 usr.sbin/bsdinstall/partedit/scripted.c    | 34 +++++++++++++++++--------
 3 files changed, 62 insertions(+), 34 deletions(-)

diff --git a/usr.sbin/bsdinstall/partedit/part_wizard.c b/usr.sbin/bsdinstall/partedit/part_wizard.c
index db78cea768aa..e7b513f3193a 100644
--- a/usr.sbin/bsdinstall/partedit/part_wizard.c
+++ b/usr.sbin/bsdinstall/partedit/part_wizard.c
@@ -65,31 +65,37 @@ part_wizard(const char *fsreq)
 
 startwizard:
 	error = geom_gettree(&mesh);
+	if (error != 0)
+		return (1);
 
 	bsddialog_backtitle(&conf, "FreeBSD Installer");
-	error = geom_gettree(&mesh);
 	disk = boot_disk_select(&mesh);
-	if (disk == NULL)
+	if (disk == NULL) {
+		geom_deletetree(&mesh);
 		return (1);
+	}
 
 	bsddialog_clearterminal();
 	bsddialog_backtitle(&conf, "FreeBSD Installer");
 	schemeroot = wizard_partition(&mesh, disk);
 	free(disk);
+	geom_deletetree(&mesh);
 	if (schemeroot == NULL)
 		return (1);
 
-	geom_deletetree(&mesh);
 	bsddialog_clearterminal();
 	bsddialog_backtitle(&conf, "FreeBSD Installer");
 	error = geom_gettree(&mesh);
+	if (error != 0) {
+		free(schemeroot);
+		return (1);
+	}
 
 	error = wizard_makeparts(&mesh, schemeroot, fstype, 1);
-	if (error)
-		goto startwizard;
 	free(schemeroot);
-
 	geom_deletetree(&mesh);
+	if (error)
+		goto startwizard;
 
 	return (0);
 }
@@ -310,11 +316,13 @@ query:
 
 	if (strcmp(scheme, "MBR") == 0) {
 		struct gmesh submesh;
-		geom_gettree(&submesh);
-		gpart_create(provider_for_name(&submesh, disk),
-		    "freebsd", NULL, NULL, &retval,
-		    choice /* Non-interactive for "Entire Disk" */);
-		geom_deletetree(&submesh);
+
+		if (geom_gettree(&submesh) == 0) {
+			gpart_create(provider_for_name(&submesh, disk),
+			    "freebsd", NULL, NULL, &retval,
+			    choice /* Non-interactive for "Entire Disk" */);
+			geom_deletetree(&submesh);
+		}
 	} else {
 		retval = strdup(disk);
 	}
@@ -334,7 +342,7 @@ wizard_makeparts(struct gmesh *mesh, const char *disk, const char *fstype,
 	struct gmesh submesh;
 	char swapsizestr[10], rootsizestr[10];
 	intmax_t swapsize, available;
-	int retval;
+	int error, retval;
 	struct bsddialog_conf conf;
 
 	if (strcmp(fstype, "zfs") == 0) {
@@ -381,12 +389,16 @@ wizard_makeparts(struct gmesh *mesh, const char *disk, const char *fstype,
 	humanize_number(rootsizestr, 7, available - swapsize - 1024*1024,
 	    "B", HN_AUTOSCALE, HN_NOSPACE | HN_DECIMAL);
 
-	geom_gettree(&submesh);
+	error = geom_gettree(&submesh);
+	if (error != 0)
+		return (error);
 	pp = provider_for_name(&submesh, disk);
 	gpart_create(pp, fsname, rootsizestr, "/", NULL, 0);
 	geom_deletetree(&submesh);
 
-	geom_gettree(&submesh);
+	error = geom_gettree(&submesh);
+	if (error != 0)
+		return (error);
 	pp = provider_for_name(&submesh, disk);
 	gpart_create(pp, "freebsd-swap", swapsizestr, NULL, NULL, 0);
 	geom_deletetree(&submesh);
diff --git a/usr.sbin/bsdinstall/partedit/partedit.c b/usr.sbin/bsdinstall/partedit/partedit.c
index f26aa0085748..ece258b2c070 100644
--- a/usr.sbin/bsdinstall/partedit/partedit.c
+++ b/usr.sbin/bsdinstall/partedit/partedit.c
@@ -63,9 +63,10 @@ sigint_handler(int sig)
 	struct gmesh mesh;
 
 	/* Revert all changes and exit dialog-mode cleanly on SIGINT */
-	geom_gettree(&mesh);
-	gpart_revert_all(&mesh);
-	geom_deletetree(&mesh);
+	if (geom_gettree(&mesh) == 0) {
+		gpart_revert_all(&mesh);
+		geom_deletetree(&mesh);
+	}
 
 	bsddialog_end();
 
@@ -216,16 +217,17 @@ main(int argc, const char **argv)
 	
 	if (prompt == NULL) {
 		error = geom_gettree(&mesh);
-		if (validate_setup()) {
-			error = apply_changes(&mesh);
-		} else {
-			gpart_revert_all(&mesh);
-			error = -1;
+		if (error != 0) {
+			if (validate_setup()) {
+				error = apply_changes(&mesh);
+			} else {
+				gpart_revert_all(&mesh);
+				error = -1;
+			}
+			geom_deletetree(&mesh);
 		}
 	}
 
-	geom_deletetree(&mesh);
-	free(items);
 	bsddialog_end();
 
 	return (error);
diff --git a/usr.sbin/bsdinstall/partedit/scripted.c b/usr.sbin/bsdinstall/partedit/scripted.c
index 62c36724d7c5..2f79d643fd08 100644
--- a/usr.sbin/bsdinstall/partedit/scripted.c
+++ b/usr.sbin/bsdinstall/partedit/scripted.c
@@ -76,6 +76,8 @@ part_config(char *disk, const char *scheme, char *config)
 		scheme = default_scheme();
 
 	error = geom_gettree(&mesh);
+	if (error != 0)
+		return (-1);
 	if (provider_for_name(&mesh, disk) == NULL) {
 		fprintf(stderr, "GEOM provider %s not found\n", disk);
 		geom_deletetree(&mesh);
@@ -97,16 +99,22 @@ part_config(char *disk, const char *scheme, char *config)
 
 	if (strcmp(scheme, "MBR") == 0) {
 		struct gmesh submesh;
-		geom_gettree(&submesh);
-		gpart_create(provider_for_name(&submesh, disk),
-		    "freebsd", NULL, NULL, &disk, 0);
-		geom_deletetree(&submesh);
+
+		if (geom_gettree(&submesh) == 0) {
+			gpart_create(provider_for_name(&submesh, disk),
+			    "freebsd", NULL, NULL, &disk, 0);
+			geom_deletetree(&submesh);
+		}
 	} else {
-		disk= strdup(disk);
+		disk = strdup(disk);
 	}
 
 	geom_deletetree(&mesh);
 	error = geom_gettree(&mesh);
+	if (error != 0) {
+		free(disk);
+		return (-1);
+	}
 
 	/* Create partitions */
 	if (config == NULL) {
@@ -133,6 +141,10 @@ part_config(char *disk, const char *scheme, char *config)
 		    NULL, 0);
 		geom_deletetree(&mesh);
 		error = geom_gettree(&mesh);
+		if (error != 0) {
+			free(disk);
+			return (-1);
+		}
 		size = type = mount = NULL;
 	}
 
@@ -143,8 +155,8 @@ finished:
 	return (0);
 }
 
-static
-int parse_disk_config(char *input)
+static int
+parse_disk_config(char *input)
 {
 	char *ap;
 	char *disk = NULL, *scheme = NULL, *partconfig = NULL;
@@ -184,9 +196,11 @@ int parse_disk_config(char *input)
 
 	if (disk == NULL || strcmp(disk, "DEFAULT") == 0) {
 		struct gmesh mesh;
-		geom_gettree(&mesh);
-		disk = boot_disk_select(&mesh);
-		geom_deletetree(&mesh);
+
+		if (geom_gettree(&mesh) == 0) {
+			disk = boot_disk_select(&mesh);
+			geom_deletetree(&mesh);
+		}
 	}
 
 	return (part_config(disk, scheme, partconfig));