Skip site navigation (1)Skip section navigation (2)
Date:      Mon, 16 Oct 2023 23:32:19 GMT
From:      John Baldwin <jhb@FreeBSD.org>
To:        src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org
Subject:   git: 51749e05e96e - main - bsdinstall partedit: Replace malloc + sprintf with asprintf
Message-ID:  <202310162332.39GNWJEO094696@gitrepo.freebsd.org>

next in thread | raw e-mail | index | archive | help
The branch main has been updated by jhb:

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

commit 51749e05e96eb07134a38984a8c06608b20f07ea
Author:     John Baldwin <jhb@FreeBSD.org>
AuthorDate: 2023-10-16 23:25:15 +0000
Commit:     John Baldwin <jhb@FreeBSD.org>
CommitDate: 2023-10-16 23:25:15 +0000

    bsdinstall partedit: Replace malloc + sprintf with asprintf
    
    This avoids potential bugs with the length passed to malloc not
    matching the string written via sprintf.
    
    Reviewed by:    emaste
    Differential Revision:  https://reviews.freebsd.org/D42238
---
 usr.sbin/bsdinstall/partedit/gpart_ops.c | 10 +++-------
 usr.sbin/bsdinstall/partedit/partedit.c  |  4 ++--
 2 files changed, 5 insertions(+), 9 deletions(-)

diff --git a/usr.sbin/bsdinstall/partedit/gpart_ops.c b/usr.sbin/bsdinstall/partedit/gpart_ops.c
index 7f34819a3d4d..7395030b26aa 100644
--- a/usr.sbin/bsdinstall/partedit/gpart_ops.c
+++ b/usr.sbin/bsdinstall/partedit/gpart_ops.c
@@ -732,18 +732,16 @@ set_default_part_metadata(const char *name, const char *scheme,
 		}
 
 		if (newfs != NULL && newfs[0] != '\0') {
-			md->newfs = malloc(strlen(newfs) + strlen(" /dev/") +
-			    strlen(mountpoint) + 5 + strlen(name) + 1);
 			if (strcmp("freebsd-zfs", type) == 0) {
 				zpool_name = strdup((strlen(mountpoint) == 1) ?
 				    "root" : &mountpoint[1]);
 				for (i = 0; zpool_name[i] != 0; i++)
 					if (!isalnum(zpool_name[i]))
 						zpool_name[i] = '_';
-				sprintf(md->newfs, "%s %s /dev/%s", newfs,
+				asprintf(&md->newfs, "%s %s /dev/%s", newfs,
 				    zpool_name, name);
 			} else {
-				sprintf(md->newfs, "%s /dev/%s", newfs, name);
+				asprintf(&md->newfs, "%s /dev/%s", newfs, name);
 			}
 		}
 	}
@@ -780,9 +778,7 @@ set_default_part_metadata(const char *name, const char *scheme,
 		if (strcmp("freebsd-zfs", type) == 0) {
 			md->fstab->fs_spec = strdup(zpool_name);
 		} else {
-			md->fstab->fs_spec = malloc(strlen(name) +
-			    strlen("/dev/") + 1);
-			sprintf(md->fstab->fs_spec, "/dev/%s", name);
+			asprintf(&md->fstab->fs_spec, "/dev/%s", name);
 		}
 		md->fstab->fs_file = strdup(mountpoint);
 		/* Get VFS from text after freebsd-, if possible */
diff --git a/usr.sbin/bsdinstall/partedit/partedit.c b/usr.sbin/bsdinstall/partedit/partedit.c
index bb2580789fe8..b6c81ad9fc25 100644
--- a/usr.sbin/bsdinstall/partedit/partedit.c
+++ b/usr.sbin/bsdinstall/partedit/partedit.c
@@ -360,8 +360,8 @@ apply_changes(struct gmesh *mesh)
 	TAILQ_FOREACH(md, &part_metadata, metadata) {
 		if (md->newfs != NULL) {
 			char *item;
-			item = malloc(255);
-			sprintf(item, "Initializing %s", md->name);
+
+			asprintf(&item, "Initializing %s", md->name);
 			minilabel[i] = item;
 			miniperc[i]  = BSDDIALOG_MG_PENDING;
 			i++;



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