git: f94513a3a36b - main - pac: Use strdup and asprintf in place of dubious string building

From: John Baldwin <jhb_at_FreeBSD.org>
Date: Fri, 03 Jan 2025 15:48:59 UTC
The branch main has been updated by jhb:

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

commit f94513a3a36b50823c3918c93ee5c6bf5f525e91
Author:     John Baldwin <jhb@FreeBSD.org>
AuthorDate: 2025-01-03 15:39:44 +0000
Commit:     John Baldwin <jhb@FreeBSD.org>
CommitDate: 2025-01-03 15:44:22 +0000

    pac: Use strdup and asprintf in place of dubious string building
    
    GCC 14 warned about transposed arguments to calloc, but these cases
    are better served by more abstract string functions.
---
 usr.sbin/lpr/pac/pac.c | 9 +++------
 1 file changed, 3 insertions(+), 6 deletions(-)

diff --git a/usr.sbin/lpr/pac/pac.c b/usr.sbin/lpr/pac/pac.c
index 1cc0e050a99e..22ed8bf7e3bb 100644
--- a/usr.sbin/lpr/pac/pac.c
+++ b/usr.sbin/lpr/pac/pac.c
@@ -327,8 +327,7 @@ enter(const char name[])
 	h = hash(name);
 	hcount++;
 	hp = (struct hent *) calloc(sizeof *hp, (size_t)1);
-	hp->h_name = (char *) calloc(sizeof(char), strlen(name)+1);
-	strcpy(hp->h_name, name);
+	hp->h_name = strdup(name);
 	hp->h_feetpages = 0.0;
 	hp->h_count = 0;
 	hp->h_link = hashtab[h];
@@ -429,10 +428,8 @@ chkprinter(const char *ptrname)
 		errx(3, "accounting not enabled for printer %s", ptrname);
 	if (!pflag && pp->price100)
 		price = pp->price100/10000.0;
-	sumfile = (char *) calloc(sizeof(char), strlen(acctfile)+5);
+	asprintf(&sumfile, "%s_sum", acctfile);
 	if (sumfile == NULL)
-		errx(1, "calloc failed");
-	strcpy(sumfile, acctfile);
-	strcat(sumfile, "_sum");
+		errx(1, "asprintf failed");
 	return(1);
 }