Fix race condition 'time of check time of use' - TOCTOU - when creating subdirectories.

git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/trunk@13891 626c5289-ae23-0410-ae9c-e8d60b6d4f22
This commit is contained in:
William S Fulton 2012-11-16 19:32:40 +00:00
commit e475ce1d6b

View file

@ -172,7 +172,6 @@ static int is_directory(String *directory) {
String *Swig_new_subdirectory(String *basedirectory, String *subdirectory) {
String *error = 0;
struct stat st;
int current_directory = basedirectory ? (Len(basedirectory) == 0 ? 1 : 0) : 0;
if (current_directory || is_directory(basedirectory)) {
@ -181,30 +180,23 @@ String *Swig_new_subdirectory(String *basedirectory, String *subdirectory) {
List *subdirs = Split(subdirectory, SWIG_FILE_DELIMITER[0], INT_MAX);
for (it = First(subdirs); it.item; it = Next(it)) {
int statdir;
int result;
String *subdirectory = it.item;
Printf(dir, "%s", subdirectory);
statdir = stat(Char(dir), &st);
if (statdir == 0) {
Printf(dir, SWIG_FILE_DELIMITER);
if (S_ISDIR(st.st_mode)) {
continue;
} else {
error = NewStringf("Cannot create directory %s", dir);
break;
}
} else {
#ifdef _WIN32
int result = _mkdir(Char(dir));
result = _mkdir(Char(dir));
#else
int result = mkdir(Char(dir), 0777);
result = mkdir(Char(dir), 0777);
#endif
Printf(dir, SWIG_FILE_DELIMITER);
if (result != 0 && errno != EEXIST) {
error = NewStringf("Cannot create directory %s", dir);
break;
}
if (result != 0 && errno != EEXIST) {
error = NewStringf("Cannot create directory %s: %s", dir, strerror(errno));
break;
}
if (!is_directory(dir)) {
error = NewStringf("Cannot create directory %s: it may already exist but not be a directory", dir);
break;
}
Printf(dir, SWIG_FILE_DELIMITER);
}
} else {
error = NewStringf("Cannot create subdirectory %s under the base directory %s. Either the base does not exist as a directory or it is not readable.", subdirectory, basedirectory);