1
0
mirror of https://git.zap.org.au/git/trader.git synced 2024-06-09 19:20:43 +00:00

Eliminate a second TOCTOU bug in xmkdir() itself

The theoretical odds of hitting this time-of-check / time-of-use bug are
extremely remote.  But we do want to keep Coverity Scan happy, don't we?
This commit is contained in:
John Zaitseff 2022-08-01 22:26:12 +10:00
parent 89fdb5a65e
commit e3d5d04774

View File

@ -1223,19 +1223,21 @@ int xmkdir (const char *pathname, mode_t mode)
return -1;
}
// Check that pathname already exists and is a directory
if (stat(pathname, &statbuf) == 0) {
if (S_ISDIR(statbuf.st_mode)) {
return 0;
} else {
errno = ENOTDIR;
return -1;
}
}
// Try creating the directory
ret = mkdir(pathname, mode);
if (ret == 0 || (errno != ENOENT && errno != ENOTDIR)) {
if (ret != 0 && errno == EEXIST) {
// Pathname exists: is it a directory?
if (stat(pathname, &statbuf) == 0) {
if (S_ISDIR(statbuf.st_mode)) {
return 0;
} else {
errno = ENOTDIR;
return -1;
}
} else {
return -1;
}
} else if (ret == 0 || (errno != ENOENT && errno != ENOTDIR)) {
return ret;
}