Skip site navigation (1)Skip section navigation (2)
Date:      Mon, 1 Feb 2021 12:40:38 GMT
From:      Mateusz Guzik <mjg@FreeBSD.org>
To:        src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-branches@FreeBSD.org
Subject:   git: 3eae6412b50b - stable/13 - libc: try to skip memcpy in _gettemp
Message-ID:  <202102011240.111CecuM094761@gitrepo.freebsd.org>

next in thread | raw e-mail | index | archive | help
The branch stable/13 has been updated by mjg:

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

commit 3eae6412b50bbcfe1a7affc26202dc896aedd1ab
Author:     Mateusz Guzik <mjg@FreeBSD.org>
AuthorDate: 2021-01-24 04:34:22 +0000
Commit:     Mateusz Guzik <mjg@FreeBSD.org>
CommitDate: 2021-02-01 12:39:15 +0000

    libc: try to skip memcpy in _gettemp
    
    (cherry picked from commit b22fdf45ff8ef1d1f9a6c28f1d7f59ca4b012da6)
---
 lib/libc/stdio/mktemp.c | 11 ++++++++---
 1 file changed, 8 insertions(+), 3 deletions(-)

diff --git a/lib/libc/stdio/mktemp.c b/lib/libc/stdio/mktemp.c
index c47e0221e5cd..0ede3058a3d5 100644
--- a/lib/libc/stdio/mktemp.c
+++ b/lib/libc/stdio/mktemp.c
@@ -123,6 +123,7 @@ _gettemp(int dfd, char *path, int *doopen, int domkdir, int slen, int oflags)
 	struct stat sbuf;
 	uint32_t rand;
 	char carrybuf[MAXPATHLEN];
+	int saved;
 
 	if ((doopen != NULL && domkdir) || slen < 0 ||
 	    (oflags & ~(O_APPEND | O_DIRECT | O_SHLOCK | O_EXLOCK | O_SYNC |
@@ -151,9 +152,7 @@ _gettemp(int dfd, char *path, int *doopen, int domkdir, int slen, int oflags)
 	}
 	start = trv + 1;
 
-	/* save first combination of random characters */
-	memcpy(carrybuf, start, suffp - start);
-
+	saved = 0;
 	oflags |= O_CREAT | O_EXCL | O_RDWR;
 	for (;;) {
 		if (doopen) {
@@ -170,6 +169,12 @@ _gettemp(int dfd, char *path, int *doopen, int domkdir, int slen, int oflags)
 		} else if (lstat(path, &sbuf))
 			return (errno == ENOENT);
 
+		/* save first combination of random characters */
+		if (!saved) {
+			memcpy(carrybuf, start, suffp - start);
+			saved = 1;
+		}
+
 		/* If we have a collision, cycle through the space of filenames */
 		for (trv = start, carryp = carrybuf;;) {
 			/* have we tried all possible permutations? */



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?202102011240.111CecuM094761>