From fc6d051dfdddc13835548537d025e760ba186b5b Mon Sep 17 00:00:00 2001 From: Max Wipfli Date: Tue, 29 Jun 2021 16:46:16 +0200 Subject: AK+Everywhere: Add and use static APIs for LexicalPath The LexicalPath instance methods dirname(), basename(), title() and extension() will be changed to return StringView const& in a further commit. Due to this, users creating temporary LexicalPath objects just to call one of those getters will recieve a StringView const& pointing to a possible freed buffer. To avoid this, static methods for those APIs have been added, which will return a String by value to avoid those problems. All cases where temporary LexicalPath objects have been used as described above haven been changed to use the static APIs. --- Userland/Utilities/basename.cpp | 2 +- Userland/Utilities/bt.cpp | 2 +- Userland/Utilities/cp.cpp | 2 +- Userland/Utilities/dirname.cpp | 2 +- Userland/Utilities/du.cpp | 2 +- Userland/Utilities/ln.cpp | 2 +- Userland/Utilities/mv.cpp | 2 +- Userland/Utilities/test.cpp | 2 +- Userland/Utilities/tree.cpp | 2 +- 9 files changed, 9 insertions(+), 9 deletions(-) (limited to 'Userland/Utilities') diff --git a/Userland/Utilities/basename.cpp b/Userland/Utilities/basename.cpp index ee54a200f8..4b6031715e 100644 --- a/Userland/Utilities/basename.cpp +++ b/Userland/Utilities/basename.cpp @@ -24,7 +24,7 @@ int main(int argc, char** argv) args_parser.add_positional_argument(suffix, "Suffix to strip from name", "suffix", Core::ArgsParser::Required::No); args_parser.parse(argc, argv); - auto result = LexicalPath(path).basename(); + auto result = LexicalPath::basename(path); if (!suffix.is_null() && result.length() != suffix.length() && result.ends_with(suffix)) result = result.substring(0, result.length() - suffix.length()); diff --git a/Userland/Utilities/bt.cpp b/Userland/Utilities/bt.cpp index 18d56bdc00..c291894dbc 100644 --- a/Userland/Utilities/bt.cpp +++ b/Userland/Utilities/bt.cpp @@ -65,7 +65,7 @@ int main(int argc, char** argv) out("\033]8;;{}\033\\", url.serialize()); } - out("\033[34;1m{}:{}\033[0m", LexicalPath(source_position.file_path).basename(), source_position.line_number); + out("\033[34;1m{}:{}\033[0m", LexicalPath::basename(source_position.file_path), source_position.line_number); if (linked) out("\033]8;;\033\\"); diff --git a/Userland/Utilities/cp.cpp b/Userland/Utilities/cp.cpp index 86a08e6877..3f6803e674 100644 --- a/Userland/Utilities/cp.cpp +++ b/Userland/Utilities/cp.cpp @@ -36,7 +36,7 @@ int main(int argc, char** argv) for (auto& source : sources) { auto destination_path = destination_is_existing_dir - ? String::formatted("{}/{}", destination, LexicalPath(source).basename()) + ? String::formatted("{}/{}", destination, LexicalPath::basename(source)) : destination; auto result = Core::File::copy_file_or_directory( diff --git a/Userland/Utilities/dirname.cpp b/Userland/Utilities/dirname.cpp index 05edc7d551..313c532a5a 100644 --- a/Userland/Utilities/dirname.cpp +++ b/Userland/Utilities/dirname.cpp @@ -14,6 +14,6 @@ int main(int argc, char** argv) args_parser.add_positional_argument(path, "Path", "path"); args_parser.parse(argc, argv); - outln("{}", LexicalPath(path).dirname()); + outln("{}", LexicalPath::dirname(path)); return 0; } diff --git a/Userland/Utilities/du.cpp b/Userland/Utilities/du.cpp index 0034ef9714..eb007193e0 100644 --- a/Userland/Utilities/du.cpp +++ b/Userland/Utilities/du.cpp @@ -151,7 +151,7 @@ int print_space_usage(const String& path, const DuOption& du_option, int max_dep } } - const auto basename = LexicalPath(path).basename(); + const auto basename = LexicalPath::basename(path); for (const auto& pattern : du_option.excluded_patterns) { if (basename.matches(pattern, CaseSensitivity::CaseSensitive)) return 0; diff --git a/Userland/Utilities/ln.cpp b/Userland/Utilities/ln.cpp index 7525e042c9..ece88fb947 100644 --- a/Userland/Utilities/ln.cpp +++ b/Userland/Utilities/ln.cpp @@ -30,7 +30,7 @@ int main(int argc, char** argv) String path_buffer; if (!path) { - path_buffer = LexicalPath(target).basename(); + path_buffer = LexicalPath::basename(target); path = path_buffer.characters(); } diff --git a/Userland/Utilities/mv.cpp b/Userland/Utilities/mv.cpp index 6962a6a3e7..1e917f69b7 100644 --- a/Userland/Utilities/mv.cpp +++ b/Userland/Utilities/mv.cpp @@ -59,7 +59,7 @@ int main(int argc, char** argv) String combined_new_path; const char* new_path = original_new_path; if (S_ISDIR(st.st_mode)) { - auto old_basename = LexicalPath(old_path).basename(); + auto old_basename = LexicalPath::basename(old_path); combined_new_path = String::formatted("{}/{}", original_new_path, old_basename); new_path = combined_new_path.characters(); } diff --git a/Userland/Utilities/test.cpp b/Userland/Utilities/test.cpp index aea9d67c7b..a868348411 100644 --- a/Userland/Utilities/test.cpp +++ b/Userland/Utilities/test.cpp @@ -498,7 +498,7 @@ int main(int argc, char* argv[]) return 126; } - if (LexicalPath { argv[0] }.basename() == "[") { + if (LexicalPath::basename(argv[0]) == "[") { --argc; if (StringView { argv[argc] } != "]") fatal_error("test invoked as '[' requires a closing bracket ']'"); diff --git a/Userland/Utilities/tree.cpp b/Userland/Utilities/tree.cpp index a6a3f2c78b..e31522b2a2 100644 --- a/Userland/Utilities/tree.cpp +++ b/Userland/Utilities/tree.cpp @@ -36,7 +36,7 @@ static void print_directory_tree(const String& root_path, int depth, const Strin out("{}|-- ", root_indent_string); } - String root_dir_name = LexicalPath(root_path).basename(); + String root_dir_name = LexicalPath::basename(root_path); out("\033[34;1m{}\033[0m\n", root_dir_name); if (depth >= max_depth) { -- cgit v1.2.3