소스 검색

fix (deprecated) mktemp logic and update it to match other temp functions

the access function cannot be used to check for existence, because it
operates using real uid/gid rather than effective to determine
accessibility; this matters for the non-final path components.
instead, use stat. failure of stat is success if only the final
component is missing (ENOENT) and otherwise is failure.
Rich Felker 11 년 전
부모
커밋
38f44d6923
1개의 변경된 파일11개의 추가작업 그리고 4개의 파일을 삭제
  1. 11 4
      src/temp/mktemp.c

+ 11 - 4
src/temp/mktemp.c

@@ -2,23 +2,30 @@
 #include <fcntl.h>
 #include <unistd.h>
 #include <errno.h>
+#include <sys/stat.h>
 
 char *__randname(char *);
 
 char *mktemp(char *template)
 {
 	size_t l = strlen(template);
-	int retries = 10000;
+	int retries = 100;
+	struct stat st;
 
 	if (l < 6 || memcmp(template+l-6, "XXXXXX", 6)) {
 		errno = EINVAL;
 		*template = 0;
 		return template;
 	}
-	while (retries--) {
+
+	do {
 		__randname(template+l-6);
-		if (access(template, F_OK) < 0) return template;
-	}
+		if (stat(template, &st)) {
+			if (errno != ENOENT) *template = 0;
+			return template;
+		}
+	} while (--retries);
+
 	*template = 0;
 	errno = EEXIST;
 	return template;