Skip to content

Commit 3849bb6

Browse files
committed
Fix race condition between time of check and time of use
1 parent 3123a93 commit 3849bb6

File tree

1 file changed

+6
-9
lines changed

1 file changed

+6
-9
lines changed

src/scitokens_cache.cpp

+6-9
Original file line numberDiff line numberDiff line change
@@ -69,18 +69,15 @@ get_cache_file() {
6969
return "";
7070
}
7171

72-
struct stat cache_dir_stat;
73-
if (-1 == stat(cache_dir.c_str(), &cache_dir_stat)) {
74-
if (errno == ENOENT) {
75-
if (-1 == mkdir(cache_dir.c_str(), 0700)) return "";
76-
}
72+
int r = mkdir(cache_dir.c_str(), 0700);
73+
if ((r < 0) && errno != EEXIST) {
74+
return "";
7775
}
7876

7977
std::string keycache_dir = cache_dir + "/scitokens";
80-
if (-1 == stat(keycache_dir.c_str(), &cache_dir_stat)) {
81-
if (errno == ENOENT) {
82-
if (-1 == mkdir(keycache_dir.c_str(), 0700)) return "";
83-
}
78+
r = mkdir(keycache_dir.c_str(), 0700);
79+
if ((r < 0) && errno != EEXIST) {
80+
return "";
8481
}
8582

8683
std::string keycache_file = keycache_dir + "/scitokens_cpp.sqllite";

0 commit comments

Comments
 (0)