git: 1286974328af - stable/13 - sort: use mkstemp(3) instead of reinventing it

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

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

commit 1286974328afdef7f3419ec830e12804a3a28e80
Author:     Baptiste Daroussin <bapt@FreeBSD.org>
AuthorDate: 2022-10-12 15:57:37 +0000
Commit:     Baptiste Daroussin <bapt@FreeBSD.org>
CommitDate: 2022-10-19 07:59:28 +0000

    sort: use mkstemp(3) instead of reinventing it
    
    MFC After:      1 week
    
    (cherry picked from commit 3f9e5e59bda05acea409fa3121c835a74672de1c)
---
 usr.bin/sort/file.c | 12 ++++++------
 1 file changed, 6 insertions(+), 6 deletions(-)

diff --git a/usr.bin/sort/file.c b/usr.bin/sort/file.c
index ffcd71e9f991..3e5754442ac1 100644
--- a/usr.bin/sort/file.c
+++ b/usr.bin/sort/file.c
@@ -195,15 +195,15 @@ file_is_tmp(const char* fn)
 char *
 new_tmp_file_name(void)
 {
-	static size_t tfcounter = 0;
-	static const char *fn = ".bsdsort.";
 	char *ret;
-	size_t sz;
+	int fd;
 
-	sz = strlen(tmpdir) + 1 + strlen(fn) + 32 + 1;
-	ret = sort_malloc(sz);
+	if (asprintf(&ret, "%s/.bsdsort.XXXXXXXXXX", tmpdir) == -1)
+		err(2, "asprintf()");
+	if ((fd = mkstemp(ret)) == -1)
+		err(2, "mkstemp()");
+	close(fd);
 
-	sprintf(ret, "%s/%s%d.%lu", tmpdir, fn, (int) getpid(), (unsigned long)(tfcounter++));
 	tmp_file_atexit(ret);
 	return (ret);
 }