From 53d015082749b9bf43a2160d5553bc0e9e1086ef Mon Sep 17 00:00:00 2001 From: Gunnar Beutner Date: Sun, 16 May 2021 08:47:46 +0200 Subject: [PATCH] AK+Userland: Remove nullability feature for the ByteBuffer type Nobody seems to use this particular feature, in fact there were some bugs which were uncovered by removing operator bool. --- AK/ByteBuffer.h | 10 ---------- AK/String.h | 2 -- AK/Vector.h | 2 -- Userland/Applications/HexEditor/HexEditor.cpp | 5 ----- Userland/Applications/HexEditor/HexEditorWidget.cpp | 2 +- Userland/Libraries/LibCore/IODevice.cpp | 4 ---- Userland/Libraries/LibGemini/Job.cpp | 2 +- Userland/Libraries/LibHTTP/Job.cpp | 2 +- Userland/Libraries/LibTLS/ClientHandshake.cpp | 4 +--- .../LibWeb/CSS/Parser/DeprecatedCSSParser.cpp | 2 +- Userland/Libraries/LibWeb/Loader/Resource.h | 2 +- Userland/Libraries/LibWeb/XHR/XMLHttpRequest.cpp | 2 +- Userland/Services/FileOperation/main.cpp | 2 +- Userland/Services/LookupServer/MulticastDNS.cpp | 2 +- Userland/Services/WebServer/Client.cpp | 2 +- Userland/Utilities/du.cpp | 3 ++- 16 files changed, 12 insertions(+), 36 deletions(-) diff --git a/AK/ByteBuffer.h b/AK/ByteBuffer.h index 3b188695b37..f3b299e586e 100644 --- a/AK/ByteBuffer.h +++ b/AK/ByteBuffer.h @@ -28,7 +28,6 @@ public: { grow(other.size()); VERIFY(m_size == other.size()); - VERIFY(!m_is_null); __builtin_memcpy(data(), other.data(), other.size()); } @@ -96,10 +95,6 @@ public: bool operator!=(ByteBuffer const& other) const { return !(*this == other); } - operator bool() const { return !is_null(); } - bool operator!() const { return is_null(); } - [[nodiscard]] bool is_null() const { return m_is_null; } - [[nodiscard]] u8& operator[](size_t i) { VERIFY(i < m_size); @@ -152,7 +147,6 @@ public: void grow(size_t new_size) { - m_is_null = false; if (new_size <= m_size) return; if (new_size <= capacity()) { @@ -208,20 +202,17 @@ private: ByteBuffer(size_t size) { grow(size); - VERIFY(!m_is_null); VERIFY(m_size == size); } void move_from(ByteBuffer&& other) { - m_is_null = other.m_is_null; m_size = other.m_size; if (other.m_size > inline_capacity) { m_outline_buffer = other.m_outline_buffer; m_outline_capacity = other.m_outline_capacity; } else __builtin_memcpy(m_inline_buffer, other.m_inline_buffer, other.m_size); - other.m_is_null = true; other.m_size = 0; } @@ -242,7 +233,6 @@ private: size_t capacity() const { return is_inline() ? inline_capacity : m_outline_capacity; } size_t m_size { 0 }; - bool m_is_null { true }; union { u8 m_inline_buffer[inline_capacity]; struct { diff --git a/AK/String.h b/AK/String.h index 621ff92dc11..ff729558cd1 100644 --- a/AK/String.h +++ b/AK/String.h @@ -240,8 +240,6 @@ public: template [[nodiscard]] static String copy(const BufferType& buffer, ShouldChomp should_chomp = NoChomp) { - if (buffer.is_null()) - return {}; if (buffer.is_empty()) return empty(); return String((const char*)buffer.data(), buffer.size(), should_chomp); diff --git a/AK/Vector.h b/AK/Vector.h index 7acc9bdd154..55cf46ca2a7 100644 --- a/AK/Vector.h +++ b/AK/Vector.h @@ -150,8 +150,6 @@ public: return false; } - // NOTE: Vector::is_null() exists for the benefit of String::copy(). - bool is_null() const { return false; } bool is_empty() const { return size() == 0; } ALWAYS_INLINE size_t size() const { return m_size; } size_t capacity() const { return m_capacity; } diff --git a/Userland/Applications/HexEditor/HexEditor.cpp b/Userland/Applications/HexEditor/HexEditor.cpp index 31177674e3c..021eaac9652 100644 --- a/Userland/Applications/HexEditor/HexEditor.cpp +++ b/Userland/Applications/HexEditor/HexEditor.cpp @@ -563,11 +563,6 @@ int HexEditor::find_and_highlight(ByteBuffer& needle, int start) if (m_buffer.is_empty()) return -1; - if (needle.is_null()) { - dbgln("needle is null"); - return -1; - } - auto raw_offset = memmem(m_buffer.data() + start, m_buffer.size(), needle.data(), needle.size()); if (raw_offset == NULL) return -1; diff --git a/Userland/Applications/HexEditor/HexEditorWidget.cpp b/Userland/Applications/HexEditor/HexEditorWidget.cpp index b328e8533cd..2be3aed1ce8 100644 --- a/Userland/Applications/HexEditor/HexEditorWidget.cpp +++ b/Userland/Applications/HexEditor/HexEditorWidget.cpp @@ -189,7 +189,7 @@ void HexEditorWidget::initialize_menubar(GUI::Menubar& menubar) })); edit_menu.add_action(GUI::Action::create("Find &Next", { Mod_None, Key_F3 }, Gfx::Bitmap::load_from_file("/res/icons/16x16/find-next.png"), [&](const GUI::Action&) { - if (m_search_text.is_empty() || m_search_buffer.is_empty() || m_search_buffer.is_null()) { + if (m_search_text.is_empty() || m_search_buffer.is_empty()) { GUI::MessageBox::show(window(), "Nothing to search for", "Not found", GUI::MessageBox::Type::Warning); return; } diff --git a/Userland/Libraries/LibCore/IODevice.cpp b/Userland/Libraries/LibCore/IODevice.cpp index cde5c55548f..6dd7e3dc9c9 100644 --- a/Userland/Libraries/LibCore/IODevice.cpp +++ b/Userland/Libraries/LibCore/IODevice.cpp @@ -33,8 +33,6 @@ const char* IODevice::error_string() const int IODevice::read(u8* buffer, int length) { auto read_buffer = read(length); - if (read_buffer.is_null()) - return 0; memcpy(buffer, read_buffer.data(), length); return read_buffer.size(); } @@ -151,8 +149,6 @@ ByteBuffer IODevice::read_all() } data.append((const u8*)read_buffer, nread); } - if (data.is_empty()) - return {}; return ByteBuffer::copy(data.data(), data.size()); } diff --git a/Userland/Libraries/LibGemini/Job.cpp b/Userland/Libraries/LibGemini/Job.cpp index e7c2f20ecad..bd0dfa5db36 100644 --- a/Userland/Libraries/LibGemini/Job.cpp +++ b/Userland/Libraries/LibGemini/Job.cpp @@ -110,7 +110,7 @@ void Job::on_socket_connected() auto read_size = 64 * KiB; auto payload = receive(read_size); - if (!payload) { + if (payload.is_empty()) { if (eof()) { finish_up(); return IterationDecision::Break; diff --git a/Userland/Libraries/LibHTTP/Job.cpp b/Userland/Libraries/LibHTTP/Job.cpp index 6ead5fad106..bed513576ba 100644 --- a/Userland/Libraries/LibHTTP/Job.cpp +++ b/Userland/Libraries/LibHTTP/Job.cpp @@ -283,7 +283,7 @@ void Job::on_socket_connected() } auto payload = receive(read_size); - if (!payload) { + if (payload.is_empty()) { if (eof()) { finish_up(); return IterationDecision::Break; diff --git a/Userland/Libraries/LibTLS/ClientHandshake.cpp b/Userland/Libraries/LibTLS/ClientHandshake.cpp index c012df5bc71..ebe9949abce 100644 --- a/Userland/Libraries/LibTLS/ClientHandshake.cpp +++ b/Userland/Libraries/LibTLS/ClientHandshake.cpp @@ -474,9 +474,7 @@ ssize_t TLSv12::handle_payload(ReadonlyBytes vbuffer) } break; case Finished: - if (m_context.cached_handshake) { - m_context.cached_handshake.clear(); - } + m_context.cached_handshake.clear(); if (m_context.handshake_messages[10] >= 1) { dbgln("unexpected finished message"); payload_res = (i8)Error::UnexpectedMessage; diff --git a/Userland/Libraries/LibWeb/CSS/Parser/DeprecatedCSSParser.cpp b/Userland/Libraries/LibWeb/CSS/Parser/DeprecatedCSSParser.cpp index 83366ae1f7b..244ba6c0cc5 100644 --- a/Userland/Libraries/LibWeb/CSS/Parser/DeprecatedCSSParser.cpp +++ b/Userland/Libraries/LibWeb/CSS/Parser/DeprecatedCSSParser.cpp @@ -431,7 +431,7 @@ public: if (type != CSS::Selector::SimpleSelector::Type::Universal) { while (is_valid_selector_char(peek())) buffer.append(consume_one()); - PARSE_VERIFY(!buffer.is_null()); + PARSE_VERIFY(!buffer.is_empty()); } auto value = String::copy(buffer); diff --git a/Userland/Libraries/LibWeb/Loader/Resource.h b/Userland/Libraries/LibWeb/Loader/Resource.h index 1575040ba0a..1fca238e3e4 100644 --- a/Userland/Libraries/LibWeb/Loader/Resource.h +++ b/Userland/Libraries/LibWeb/Loader/Resource.h @@ -42,7 +42,7 @@ public: bool is_failed() const { return m_failed; } const String& error() const { return m_error; } - bool has_encoded_data() const { return !m_encoded_data.is_null(); } + bool has_encoded_data() const { return !m_encoded_data.is_empty(); } const URL& url() const { return m_request.url(); } const ByteBuffer& encoded_data() const { return m_encoded_data; } diff --git a/Userland/Libraries/LibWeb/XHR/XMLHttpRequest.cpp b/Userland/Libraries/LibWeb/XHR/XMLHttpRequest.cpp index baa68b4727d..250f22fc39a 100644 --- a/Userland/Libraries/LibWeb/XHR/XMLHttpRequest.cpp +++ b/Userland/Libraries/LibWeb/XHR/XMLHttpRequest.cpp @@ -47,7 +47,7 @@ void XMLHttpRequest::fire_progress_event(const String& event_name, u64 transmitt String XMLHttpRequest::response_text() const { - if (m_response_object.is_null()) + if (m_response_object.is_empty()) return {}; return String::copy(m_response_object); } diff --git a/Userland/Services/FileOperation/main.cpp b/Userland/Services/FileOperation/main.cpp index 632f7e721c5..44919a9a5f7 100644 --- a/Userland/Services/FileOperation/main.cpp +++ b/Userland/Services/FileOperation/main.cpp @@ -143,7 +143,7 @@ int perform_copy(const String& source, const String& destination) while (true) { print_progress(); auto buffer = source_file.read(65536); - if (buffer.is_null()) + if (buffer.is_empty()) break; if (!destination_file.write(buffer)) { report_warning(String::formatted("Failed to write to destination file: {}", destination_file.error_string())); diff --git a/Userland/Services/LookupServer/MulticastDNS.cpp b/Userland/Services/LookupServer/MulticastDNS.cpp index 31131c2a469..268c15ac040 100644 --- a/Userland/Services/LookupServer/MulticastDNS.cpp +++ b/Userland/Services/LookupServer/MulticastDNS.cpp @@ -168,7 +168,7 @@ Vector MulticastDNS::lookup(const DNSName& name, DNSRecordType record } auto buffer = receive(1024); - if (!buffer) + if (buffer.is_empty()) return {}; auto optional_packet = DNSPacket::from_raw_packet(buffer.data(), buffer.size()); if (!optional_packet.has_value()) { diff --git a/Userland/Services/WebServer/Client.cpp b/Userland/Services/WebServer/Client.cpp index bdde301e5d9..49462bba665 100644 --- a/Userland/Services/WebServer/Client.cpp +++ b/Userland/Services/WebServer/Client.cpp @@ -41,7 +41,7 @@ void Client::start() { m_socket->on_ready_to_read = [this] { auto raw_request = m_socket->read_all(); - if (raw_request.is_null()) { + if (raw_request.is_empty()) { die(); return; } diff --git a/Userland/Utilities/du.cpp b/Userland/Utilities/du.cpp index 038b150d580..fc1ea7acf6a 100644 --- a/Userland/Utilities/du.cpp +++ b/Userland/Utilities/du.cpp @@ -106,7 +106,8 @@ int parse_args(int argc, char** argv, Vector& files, DuOption& du_option auto file = Core::File::construct(exclude_from); bool success = file->open(Core::OpenMode::ReadOnly); VERIFY(success); - if (const auto buff = file->read_all()) { + const auto buff = file->read_all(); + if (!buff.is_empty()) { String patterns = String::copy(buff, Chomp); du_option.excluded_patterns.append(patterns.split('\n')); }