Browse Source

Userland: Avoid some conversions from rvalue strings to StringView

These are all actually fine, there is no UAF here. But once e.g.
`ByteString::view() &&` is deleted, these instances won't compile.
Timothy Flynn 1 year ago
parent
commit
683c08744a

+ 3 - 3
Meta/Lagom/Tools/CodeGenerators/LibUnicode/GenerateUnicodeData.cpp

@@ -370,7 +370,7 @@ static ErrorOr<void> parse_prop_list(Core::InputBufferedFile& file, PropList& pr
             properties = { segments[1].trim_whitespace() };
             properties = { segments[1].trim_whitespace() };
 
 
         for (auto& property : properties) {
         for (auto& property : properties) {
-            auto& code_points = prop_list.ensure(sanitize_property ? sanitize_entry(property).trim_whitespace().view() : property.trim_whitespace());
+            auto& code_points = prop_list.ensure(sanitize_property ? sanitize_entry(property).trim_whitespace() : ByteString { property.trim_whitespace() });
             code_points.append(code_point_range);
             code_points.append(code_point_range);
         }
         }
     }
     }
@@ -490,11 +490,11 @@ static ErrorOr<void> parse_value_alias_list(Core::InputBufferedFile& file, Strin
         VERIFY((segments.size() == 3) || (segments.size() == 4));
         VERIFY((segments.size() == 3) || (segments.size() == 4));
         auto value = primary_value_is_first ? segments[1].trim_whitespace() : segments[2].trim_whitespace();
         auto value = primary_value_is_first ? segments[1].trim_whitespace() : segments[2].trim_whitespace();
         auto alias = primary_value_is_first ? segments[2].trim_whitespace() : segments[1].trim_whitespace();
         auto alias = primary_value_is_first ? segments[2].trim_whitespace() : segments[1].trim_whitespace();
-        append_alias(sanitize_alias ? sanitize_entry(alias).view() : alias, value);
+        append_alias(sanitize_alias ? sanitize_entry(alias) : ByteString { alias }, value);
 
 
         if (segments.size() == 4) {
         if (segments.size() == 4) {
             alias = segments[3].trim_whitespace();
             alias = segments[3].trim_whitespace();
-            append_alias(sanitize_alias ? sanitize_entry(alias).view() : alias, value);
+            append_alias(sanitize_alias ? sanitize_entry(alias) : ByteString { alias }, value);
         }
         }
     }
     }
 
 

+ 2 - 2
Userland/Applications/Calendar/EventManager.cpp

@@ -42,8 +42,8 @@ ErrorOr<void> EventManager::save(FileSystemAccessClient::File& file)
     set_filename(file.filename());
     set_filename(file.filename());
 
 
     auto stream = file.release_stream();
     auto stream = file.release_stream();
-    auto json = TRY(serialize_events());
-    TRY(stream->write_some(json.to_byte_string().bytes()));
+    auto json = TRY(serialize_events()).to_byte_string();
+    TRY(stream->write_some(json.bytes()));
     stream->close();
     stream->close();
 
 
     m_dirty = false;
     m_dirty = false;

+ 4 - 2
Userland/Applications/DisplaySettings/BackgroundSettingsWidget.cpp

@@ -87,8 +87,10 @@ ErrorOr<void> BackgroundSettingsWidget::create_frame()
     m_copy_action = GUI::CommonActions::make_copy_action(
     m_copy_action = GUI::CommonActions::make_copy_action(
         [this](auto&) {
         [this](auto&) {
             auto wallpaper = m_monitor_widget->wallpaper();
             auto wallpaper = m_monitor_widget->wallpaper();
-            if (wallpaper.has_value())
-                GUI::Clipboard::the().set_data(URL::create_with_file_scheme(wallpaper.value().to_byte_string()).to_byte_string().bytes(), "text/uri-list");
+            if (wallpaper.has_value()) {
+                auto url = URL::create_with_file_scheme(wallpaper.value()).to_byte_string();
+                GUI::Clipboard::the().set_data(url.bytes(), "text/uri-list");
+            }
         },
         },
         this);
         this);
     m_context_menu->add_action(*m_copy_action);
     m_context_menu->add_action(*m_copy_action);

+ 1 - 1
Userland/Applications/FileManager/main.cpp

@@ -171,7 +171,7 @@ void do_copy(Vector<ByteString> const& selected_file_paths, FileOperation file_o
         auto url = URL::create_with_file_scheme(path);
         auto url = URL::create_with_file_scheme(path);
         copy_text.appendff("{}\n", url);
         copy_text.appendff("{}\n", url);
     }
     }
