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.
This commit is contained in:
Timothy Flynn 2024-04-03 21:51:34 -04:00 committed by Andreas Kling
parent 23b25333a5
commit 683c08744a
17 changed files with 48 additions and 26 deletions

View file

@ -370,7 +370,7 @@ static ErrorOr<void> parse_prop_list(Core::InputBufferedFile& file, PropList& pr
properties = { segments[1].trim_whitespace() };
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);
}
}
@ -490,11 +490,11 @@ static ErrorOr<void> parse_value_alias_list(Core::InputBufferedFile& file, Strin
VERIFY((segments.size() == 3) || (segments.size() == 4));
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();
append_alias(sanitize_alias ? sanitize_entry(alias).view() : alias, value);
append_alias(sanitize_alias ? sanitize_entry(alias) : ByteString { alias }, value);
if (segments.size() == 4) {
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);
}
}

View file

@ -42,8 +42,8 @@ ErrorOr<void> EventManager::save(FileSystemAccessClient::File& file)
set_filename(file.filename());
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();
m_dirty = false;

View file

@ -87,8 +87,10 @@ ErrorOr<void> BackgroundSettingsWidget::create_frame()
m_copy_action = GUI::CommonActions::make_copy_action(
[this](auto&) {
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);
m_context_menu->add_action(*m_copy_action);

View file

@ -171,7 +171,7 @@ void do_copy(Vector<ByteString> const& selected_file_paths, FileOperation file_o
auto url = URL::create_with_file_scheme(path);
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)

View file

@ -128,7 +128,8 @@ GUI::Variant SheetModel::data(const GUI::ModelIndex& index, GUI::ModelRole role)
else
builder.appendff(" while evaluating builtin '{}'\n", frame.function_name);
} 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();

View file

@ -27,8 +27,11 @@ unsigned TarFileHeader::expected_checksum() const
ErrorOr<void> TarFileHeader::calculate_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);
return {};
}

View file

@ -84,7 +84,8 @@ static void set_field(char (&field)[N], TSource&& source)
template<class TSource, size_t N>
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 {};
}

View file

@ -118,7 +118,8 @@ void FontDatabase::load_all_fonts_from_uri(StringView uri)
auto root = root_or_error.release_value();
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 (auto font_or_error = Gfx::BitmapFont::try_load_from_resource(resource); !font_or_error.is_error()) {
auto font = font_or_error.release_value();

View file

@ -313,8 +313,11 @@ void Job::on_socket_connected()
// responds with nothing (content-length = 0 with normal encoding); if that's the case,
// 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 (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),
// and manages to set a Content-Length anyway, in such cases ignore Content-Length and quit early;

View file

@ -101,7 +101,7 @@ ErrorOr<NonnullRefPtr<Node const>> Node::try_find_from_help_url(URL::URL const&
child_node_found = false;
auto children = TRY(current_node->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;
current_node = child;
break;

View file

@ -1209,10 +1209,13 @@ void TerminalWidget::drop_event(GUI::DropEvent& event)
if (!first)
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;
}

View file

@ -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()));
break;
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());
break;
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());
break;
case TRACK_TIMESTAMP_SCALE_ID:

View file

@ -1337,7 +1337,8 @@ WebIDL::ExceptionOr<JS::NonnullGCPtr<PendingResponse>> http_network_or_cache_fet
// 11. If httpRequests referrer is a URL, then:
if (http_request->referrer().has<URL::URL>()) {
// 1. Let referrerValue be httpRequests 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 httpRequests header list.
auto header = Infrastructure::Header {

View file

@ -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
// 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/*"
// Indicates that sound files are accepted.
if (value.equals_ignoring_ascii_case("audio/*"sv))

View file

@ -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:
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.
auto value = navigable.value()->active_window_proxy();

View file

@ -219,7 +219,7 @@ void WebSocket::send_client_handshake()
builder.append("\r\n"sv);
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);
}

View file

@ -148,7 +148,10 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
if (line_index >= wanted_line_count)
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;
}