BLI_path: disallow passing NULL arguments to BLI_path_split_dir_file

Instead BLI_path_split_dir_part & BLI_path_split_file_part can be used.
This commit is contained in:
Campbell Barton
2023-05-02 21:09:40 +10:00
parent 1ab72e8459
commit 27e4ab80fb
4 changed files with 35 additions and 21 deletions

View File

@@ -49,10 +49,12 @@ bool BLI_make_existing_file(const char *name);
* - Won't change \a string.
* - Won't create any directories.
* - Doesn't use CWD, or deal with relative paths.
* - Only fill's in \a dir and \a file when they are non NULL.
*/
void BLI_path_split_dir_file(
const char *string, char *dir, size_t dirlen, char *file, size_t filelen);
void BLI_path_split_dir_file(const char *string,
char *dir,
size_t dirlen,
char *file,
size_t filelen) ATTR_NONNULL(1, 2, 4);
/**
* Copies the parent directory part of string into `dir`, max length `dirlen`.
*/

View File

@@ -246,7 +246,7 @@ bool BLI_file_is_writable(const char *filepath)
else {
/* file doesn't exist -- check I can create it in parent directory */
char parent[FILE_MAX];
BLI_path_split_dir_file(filepath, parent, sizeof(parent), NULL, 0);
BLI_path_split_dir_part(filepath, parent, sizeof(parent));
#ifdef WIN32
/* windows does not have X_OK */
writable = BLI_access(parent, W_OK) == 0;

View File

@@ -1479,6 +1479,13 @@ bool BLI_path_filename_ensure(char *filepath, size_t maxlen, const char *filenam
return false;
}
static size_t path_split_dir_file_offset(const char *string)
{
const char *lslash_str = BLI_path_slash_rfind(string);
const size_t lslash = lslash_str ? (size_t)(lslash_str - string) + 1 : 0;
return lslash;
}
void BLI_path_split_dir_file(
const char *string, char *dir, const size_t dirlen, char *file, const size_t filelen)
{
@@ -1486,32 +1493,37 @@ void BLI_path_split_dir_file(
memset(dir, 0xff, sizeof(*dir) * dirlen);
memset(file, 0xff, sizeof(*file) * filelen);
#endif
const char *lslash_str = BLI_path_slash_rfind(string);
const size_t lslash = lslash_str ? (size_t)(lslash_str - string) + 1 : 0;
if (dir) {
if (lslash) {
/* +1 to include the slash and the last char. */
BLI_strncpy(dir, string, MIN2(dirlen, lslash + 1));
}
else {
dir[0] = '\0';
}
const size_t lslash = path_split_dir_file_offset(string);
if (lslash) { /* +1 to include the slash and the last char. */
BLI_strncpy(dir, string, MIN2(dirlen, lslash + 1));
}
if (file) {
BLI_strncpy(file, string + lslash, filelen);
else {
dir[0] = '\0';
}
BLI_strncpy(file, string + lslash, filelen);
}
void BLI_path_split_dir_part(const char *string, char *dir, const size_t dirlen)
{
BLI_path_split_dir_file(string, dir, dirlen, NULL, 0);
#ifdef DEBUG_STRSIZE
memset(dir, 0xff, sizeof(*dir) * dirlen);
#endif
const size_t lslash = path_split_dir_file_offset(string);
if (lslash) { /* +1 to include the slash and the last char. */
BLI_strncpy(dir, string, MIN2(dirlen, lslash + 1));
}
else {
dir[0] = '\0';
}
}
void BLI_path_split_file_part(const char *string, char *file, const size_t filelen)
{
BLI_path_split_dir_file(string, NULL, 0, file, filelen);
#ifdef DEBUG_STRSIZE
memset(file, 0xff, sizeof(*file) * filelen);
#endif
const size_t lslash = path_split_dir_file_offset(string);
BLI_strncpy(file, string + lslash, filelen);
}
const char *BLI_path_extension_or_end(const char *filepath)

View File

@@ -176,7 +176,7 @@ static void seq_disk_cache_get_files(SeqDiskCache *disk_cache, char *path)
}
char file[FILE_MAX];
BLI_path_split_dir_file(fl->path, NULL, 0, file, sizeof(file));
BLI_path_split_file_part(fl->path, file, sizeof(file));
bool is_dir = BLI_is_dir(fl->path);
if (is_dir && !FILENAME_IS_CURRPAR(file)) {