mirror of
https://github.com/zeek/zeek.git
synced 2025-10-02 06:38:20 +00:00
Expand build_path() function to handle empty dir arguments gracefully
When passing an empty string as a directory, the function would produce filenames starting with a slash even when the given file_name is not an absolute path. Defaulting to the root directory is likely never intended and might conveivably be dangerous. The middle "/" is now skipped also if dir is an empty string.
This commit is contained in:
parent
4d1b9f4a08
commit
9d59a48ae2
3 changed files with 14 additions and 7 deletions
|
@ -28,11 +28,13 @@ function extract_path(input: string): string
|
|||
## file_name: the name of the file.
|
||||
##
|
||||
## Returns: the concatenation of the directory path and file name, or just
|
||||
## the file name if it's already an absolute path.
|
||||
## the file name if it's already an absolute path or dir is empty.
|
||||
function build_path(dir: string, file_name: string): string
|
||||
{
|
||||
return (file_name == absolute_path_pat) ?
|
||||
file_name : cat(dir, "/", file_name);
|
||||
# Avoid introducing "//" into the result:
|
||||
local sep = ends_with(dir, "/") ? "" : "/";
|
||||
return (file_name == absolute_path_pat || dir == "") ?
|
||||
file_name : cat(dir, sep, file_name);
|
||||
}
|
||||
|
||||
## Returns a compressed path to a file given a directory and file name.
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue