Fix file-name sanitizing for image "Save As"

ID names of images that included slashes would attempt to write
save the image in a sub-directory based on those slashes.

Also add notes to clarify the intended use of "safe" path functions.
This commit is contained in:
Campbell Barton 2025-01-14 13:42:57 +11:00
parent be0c9174aa
commit 8460dffcbd
2 changed files with 10 additions and 1 deletions

View file

@ -179,7 +179,7 @@ bool BKE_image_save_options_init(ImageSaveOptions *opts,
}
else {
BLI_path_join(opts->filepath, sizeof(opts->filepath), "//", ima->id.name + 2);
BLI_path_make_safe(opts->filepath);
BLI_path_make_safe_filename(opts->filepath + 2);
BLI_path_abs(opts->filepath,
is_prev_save ? G.filepath_last_image : BKE_main_blendfile_path(bmain));
}

View file

@ -98,6 +98,11 @@ const char *BLI_path_parent_dir_end(const char *path, size_t path_len)
* https://en.wikipedia.org/wiki/Filename#Reserved_characters_and_words )
* by underscores ('_').
*
* \note This should only be used when creating new paths from user-input or
* to sanitize a file-names initialized from non-path text such as the name of a data-block.
* It must never be used on references to existing paths since this function
* will change the paths causing them to become invalid.
*
* \note Space case ' ' is a bit of an edge case here - in theory it is allowed,
* but again can be an issue in some cases, so we simply replace it by an underscore too
* (good practice anyway).
@ -118,6 +123,10 @@ bool BLI_path_make_safe_filename(char *filename) ATTR_NONNULL(1);
* Make given path OS-safe.
*
* \return true if \a path was changed, false otherwise.
*
* \note When the file-name component of the path may not use safe characters,
* #BLI_path_make_safe_filename should be used on the file-name part,
* so slashes are replaced, treating the text as a single file-name.
*/
bool BLI_path_make_safe(char *path) ATTR_NONNULL(1);