git: 67ca992f5ce6 - stable/13 - sort: use asprintf(3) instead of malloc + snprintf(3)

From: Baptiste Daroussin <bapt_at_FreeBSD.org>
Date: Wed, 19 Oct 2022 08:01:35 UTC
The branch stable/13 has been updated by bapt:

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

commit 67ca992f5ce64ffaeaabf969379579a0b774c5de
Author:     Baptiste Daroussin <bapt@FreeBSD.org>
AuthorDate: 2022-10-13 08:34:57 +0000
Commit:     Baptiste Daroussin <bapt@FreeBSD.org>
CommitDate: 2022-10-19 07:59:30 +0000

    sort: use asprintf(3) instead of malloc + snprintf(3)
    
    (cherry picked from commit 48a53cc4849555f1a0b805adddb9f517a305a2ae)
---
 usr.bin/sort/file.c | 16 +++++++---------
 1 file changed, 7 insertions(+), 9 deletions(-)

diff --git a/usr.bin/sort/file.c b/usr.bin/sort/file.c
index c390b4c2a71f..05058a2509b5 100644
--- a/usr.bin/sort/file.c
+++ b/usr.bin/sort/file.c
@@ -538,28 +538,26 @@ openfile(const char *fn, const char *mode)
 		    S_IRGRP | S_IROTH);
 
 	if (is_tmp && (compress_program != NULL)) {
+		int r;
 		char *cmd;
-		size_t cmdsz;
-
-		cmdsz = strlen(fn) + 128;
-		cmd = sort_malloc(cmdsz);
 
 		fflush(stdout);
 
 		if (mode[0] == 'r')
-			snprintf(cmd, cmdsz - 1, "cat %s | %s -d",
+			r = asprintf(&cmd, "cat %s | %s -d",
 			    fn, compress_program);
 		else if (mode[0] == 'w')
-			snprintf(cmd, cmdsz - 1, "%s > %s",
+			r = asprintf(&cmd, "%s > %s",
 			    compress_program, fn);
 		else
 			err(2, "%s", getstr(7));
 
+		if (r == -1)
+			err(2, "aspritnf()");
+
 		if ((file = popen(cmd, mode)) == NULL)
 			err(2, NULL);
-
-		sort_free(cmd);
-
+		free(cmd);
 	} else
 		if ((file = fopen(fn, mode)) == NULL)
 			err(2, NULL);