1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-28 10:27:36 +00:00

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.
This commit is contained in:
Max Wipfli 2021-06-29 16:46:16 +02:00 committed by Andreas Kling
parent 9b8f35259c
commit fc6d051dfd
43 changed files with 80 additions and 56 deletions

View file

@ -332,7 +332,7 @@ Result<void, File::CopyError> File::copy_file(const String& dst_path, const stru
if (errno != EISDIR)
return CopyError { OSError(errno), false };
auto dst_dir_path = String::formatted("{}/{}", dst_path, LexicalPath(source.filename()).basename());
auto dst_dir_path = String::formatted("{}/{}", dst_path, LexicalPath::basename(source.filename()));
dst_fd = creat(dst_dir_path.characters(), 0666);
if (dst_fd < 0)
return CopyError { OSError(errno), false };

View file

@ -139,7 +139,7 @@ String Backtrace::Entry::to_string(bool color) const
for (size_t i = 0; i < source_positions.size(); ++i) {
auto& position = source_positions[i];
auto fmt = color ? "\033[34;1m{}\033[0m:{}" : "{}:{}";
builder.appendff(fmt, LexicalPath(position.file_path).basename(), position.line_number);
builder.appendff(fmt, LexicalPath::basename(position.file_path), position.line_number);
if (i != source_positions.size() - 1) {
builder.append(" => ");
}

View file

@ -438,7 +438,7 @@ void DebugSession::update_loaded_libs()
String lib_name = object_path.value();
if (lib_name.ends_with(".so"))
lib_name = LexicalPath(object_path.value()).basename();
lib_name = LexicalPath::basename(object_path.value());
// FIXME: DebugInfo currently cannot parse the debug information of libgcc_s.so
if (lib_name == "libgcc_s.so")

View file

@ -77,7 +77,7 @@ Optional<DynamicObject::SymbolLookupResult> DynamicLinker::lookup_global_symbol(
static String get_library_name(String path)
{
return LexicalPath(move(path)).basename();
return LexicalPath::basename(move(path));
}
static Result<NonnullRefPtr<DynamicLoader>, DlErrorMessage> map_library(const String& filename, int fd)

View file

@ -222,7 +222,7 @@ Icon FileIconProvider::icon_for_path(const String& path, mode_t mode)
if (raw_symlink_target.starts_with('/')) {
target_path = raw_symlink_target;
} else {
target_path = Core::File::real_path_for(String::formatted("{}/{}", LexicalPath(path).dirname(), raw_symlink_target));
target_path = Core::File::real_path_for(String::formatted("{}/{}", LexicalPath::dirname(path), raw_symlink_target));
}
auto target_icon = icon_for_path(target_path);

View file

@ -653,7 +653,7 @@ void FileSystemModel::set_data(const ModelIndex& index, const Variant& data)
{
VERIFY(is_editable(index));
Node& node = const_cast<Node&>(this->node(index));
auto dirname = LexicalPath(node.full_path()).dirname();
auto dirname = LexicalPath::dirname(node.full_path());
auto new_full_path = String::formatted("{}/{}", dirname, data.to_string());
int rc = rename(node.full_path().characters(), new_full_path.characters());
if (rc < 0) {

View file

@ -56,7 +56,7 @@ int main(int argc, char** argv)
{
g_test_argc = argc;
g_test_argv = argv;
auto program_name = LexicalPath { argv[0] }.basename();
auto program_name = LexicalPath::basename(argv[0]);
g_program_name = program_name;
struct sigaction act;

View file

@ -1064,7 +1064,7 @@ void TerminalWidget::context_menu_event(GUI::ContextMenuEvent& event)
// Then add them to the context menu.
// FIXME: Adapt this code when we actually support calling LaunchServer with a specific handler in mind.
for (auto& handler : handlers) {
auto af = Desktop::AppFile::get_for_app(LexicalPath(handler).basename());
auto af = Desktop::AppFile::get_for_app(LexicalPath::basename(handler));
if (!af->is_valid())
continue;
auto action = GUI::Action::create(String::formatted("&Open in {}", af->name()), af->icon().bitmap_for_size(16), [this, handler](auto&) {
@ -1084,7 +1084,7 @@ void TerminalWidget::context_menu_event(GUI::ContextMenuEvent& event)
// file://courage/home/anon/something -> /home/anon/something
auto path = URL(m_context_menu_href).path();
// /home/anon/something -> something
auto name = LexicalPath(path).basename();
auto name = LexicalPath::basename(path);
GUI::Clipboard::the().set_plain_text(name);
}));
m_context_menu_for_hyperlink->add_separator();

View file

@ -81,7 +81,7 @@ static bool build_image_document(DOM::Document& document, const ByteBuffer& data
auto title_element = document.create_element("title");
head_element->append_child(title_element);
auto basename = LexicalPath(document.url().path()).basename();
auto basename = LexicalPath::basename(document.url().path());
auto title_text = adopt_ref(*new DOM::Text(document, String::formatted("{} [{}x{}]", basename, bitmap->width(), bitmap->height())));
title_element->append_child(title_text);