BLI_fileops: change BLI_dir_create_recursive string size handling
Replace MAXPATHLEN with FILE_MAX, always allocate if the buffer isn't big enough since creating a truncated directory isn't useful. Having some code-paths only run in an ifdef complicated the code and made it more difficult to follow. Also assert the path doesn't contain ".." directories as they aren't supported.
This commit is contained in:
@@ -290,7 +290,11 @@ static bool dir_create_recursive(char *dirname, int len)
|
||||
BLI_assert(strlen(dirname) == len);
|
||||
BLI_assert(BLI_exists(dirname) == 0);
|
||||
/* Caller must ensure the path doesn't have trailing slashes. */
|
||||
BLI_assert(len && !BLI_path_slash_is_native_compat(dirname[len - 1]));
|
||||
BLI_assert_msg(len && !BLI_path_slash_is_native_compat(dirname[len - 1]),
|
||||
"Paths must not end with a slash!");
|
||||
BLI_assert_msg(!((len >= 3) && BLI_path_slash_is_native_compat(dirname[len - 3]) &&
|
||||
STREQ(dirname + (len - 2), "..")),
|
||||
"Paths containing \"..\" components must be normalized first!");
|
||||
|
||||
bool ret = true;
|
||||
char *dirname_parent_end = (char *)BLI_path_parent_dir_end(dirname, len);
|
||||
@@ -338,19 +342,17 @@ bool BLI_dir_create_recursive(const char *dirname)
|
||||
return S_ISDIR(mode) ? true : false;
|
||||
}
|
||||
|
||||
size_t len;
|
||||
char *dirname_mut;
|
||||
char dirname_static_buf[FILE_MAX];
|
||||
char *dirname_mut = dirname_static_buf;
|
||||
|
||||
#ifdef MAXPATHLEN
|
||||
char static_buf[MAXPATHLEN];
|
||||
len = BLI_strncpy_rlen(static_buf, dirname, sizeof(static_buf));
|
||||
dirname_mut = static_buf;
|
||||
#else
|
||||
len = strlen(dirname);
|
||||
dirname_mut = MEM_mallocN(len + 1, __func__);
|
||||
memcpy(dirname_mut, dirname, len + 1)
|
||||
#endif
|
||||
size_t len = strlen(dirname);
|
||||
if (len >= sizeof(dirname_static_buf)) {
|
||||
dirname_mut = MEM_mallocN(len + 1, __func__);
|
||||
}
|
||||
memcpy(dirname_mut, dirname, len + 1);
|
||||
|
||||
/* Strip trailing chars, important for first entering #dir_create_recursive
|
||||
* when then ensures this is the case for recursive calls. */
|
||||
while ((len > 0) && BLI_path_slash_is_native_compat(dirname_mut[len - 1])) {
|
||||
len--;
|
||||
}
|
||||
@@ -361,9 +363,9 @@ bool BLI_dir_create_recursive(const char *dirname)
|
||||
/* Ensure the string was properly restored. */
|
||||
BLI_assert(memcmp(dirname, dirname_mut, len) == 0);
|
||||
|
||||
#ifndef MAXPATHLEN
|
||||
MEM_freeN(dirname_mut);
|
||||
#endif
|
||||
if (dirname_mut != dirname_static_buf) {
|
||||
MEM_freeN(dirname_mut);
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user