git: 3c9405052568 - stable/13 - bsdinstall: Replace correct, but fragile, string builder with open_memstream.

From: John Baldwin <jhb_at_FreeBSD.org>
Date: Fri, 05 Jan 2024 00:23:04 UTC
The branch stable/13 has been updated by jhb:

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

commit 3c9405052568fcc33ed815b0913aab432b2cb912
Author:     John Baldwin <jhb@FreeBSD.org>
AuthorDate: 2023-06-27 17:19:32 +0000
Commit:     John Baldwin <jhb@FreeBSD.org>
CommitDate: 2024-01-04 23:16:48 +0000

    bsdinstall: Replace correct, but fragile, string builder with open_memstream.
    
    The old one triggered a false positive -Warray-bounds from GCC (the
    compiler assumed len was always 0), but it was also fragile with
    manually computed lengths paired with strcat vs using a string
    builder.
    
    Differential Revision:  https://reviews.freebsd.org/D40658
    
    (cherry picked from commit f66a8328c3effcb4fbd7807b798d0288b865421d)
---
 usr.sbin/bsdinstall/partedit/scripted.c | 21 ++++++++++++---------
 1 file changed, 12 insertions(+), 9 deletions(-)

diff --git a/usr.sbin/bsdinstall/partedit/scripted.c b/usr.sbin/bsdinstall/partedit/scripted.c
index 08cabf4e0989..69fba8f2cfa8 100644
--- a/usr.sbin/bsdinstall/partedit/scripted.c
+++ b/usr.sbin/bsdinstall/partedit/scripted.c
@@ -194,23 +194,26 @@ int parse_disk_config(char *input)
 int
 scripted_editor(int argc, const char **argv)
 {
-	char *token;
-	int i, error = 0, len = 0;
+	FILE *fp;
+	char *input, *token;
+	size_t len;
+	int i, error = 0;
 
-	for (i = 1; i < argc; i++)
-		len += strlen(argv[i]) + 1;
-	char inputbuf[len], *input = inputbuf;
-	strcpy(input, argv[1]);
+	fp = open_memstream(&input, &len);
+	fputs(argv[1], fp);
 	for (i = 2; i < argc; i++) {
-		strcat(input, " ");
-		strcat(input, argv[i]);
+		fprintf(fp, " %s", argv[i]);
 	}
+	fclose(fp);
 
 	while ((token = strsep(&input, ";")) != NULL) {
 		error = parse_disk_config(token);
-		if (error != 0)
+		if (error != 0) {
+			free(input);
 			return (error);
+		}
 	}
+	free(input);
 
 	return (0);
 }