git: 48a53cc48495 - main - sort: use asprintf(3) instead of malloc + snprintf(3)

From: Baptiste Daroussin <bapt_at_FreeBSD.org>
Date: Thu, 13 Oct 2022 08:36:48 UTC
The branch main has been updated by bapt:

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

commit 48a53cc4849555f1a0b805adddb9f517a305a2ae
Author:     Baptiste Daroussin <bapt@FreeBSD.org>
AuthorDate: 2022-10-13 08:34:57 +0000
Commit:     Baptiste Daroussin <bapt@FreeBSD.org>
CommitDate: 2022-10-13 08:34:57 +0000

    sort: use asprintf(3) instead of malloc + snprintf(3)
---
 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);