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

Everywhere: Remove unintentional partial stream reads and writes

This commit is contained in:
Tim Schumacher 2023-03-01 17:24:50 +01:00 committed by Linus Groh
parent 26516ee160
commit ae51c1821c
44 changed files with 109 additions and 192 deletions

View file

@ -233,9 +233,7 @@ ErrorOr<MP3::MP3Frame, LoaderError> MP3LoaderPlugin::read_frame_data(MP3::Header
size_t old_reservoir_size = m_bit_reservoir.used_buffer_size();
LOADER_TRY(m_bitstream->read_until_filled(buffer));
// FIXME: This should write the entire span.
if (LOADER_TRY(m_bit_reservoir.write_some(buffer)) != header.slot_count)
return LoaderError { LoaderError::Category::IO, m_loaded_samples, "Could not write frame into bit reservoir." };
LOADER_TRY(m_bit_reservoir.write_until_depleted(buffer));
// If we don't have enough data in the reservoir to process this frame, skip it (but keep the data).
if (old_reservoir_size < static_cast<size_t>(frame.main_data_begin))

View file

@ -225,10 +225,8 @@ ErrorOr<Bytes> DeflateDecompressor::read_some(Bytes bytes)
if (block_type == 0b00) {
m_input_stream->align_to_byte_boundary();
// FIXME: This should read the entire span.
LittleEndian<u16> length, negated_length;
TRY(m_input_stream->read_some(length.bytes()));
TRY(m_input_stream->read_some(negated_length.bytes()));
u16 length = TRY(m_input_stream->read_value<LittleEndian<u16>>());
u16 negated_length = TRY(m_input_stream->read_value<LittleEndian<u16>>());
if ((length ^ 0xffff) != negated_length)
return Error::from_string_literal("Calculated negated length does not equal stored negated length");

View file

@ -75,10 +75,8 @@ ErrorOr<Bytes> GzipDecompressor::read_some(Bytes bytes)
current_member().m_nread += current_slice.size();
if (current_slice.size() < slice.size()) {
// FIXME: This should read the entire span.
LittleEndian<u32> crc32, input_size;
TRY(m_input_stream->read_some(crc32.bytes()));
TRY(m_input_stream->read_some(input_size.bytes()));
u32 crc32 = TRY(m_input_stream->read_value<LittleEndian<u32>>());
u32 input_size = TRY(m_input_stream->read_value<LittleEndian<u32>>());
if (crc32 != current_member().m_checksum.digest())
return Error::from_string_literal("Stored CRC32 does not match the calculated CRC32 of the current member");
@ -116,18 +114,16 @@ ErrorOr<Bytes> GzipDecompressor::read_some(Bytes bytes)
return Error::from_string_literal("Header is not supported by implementation");
if (header.flags & Flags::FEXTRA) {
// FIXME: This should read the entire span.
LittleEndian<u16> subfield_id, length;
TRY(m_input_stream->read_some(subfield_id.bytes()));
TRY(m_input_stream->read_some(length.bytes()));
u16 subfield_id = TRY(m_input_stream->read_value<LittleEndian<u16>>());
u16 length = TRY(m_input_stream->read_value<LittleEndian<u16>>());
TRY(m_input_stream->discard(length));
(void)subfield_id;
}
auto discard_string = [&]() -> ErrorOr<void> {
char next_char;
do {
// FIXME: This should read the entire span.
TRY(m_input_stream->read_some({ &next_char, sizeof(next_char) }));
next_char = TRY(m_input_stream->read_value<char>());
} while (next_char);
return {};
@ -140,10 +136,9 @@ ErrorOr<Bytes> GzipDecompressor::read_some(Bytes bytes)
TRY(discard_string());
if (header.flags & Flags::FHCRC) {
// FIXME: This should read the entire span.
LittleEndian<u16> crc16;
TRY(m_input_stream->read_some(crc16.bytes()));
u16 crc = TRY(m_input_stream->read_value<LittleEndian<u16>>());
// FIXME: we should probably verify this instead of just assuming it matches
(void)crc;
}
m_current_member = TRY(Member::construct(header, *m_input_stream));

View file

@ -113,8 +113,7 @@ ErrorOr<void> ZlibCompressor::write_header(ZlibCompressionMethod compression_met
// FIXME: Support pre-defined dictionaries.
// FIXME: This should write the entire span.
TRY(m_output_stream->write_some(header.as_u16.bytes()));
TRY(m_output_stream->write_until_depleted(header.as_u16.bytes()));
return {};
}
@ -155,8 +154,7 @@ ErrorOr<void> ZlibCompressor::finish()
TRY(static_cast<DeflateCompressor*>(m_compressor.ptr())->final_flush());
NetworkOrdered<u32> adler_sum = m_adler32_checksum.digest();
// FIXME: This should write the entire span.
TRY(m_output_stream->write_some(adler_sum.bytes()));
TRY(m_output_stream->write_value(adler_sum));
m_finished = true;

View file

@ -179,11 +179,10 @@ ErrorOr<void> ConfigFile::sync()
TRY(m_file->seek(0, SeekMode::SetPosition));
for (auto& it : m_groups) {
// FIXME: This should write the entire span.
TRY(m_file->write_some(DeprecatedString::formatted("[{}]\n", it.key).bytes()));
TRY(m_file->write_until_depleted(DeprecatedString::formatted("[{}]\n", it.key).bytes()));
for (auto& jt : it.value)
TRY(m_file->write_some(DeprecatedString::formatted("{}={}\n", jt.key, jt.value).bytes()));
TRY(m_file->write_some("\n"sv.bytes()));
TRY(m_file->write_until_depleted(DeprecatedString::formatted("{}={}\n", jt.key, jt.value).bytes()));
TRY(m_file->write_until_depleted("\n"sv.bytes()));
}
m_dirty = false;

View file

@ -221,9 +221,7 @@ public:
auto bytes_to_send = serialized.bytes();
u32 length = bytes_to_send.size();
// FIXME: Propagate errors
// FIXME: This should write the entire span.
auto sent = MUST(m_socket->write_some({ (u8 const*)&length, sizeof(length) }));
VERIFY(sent == sizeof(length));
MUST(m_socket->write_value(length));
while (!bytes_to_send.is_empty()) {
size_t bytes_sent = MUST(m_socket->write_some(bytes_to_send));
bytes_to_send = bytes_to_send.slice(bytes_sent);

View file

@ -1593,9 +1593,8 @@ ErrorOr<void> TextEditor::write_to_file(Core::File& file)
// A size 0 file doesn't need a data copy.
} else {
for (size_t i = 0; i < line_count(); ++i) {
// FIXME: This should write the entire span.
TRY(file.write_some(line(i).to_utf8().bytes()));
TRY(file.write_some("\n"sv.bytes()));
TRY(file.write_until_depleted(line(i).to_utf8().bytes()));
TRY(file.write_until_depleted("\n"sv.bytes()));
}
}
document().set_unmodified();

View file

@ -60,8 +60,7 @@ ErrorOr<void> Client::on_ready_to_receive()
auto pending_bytes = TRY(m_socket->pending_bytes());
auto receive_buffer = TRY(m_buffer.get_bytes_for_writing(pending_bytes));
// FIXME: This should read the entire span.
TRY(m_socket->read_some(receive_buffer));
TRY(m_socket->read_until_filled(receive_buffer));
// Once we get server hello we can start sending.
if (m_connect_pending) {
@ -146,9 +145,8 @@ static ReadonlyBytes command_byte_buffer(CommandType command)
ErrorOr<void> Client::send_raw(StringView data)
{
// FIXME: This should write the entire span.
TRY(m_socket->write_some(data.bytes()));
TRY(m_socket->write_some("\r\n"sv.bytes()));
TRY(m_socket->write_until_depleted(data.bytes()));
TRY(m_socket->write_until_depleted("\r\n"sv.bytes()));
return {};
}

View file

@ -680,8 +680,7 @@ ThrowCompletionOr<String> ConsoleClient::generically_format_values(MarkedVector<
bool first = true;
for (auto const& value : values) {
if (!first)
// FIXME: This should write the entire span.
TRY_OR_THROW_OOM(vm, stream.write_some(" "sv.bytes()));
TRY_OR_THROW_OOM(vm, stream.write_until_depleted(" "sv.bytes()));
TRY_OR_THROW_OOM(vm, JS::print(value, ctx));
first = false;
}

View file

@ -51,8 +51,7 @@ ErrorOr<void> XtermSuggestionDisplay::display(SuggestionManager const& manager)
// the suggestion list to fit in the prompt line.
auto start = max_line_count - m_prompt_lines_at_suggestion_initiation;
for (size_t i = start; i < max_line_count; ++i)
// FIXME: This should write the entire span.
TRY(stderr_stream->write_some("\n"sv.bytes()));
TRY(stderr_stream->write_until_depleted("\n"sv.bytes()));
lines_used += max_line_count;
longest_suggestion_length = 0;
}
@ -100,8 +99,7 @@ ErrorOr<void> XtermSuggestionDisplay::display(SuggestionManager const& manager)
if (next_column > m_num_columns) {
auto lines = (suggestion.text_view.length() + m_num_columns - 1) / m_num_columns;
lines_used += lines;
// FIXME: This should write the entire span.
TRY(stderr_stream->write_some("\n"sv.bytes()));
TRY(stderr_stream->write_until_depleted("\n"sv.bytes()));
num_printed = 0;
}
@ -117,13 +115,11 @@ ErrorOr<void> XtermSuggestionDisplay::display(SuggestionManager const& manager)
if (spans_entire_line) {
num_printed += m_num_columns;
// FIXME: This should write the entire span.
TRY(stderr_stream->write_some(suggestion.text_string.bytes()));
TRY(stderr_stream->write_some(suggestion.display_trivia_string.bytes()));
TRY(stderr_stream->write_until_depleted(suggestion.text_string.bytes()));
TRY(stderr_stream->write_until_depleted(suggestion.display_trivia_string.bytes()));
} else {
auto field = DeprecatedString::formatted("{: <{}} {}", suggestion.text_string, longest_suggestion_byte_length_without_trivia, suggestion.display_trivia_string);
// FIXME: This should write the entire span.
TRY(stderr_stream->write_some(DeprecatedString::formatted("{: <{}}", field, longest_suggestion_byte_length + 2).bytes()));
TRY(stderr_stream->write_until_depleted(DeprecatedString::formatted("{: <{}}", field, longest_suggestion_byte_length + 2).bytes()));
num_printed += longest_suggestion_length + 2;
}
@ -154,8 +150,7 @@ ErrorOr<void> XtermSuggestionDisplay::display(SuggestionManager const& manager)
TRY(VT::move_absolute(m_origin_row + lines_used, m_num_columns - string.length() - 1, *stderr_stream));
TRY(VT::apply_style({ Style::Background(Style::XtermColor::Green) }, *stderr_stream));
// FIXME: This should write the entire span.
TRY(stderr_stream->write_some(string.bytes()));
TRY(stderr_stream->write_until_depleted(string.bytes()));
TRY(VT::apply_style(Style::reset_style(), *stderr_stream));
}

View file

@ -67,8 +67,7 @@ static ErrorOr<void> launch_server(DeprecatedString const& socket_path, Deprecat
if (server_pid != 0) {
auto server_pid_file = TRY(Core::File::open(pid_path, Core::File::OpenMode::Write));
// FIXME: This should write the entire span.
TRY(server_pid_file->write_some(DeprecatedString::number(server_pid).bytes()));
TRY(server_pid_file->write_until_depleted(DeprecatedString::number(server_pid).bytes()));
TRY(Core::System::kill(getpid(), SIGTERM));
}

View file

@ -220,8 +220,7 @@ inline ByteBuffer load_entire_file(StringView path)
auto file = TRY(Core::File::open(path, Core::File::OpenMode::Read));
auto file_size = TRY(file->size());
auto content = TRY(ByteBuffer::create_uninitialized(file_size));
// FIXME: This should read the entire span.
TRY(file->read_some(content.bytes()));
TRY(file->read_until_filled(content.bytes()));
return content;
};

View file

@ -279,8 +279,7 @@ ErrorOr<void, Client::WrappedError> Client::send_success_response(JsonValue resu
builder.append("\r\n"sv);
auto builder_contents = TRY(builder.to_byte_buffer());
// FIXME: This should write the entire span.
TRY(m_socket->write_some(builder_contents));
TRY(m_socket->write_until_depleted(builder_contents));
while (!content.is_empty()) {
auto bytes_sent = TRY(m_socket->write_some(content.bytes()));
@ -320,9 +319,8 @@ ErrorOr<void, Client::WrappedError> Client::send_error_response(Error const& err
header_builder.appendff("Content-Length: {}\r\n", content_builder.length());
header_builder.append("\r\n"sv);
// FIXME: This should write the entire span.
TRY(m_socket->write_some(TRY(header_builder.to_byte_buffer())));
TRY(m_socket->write_some(TRY(content_builder.to_byte_buffer())));
TRY(m_socket->write_until_depleted(TRY(header_builder.to_byte_buffer())));
TRY(m_socket->write_until_depleted(TRY(content_builder.to_byte_buffer())));
log_response(error.http_status);
return {};