aboutsummaryrefslogtreecommitdiff
path: root/lib/libc
diff options
context:
space:
mode:
authorMateusz Guzik <mjg@FreeBSD.org>2021-01-24 04:34:22 +0000
committerMateusz Guzik <mjg@FreeBSD.org>2021-01-24 11:06:36 +0000
commitb22fdf45ff8ef1d1f9a6c28f1d7f59ca4b012da6 (patch)
treeef9e5eccab4c6ba7b9fb595447537d336249d6e8 /lib/libc
parent6fe328ace8f418bab0ab7f78c6fa1f2e757dd8a9 (diff)
downloadsrc-b22fdf45ff8ef1d1f9a6c28f1d7f59ca4b012da6.tar.gz
src-b22fdf45ff8ef1d1f9a6c28f1d7f59ca4b012da6.zip
libc: try to skip memcpy in _gettemp
Diffstat (limited to 'lib/libc')
-rw-r--r--lib/libc/stdio/mktemp.c11
1 files 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? */