-    GUI::Clipboard::the().set_data(copy_text.to_byte_string().bytes(), "text/uri-list");
+    GUI::Clipboard::the().set_data(copy_text.string_view().bytes(), "text/uri-list");
 }
 }
 
 
 void do_paste(ByteString const& target_directory, GUI::Window* window)
 void do_paste(ByteString const& target_directory, GUI::Window* window)

+ 2 - 1
Userland/Applications/Spreadsheet/SpreadsheetModel.cpp

@@ -128,7 +128,8 @@ GUI::Variant SheetModel::data(const GUI::ModelIndex& index, GUI::ModelRole role)
                 else
                 else
                     builder.appendff("  while evaluating builtin '{}'\n", frame.function_name);
                     builder.appendff("  while evaluating builtin '{}'\n", frame.function_name);
             } else if (frame.source_range().filename().starts_with("cell "sv)) {
             } else if (frame.source_range().filename().starts_with("cell "sv)) {
-                builder.appendff("  in cell '{}', at line {}, column {}\n", frame.source_range().filename().substring_view(5), frame.source_range().start.line, frame.source_range().start.column);
+                auto filename = frame.source_range().filename();
+                builder.appendff("  in cell '{}', at line {}, column {}\n", filename.substring_view(5), frame.source_range().start.line, frame.source_range().start.column);
             }
             }
         }
         }
         return builder.to_byte_string();
         return builder.to_byte_string();

+ 4 - 1
Userland/Libraries/LibArchive/Tar.cpp

@@ -27,8 +27,11 @@ unsigned TarFileHeader::expected_checksum() const
 ErrorOr<void> TarFileHeader::calculate_checksum()
 ErrorOr<void> TarFileHeader::calculate_checksum()
 {
 {
     memset(m_checksum, ' ', sizeof(m_checksum));
     memset(m_checksum, ' ', sizeof(m_checksum));
-    bool copy_successful = TRY(String::formatted("{:06o}", expected_checksum())).bytes_as_string_view().copy_characters_to_buffer(m_checksum, sizeof(m_checksum));
+
+    auto octal = TRY(String::formatted("{:06o}", expected_checksum()));
+    bool copy_successful = octal.bytes_as_string_view().copy_characters_to_buffer(m_checksum, sizeof(m_checksum));
     VERIFY(copy_successful);
     VERIFY(copy_successful);
+
     return {};
     return {};
 }
 }
 
 

+ 2 - 1
Userland/Libraries/LibArchive/Tar.h

@@ -84,7 +84,8 @@ static void set_field(char (&field)[N], TSource&& source)
 template<class TSource, size_t N>
 template<class TSource, size_t N>
 static ErrorOr<void> set_octal_field(char (&field)[N], TSource&& source)
 static ErrorOr<void> set_octal_field(char (&field)[N], TSource&& source)
 {
 {
-    set_field(field, TRY(String::formatted("{:o}", forward<TSource>(source))).bytes_as_string_view());
+    auto octal = TRY(String::formatted("{:o}", forward<TSource>(source)));
+    set_field(field, octal.bytes_as_string_view());
     return {};
     return {};
 }
 }
 
 

+ 2 - 1
Userland/Libraries/LibGfx/Font/FontDatabase.cpp

@@ -118,7 +118,8 @@ void FontDatabase::load_all_fonts_from_uri(StringView uri)
     auto root = root_or_error.release_value();
     auto root = root_or_error.release_value();
 
 
     root->for_each_descendant_file([this](Core::Resource const& resource) -> IterationDecision {
     root->for_each_descendant_file([this](Core::Resource const& resource) -> IterationDecision {
-        auto path = LexicalPath(resource.uri().bytes_as_string_view());
+        auto uri = resource.uri();
+        auto path = LexicalPath(uri.bytes_as_string_view());
         if (path.has_extension(".font"sv)) {
         if (path.has_extension(".font"sv)) {
             if (auto font_or_error = Gfx::BitmapFont::try_load_from_resource(resource); !font_or_error.is_error()) {
             if (auto font_or_error = Gfx::BitmapFont::try_load_from_resource(resource); !font_or_error.is_error()) {
                 auto font = font_or_error.release_value();
                 auto font = font_or_error.release_value();

+ 5 - 2
Userland/Libraries/LibHTTP/Job.cpp

@@ -313,8 +313,11 @@ void Job::on_socket_connected()
                 // responds with nothing (content-length = 0 with normal encoding); if that's the case,
                 // responds with nothing (content-length = 0 with normal encoding); if that's the case,
                 // quit early as we won't be reading anything anyway.
                 // quit early as we won't be reading anything anyway.
                 if (auto result = m_headers.get("Content-Length"sv).value_or(""sv).to_number<unsigned>(); result.has_value()) {
                 if (auto result = m_headers.get("Content-Length"sv).value_or(""sv).to_number<unsigned>(); result.has_value()) {
-                    if (result.value() == 0 && !m_headers.get("Transfer-Encoding"sv).value_or(""sv).view().trim_whitespace().equals_ignoring_ascii_case("chunked"sv))
-                        return finish_up();
+                    if (result.value() == 0) {
+                        auto transfer_encoding = m_headers.get("Transfer-Encoding"sv);
+                        if (!transfer_encoding.has_value() || !transfer_encoding->view().trim_whitespace().equals_ignoring_ascii_case("chunked"sv))
+                            return finish_up();
+                    }
                 }
                 }
                 // There's also the possibility that the server responds with 204 (No Content),
                 // There's also the possibility that the server responds with 204 (No Content),
                 // and manages to set a Content-Length anyway, in such cases ignore Content-Length and quit early;
                 // and manages to set a Content-Length anyway, in such cases ignore Content-Length and quit early;

+ 1 - 1
Userland/Libraries/LibManual/Node.cpp

@@ -101,7 +101,7 @@ ErrorOr<NonnullRefPtr<Node const>> Node::try_find_from_help_url(URL::URL const&
         child_node_found = false;
         child_node_found = false;
         auto children = TRY(current_node->children());
         auto children = TRY(current_node->children());
         for (auto const& child : children) {
         for (auto const& child : children) {
-            if (TRY(child->name()) == url.path_segment_at_index(i).view()) {
+            if (auto path = url.path_segment_at_index(i); TRY(child->name()) == path.view()) {
                 child_node_found = true;
                 child_node_found = true;
                 current_node = child;
                 current_node = child;
                 break;
                 break;

+ 7 - 4
Userland/Libraries/LibVT/TerminalWidget.cpp

@@ -1209,10 +1209,13 @@ void TerminalWidget::drop_event(GUI::DropEvent& event)
             if (!first)
             if (!first)
                 send_non_user_input(" "sv.bytes());
                 send_non_user_input(" "sv.bytes());
 
 
-            if (url.scheme() == "file")
-                send_non_user_input(url.serialize_path().bytes());
-            else
-                send_non_user_input(url.to_byte_string().bytes());
+            if (url.scheme() == "file") {
+                auto path = url.serialize_path();
+                send_non_user_input(path.bytes());
+            } else {
+                auto url_string = url.to_byte_string();
+                send_non_user_input(url_string.bytes());
+            }
 
 
             first = false;
             first = false;
         }
         }

+ 2 - 2
Userland/Libraries/LibVideo/Containers/Matroska/Reader.cpp

@@ -468,11 +468,11 @@ static DecoderErrorOr<TrackEntry> parse_track_entry(Streamer& streamer)
             dbgln_if(MATROSKA_TRACE_DEBUG, "Read TrackType attribute: {}", to_underlying(track_entry.track_type()));
             dbgln_if(MATROSKA_TRACE_DEBUG, "Read TrackType attribute: {}", to_underlying(track_entry.track_type()));
             break;
             break;
         case TRACK_LANGUAGE_ID:
         case TRACK_LANGUAGE_ID:
-            track_entry.set_language(DECODER_TRY_ALLOC(FlyString::from_utf8(TRY_READ(streamer.read_string()).view())));
+            track_entry.set_language(DECODER_TRY_ALLOC(String::from_byte_string(TRY_READ(streamer.read_string()))));
             dbgln_if(MATROSKA_TRACE_DEBUG, "Read Track's Language attribute: {}", track_entry.language());
             dbgln_if(MATROSKA_TRACE_DEBUG, "Read Track's Language attribute: {}", track_entry.language());
             break;
             break;
         case TRACK_CODEC_ID:
         case TRACK_CODEC_ID:
-            track_entry.set_codec_id(DECODER_TRY_ALLOC(FlyString::from_utf8(TRY_READ(streamer.read_string()).view())));
+            track_entry.set_codec_id(DECODER_TRY_ALLOC(String::from_byte_string(TRY_READ(streamer.read_string()))));
             dbgln_if(MATROSKA_TRACE_DEBUG, "Read Track's CodecID attribute: {}", track_entry.codec_id());
             dbgln_if(MATROSKA_TRACE_DEBUG, "Read Track's CodecID attribute: {}", track_entry.codec_id());
             break;
             break;
         case TRACK_TIMESTAMP_SCALE_ID:
         case TRACK_TIMESTAMP_SCALE_ID:

+ 2 - 1
Userland/Libraries/LibWeb/Fetch/Fetching/Fetching.cpp

@@ -1337,7 +1337,8 @@ WebIDL::ExceptionOr<JS::NonnullGCPtr<PendingResponse>> http_network_or_cache_fet
         // 11. If httpRequest’s referrer is a URL, then:
         // 11. If httpRequest’s referrer is a URL, then:
         if (http_request->referrer().has<URL::URL>()) {
         if (http_request->referrer().has<URL::URL>()) {
             // 1. Let referrerValue be httpRequest’s referrer, serialized and isomorphic encoded.
             // 1. Let referrerValue be httpRequest’s referrer, serialized and isomorphic encoded.
-            auto referrer_value = TRY_OR_THROW_OOM(vm, ByteBuffer::copy(http_request->referrer().get<URL::URL>().serialize().bytes()));
+            auto referrer_string = http_request->referrer().get<URL::URL>().serialize();
+            auto referrer_value = TRY_OR_THROW_OOM(vm, ByteBuffer::copy(referrer_string.bytes()));
 
 
             // 2. Append (`Referer`, referrerValue) to httpRequest’s header list.
             // 2. Append (`Referer`, referrerValue) to httpRequest’s header list.
             auto header = Infrastructure::Header {
             auto header = Infrastructure::Header {

+ 3 - 1
Userland/Libraries/LibWeb/HTML/HTMLInputElement.cpp

@@ -189,7 +189,9 @@ FileFilter HTMLInputElement::parse_accept_attribute() const
 
 
     // If specified, the attribute must consist of a set of comma-separated tokens, each of which must be an ASCII
     // If specified, the attribute must consist of a set of comma-separated tokens, each of which must be an ASCII
     // case-insensitive match for one of the following:
     // case-insensitive match for one of the following:
-    get_attribute_value(HTML::AttributeNames::accept).bytes_as_string_view().for_each_split_view(',', SplitBehavior::Nothing, [&](StringView value) {
+    auto accept = get_attribute_value(HTML::AttributeNames::accept);
+
+    accept.bytes_as_string_view().for_each_split_view(',', SplitBehavior::Nothing, [&](StringView value) {
         // The string "audio/*"
         // The string "audio/*"
         //     Indicates that sound files are accepted.
         //     Indicates that sound files are accepted.
         if (value.equals_ignoring_ascii_case("audio/*"sv))
         if (value.equals_ignoring_ascii_case("audio/*"sv))

+ 3 - 1
Userland/Libraries/LibWeb/HTML/WindowProxy.cpp

@@ -117,7 +117,9 @@ JS::ThrowCompletionOr<Optional<JS::PropertyDescriptor>> WindowProxy::internal_ge
 
 
     // 6. If property is undefined and P is in W's document-tree child navigable target name property set, then:
     // 6. If property is undefined and P is in W's document-tree child navigable target name property set, then:
     auto navigable_property_set = m_window->document_tree_child_navigable_target_name_property_set();
     auto navigable_property_set = m_window->document_tree_child_navigable_target_name_property_set();
-    if (auto navigable = navigable_property_set.get(property_key.to_string().view()); navigable.has_value()) {
+    auto property_key_string = property_key.to_string();
+
+    if (auto navigable = navigable_property_set.get(property_key_string.view()); navigable.has_value()) {
         // 1. Let value be the active WindowProxy of the named object of W with the name P.
         // 1. Let value be the active WindowProxy of the named object of W with the name P.
         auto value = navigable.value()->active_window_proxy();
         auto value = navigable.value()->active_window_proxy();
 
 

+ 1 - 1
Userland/Libraries/LibWebSocket/WebSocket.cpp

@@ -219,7 +219,7 @@ void WebSocket::send_client_handshake()
     builder.append("\r\n"sv);
     builder.append("\r\n"sv);
 
 
     m_state = WebSocket::InternalState::WaitingForServerHandshake;
     m_state = WebSocket::InternalState::WaitingForServerHandshake;
-    auto success = m_impl->send(builder.to_byte_string().bytes());
+    auto success = m_impl->send(builder.string_view().bytes());
     VERIFY(success);
     VERIFY(success);
 }
 }
 
 

+ 4 - 1
Userland/Utilities/tail.cpp

@@ -148,7 +148,10 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
                     if (line_index >= wanted_line_count)
                     if (line_index >= wanted_line_count)
                         line.append(ch);
                         line.append(ch);
                 }
                 }
-                out("{}", line.to_byte_string().substring_view(1, line.length() - 1));
+
+                auto line_string = line.to_byte_string();
+                out("{}", line_string.substring_view(1, line.length() - 1));
+
                 continue;
                 continue;
             }
             }