diff options
author | Andreas Kling <kling@serenityos.org> | 2020-02-20 12:54:15 +0100 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2020-02-20 13:20:34 +0100 |
commit | 88b9fcb9764bba777fe18bb28114a3743bb971a4 (patch) | |
tree | d3eb3caba906f2ec0beab721d4b69626a0b249ab | |
parent | 1dfc66c7cc055d03a87f5851334c5ae2f65d1e2d (diff) | |
download | serenity-88b9fcb9764bba777fe18bb28114a3743bb971a4.zip |
AK: Use size_t for ByteBuffer sizes
This matches what we already do for string types.
-rw-r--r-- | AK/BufferStream.h | 4 | ||||
-rw-r--r-- | AK/ByteBuffer.h | 78 | ||||
-rw-r--r-- | Applications/HexEditor/HexEditor.cpp | 26 | ||||
-rw-r--r-- | Applications/IRCClient/IRCClient.cpp | 2 | ||||
-rw-r--r-- | DevTools/IPCCompiler/main.cpp | 4 | ||||
-rw-r--r-- | DevTools/Inspector/RemoteProcess.cpp | 2 | ||||
-rw-r--r-- | Kernel/FileSystem/Ext2FileSystem.cpp | 4 | ||||
-rw-r--r-- | Kernel/FileSystem/FileDescription.cpp | 9 | ||||
-rw-r--r-- | Libraries/LibCore/EventLoop.cpp | 4 | ||||
-rw-r--r-- | Libraries/LibCore/Gzip.cpp | 2 | ||||
-rw-r--r-- | Libraries/LibCore/HttpRequest.cpp | 2 | ||||
-rw-r--r-- | Libraries/LibCore/Socket.cpp | 4 | ||||
-rw-r--r-- | Userland/dmesg.cpp | 2 | ||||
-rw-r--r-- | Userland/rpcdump.cpp | 2 |
14 files changed, 75 insertions, 70 deletions
diff --git a/AK/BufferStream.h b/AK/BufferStream.h index b9f2990877..2e2e6bac17 100644 --- a/AK/BufferStream.h +++ b/AK/BufferStream.h @@ -311,7 +311,7 @@ public: BufferStream& operator<<(const ByteBuffer& value) { - for (ssize_t i = 0; i < value.size(); ++i) + for (size_t i = 0; i < value.size(); ++i) m_buffer[m_offset++] = value[i]; return *this; } @@ -349,7 +349,7 @@ public: private: ByteBuffer& m_buffer; - ssize_t m_offset { 0 }; + size_t m_offset { 0 }; bool m_read_failure { false }; }; diff --git a/AK/ByteBuffer.h b/AK/ByteBuffer.h index a15c42f375..23684f9820 100644 --- a/AK/ByteBuffer.h +++ b/AK/ByteBuffer.h @@ -38,12 +38,12 @@ namespace AK { class ByteBufferImpl : public RefCounted<ByteBufferImpl> { public: - static NonnullRefPtr<ByteBufferImpl> create_uninitialized(int size); - static NonnullRefPtr<ByteBufferImpl> create_zeroed(int); - static NonnullRefPtr<ByteBufferImpl> copy(const void*, int); - static NonnullRefPtr<ByteBufferImpl> wrap(void*, int); - static NonnullRefPtr<ByteBufferImpl> wrap(const void*, int); - static NonnullRefPtr<ByteBufferImpl> adopt(void*, int); + static NonnullRefPtr<ByteBufferImpl> create_uninitialized(size_t size); + static NonnullRefPtr<ByteBufferImpl> create_zeroed(size_t); + static NonnullRefPtr<ByteBufferImpl> copy(const void*, size_t); + static NonnullRefPtr<ByteBufferImpl> wrap(void*, size_t); + static NonnullRefPtr<ByteBufferImpl> wrap(const void*, size_t); + static NonnullRefPtr<ByteBufferImpl> adopt(void*, size_t); ~ByteBufferImpl() { clear(); } @@ -56,18 +56,18 @@ public: m_data = nullptr; } - u8& operator[](int i) + u8& operator[](size_t i) { ASSERT(i < m_size); return m_data[i]; } - const u8& operator[](int i) const + const u8& operator[](size_t i) const { ASSERT(i < m_size); return m_data[i]; } bool is_empty() const { return !m_size; } - int size() const { return m_size; } + size_t size() const { return m_size; } u8* data() { return m_data; } const u8* data() const { return m_data; } @@ -79,13 +79,13 @@ public: const void* end_pointer() const { return m_data + m_size; } // NOTE: trim() does not reallocate. - void trim(int size) + void trim(size_t size) { ASSERT(size <= m_size); m_size = size; } - void grow(int size); + void grow(size_t size); private: enum ConstructionMode { @@ -94,13 +94,13 @@ private: Wrap, Adopt }; - explicit ByteBufferImpl(int); // For ConstructionMode=Uninitialized - ByteBufferImpl(const void*, int, ConstructionMode); // For ConstructionMode=Copy - ByteBufferImpl(void*, int, ConstructionMode); // For ConstructionMode=Wrap/Adopt + explicit ByteBufferImpl(size_t); // For ConstructionMode=Uninitialized + ByteBufferImpl(const void*, size_t, ConstructionMode); // For ConstructionMode=Copy + ByteBufferImpl(void*, size_t, ConstructionMode); // For ConstructionMode=Wrap/Adopt ByteBufferImpl() {} u8* m_data { nullptr }; - int m_size { 0 }; + size_t m_size { 0 }; bool m_owned { false }; }; @@ -129,12 +129,12 @@ public: return *this; } - static ByteBuffer create_uninitialized(int size) { return ByteBuffer(ByteBufferImpl::create_uninitialized(size)); } - static ByteBuffer create_zeroed(int size) { return ByteBuffer(ByteBufferImpl::create_zeroed(size)); } - static ByteBuffer copy(const void* data, int size) { return ByteBuffer(ByteBufferImpl::copy(data, size)); } - static ByteBuffer wrap(const void* data, int size) { return ByteBuffer(ByteBufferImpl::wrap(data, size)); } - static ByteBuffer wrap(void* data, int size) { return ByteBuffer(ByteBufferImpl::wrap(data, size)); } - static ByteBuffer adopt(void* data, int size) { return ByteBuffer(ByteBufferImpl::adopt(data, size)); } + static ByteBuffer create_uninitialized(size_t size) { return ByteBuffer(ByteBufferImpl::create_uninitialized(size)); } + static ByteBuffer create_zeroed(size_t size) { return ByteBuffer(ByteBufferImpl::create_zeroed(size)); } + static ByteBuffer copy(const void* data, size_t size) { return ByteBuffer(ByteBufferImpl::copy(data, size)); } + static ByteBuffer wrap(const void* data, size_t size) { return ByteBuffer(ByteBufferImpl::wrap(data, size)); } + static ByteBuffer wrap(void* data, size_t size) { return ByteBuffer(ByteBufferImpl::wrap(data, size)); } + static ByteBuffer adopt(void* data, size_t size) { return ByteBuffer(ByteBufferImpl::adopt(data, size)); } ~ByteBuffer() { clear(); } void clear() { m_impl = nullptr; } @@ -143,18 +143,18 @@ public: bool operator!() const { return is_null(); } bool is_null() const { return m_impl == nullptr; } - u8& operator[](int i) + u8& operator[](size_t i) { ASSERT(m_impl); return (*m_impl)[i]; } - u8 operator[](int i) const + u8 operator[](size_t i) const { ASSERT(m_impl); return (*m_impl)[i]; } bool is_empty() const { return !m_impl || m_impl->is_empty(); } - int size() const { return m_impl ? m_impl->size() : 0; } + size_t size() const { return m_impl ? m_impl->size() : 0; } u8* data() { return m_impl ? m_impl->data() : nullptr; } const u8* data() const { return m_impl ? m_impl->data() : nullptr; } @@ -173,13 +173,13 @@ public: } // NOTE: trim() does not reallocate. - void trim(int size) + void trim(size_t size) { if (m_impl) m_impl->trim(size); } - ByteBuffer slice_view(int offset, int size) const + ByteBuffer slice_view(size_t offset, size_t size) const { if (is_null()) return {}; @@ -190,7 +190,7 @@ public: return wrap(offset_pointer(offset), size); } - ByteBuffer slice(int offset, int size) const + ByteBuffer slice(size_t offset, size_t size) const { if (is_null()) return {}; @@ -201,7 +201,7 @@ public: return copy(offset_pointer(offset), size); } - void grow(int size) + void grow(size_t size) { if (!m_impl) m_impl = ByteBufferImpl::create_uninitialized(size); @@ -209,7 +209,7 @@ public: m_impl->grow(size); } - void append(const void* data, int data_size) + void append(const void* data, size_t data_size) { int old_size = size(); grow(size() + data_size); @@ -225,14 +225,14 @@ private: RefPtr<ByteBufferImpl> m_impl; }; -inline ByteBufferImpl::ByteBufferImpl(int size) +inline ByteBufferImpl::ByteBufferImpl(size_t size) : m_size(size) { m_data = static_cast<u8*>(kmalloc(size)); m_owned = true; } -inline ByteBufferImpl::ByteBufferImpl(const void* data, int size, ConstructionMode mode) +inline ByteBufferImpl::ByteBufferImpl(const void* data, size_t size, ConstructionMode mode) : m_size(size) { ASSERT(mode == Copy); @@ -241,7 +241,7 @@ inline ByteBufferImpl::ByteBufferImpl(const void* data, int size, ConstructionMo m_owned = true; } -inline ByteBufferImpl::ByteBufferImpl(void* data, int size, ConstructionMode mode) +inline ByteBufferImpl::ByteBufferImpl(void* data, size_t size, ConstructionMode mode) : m_data(static_cast<u8*>(data)) , m_size(size) { @@ -252,7 +252,7 @@ inline ByteBufferImpl::ByteBufferImpl(void* data, int size, ConstructionMode mod } } -inline void ByteBufferImpl::grow(int size) +inline void ByteBufferImpl::grow(size_t size) { ASSERT(size > m_size); ASSERT(m_owned); @@ -264,34 +264,34 @@ inline void ByteBufferImpl::grow(int size) kfree(old_data); } -inline NonnullRefPtr<ByteBufferImpl> ByteBufferImpl::create_uninitialized(int size) +inline NonnullRefPtr<ByteBufferImpl> ByteBufferImpl::create_uninitialized(size_t size) { return ::adopt(*new ByteBufferImpl(size)); } -inline NonnullRefPtr<ByteBufferImpl> ByteBufferImpl::create_zeroed(int size) +inline NonnullRefPtr<ByteBufferImpl> ByteBufferImpl::create_zeroed(size_t size) { auto buffer = ::adopt(*new ByteBufferImpl(size)); memset(buffer->data(), 0, size); return buffer; } -inline NonnullRefPtr<ByteBufferImpl> ByteBufferImpl::copy(const void* data, int size) +inline NonnullRefPtr<ByteBufferImpl> ByteBufferImpl::copy(const void* data, size_t size) { return ::adopt(*new ByteBufferImpl(data, size, Copy)); } -inline NonnullRefPtr<ByteBufferImpl> ByteBufferImpl::wrap(void* data, int size) +inline NonnullRefPtr<ByteBufferImpl> ByteBufferImpl::wrap(void* data, size_t size) { return ::adopt(*new ByteBufferImpl(data, size, Wrap)); } -inline NonnullRefPtr<ByteBufferImpl> ByteBufferImpl::wrap(const void* data, int size) +inline NonnullRefPtr<ByteBufferImpl> ByteBufferImpl::wrap(const void* data, size_t size) { return ::adopt(*new ByteBufferImpl(const_cast<void*>(data), size, Wrap)); } -inline NonnullRefPtr<ByteBufferImpl> ByteBufferImpl::adopt(void* data, int size) +inline NonnullRefPtr<ByteBufferImpl> ByteBufferImpl::adopt(void* data, size_t size) { return ::adopt(*new ByteBufferImpl(data, size, Adopt)); } diff --git a/Applications/HexEditor/HexEditor.cpp b/Applications/HexEditor/HexEditor.cpp index f5c96c7c77..1c4f1a53f7 100644 --- a/Applications/HexEditor/HexEditor.cpp +++ b/Applications/HexEditor/HexEditor.cpp @@ -92,7 +92,7 @@ void HexEditor::fill_selection(u8 fill_byte) void HexEditor::set_position(int position) { - if (position > m_buffer.size()) + if (position > static_cast<int>(m_buffer.size())) return; m_position = position; @@ -125,7 +125,7 @@ bool HexEditor::write_to_file(const StringView& path) return false; } - if (nwritten == m_buffer.size()) { + if (static_cast<size_t>(nwritten) == m_buffer.size()) { m_tracked_changes.clear(); update(); } @@ -224,7 +224,7 @@ void HexEditor::mousedown_event(GUI::MouseEvent& event) auto byte_y = (absolute_y - hex_start_y) / line_height(); auto offset = (byte_y * m_bytes_per_row) + byte_x; - if (offset < 0 || offset > m_buffer.size()) + if (offset < 0 || offset > static_cast<int>(m_buffer.size())) return; #ifdef HEX_DEBUG @@ -246,7 +246,7 @@ void HexEditor::mousedown_event(GUI::MouseEvent& event) auto byte_y = (absolute_y - text_start_y) / line_height(); auto offset = (byte_y * m_bytes_per_row) + byte_x; - if (offset < 0 || offset > m_buffer.size()) + if (offset < 0 || offset > static_cast<int>(m_buffer.size())) return; #ifdef HEX_DEBUG @@ -293,7 +293,7 @@ void HexEditor::mousemove_event(GUI::MouseEvent& event) auto byte_y = (absolute_y - hex_start_y) / line_height(); auto offset = (byte_y * m_bytes_per_row) + byte_x; - if (offset < 0 || offset > m_buffer.size()) + if (offset < 0 || offset > static_cast<int>(m_buffer.size())) return; m_selection_end = offset; @@ -304,7 +304,7 @@ void HexEditor::mousemove_event(GUI::MouseEvent& event) auto byte_x = (absolute_x - text_start_x) / character_width(); auto byte_y = (absolute_y - text_start_y) / line_height(); auto offset = (byte_y * m_bytes_per_row) + byte_x; - if (offset < 0 || offset > m_buffer.size()) + if (offset < 0 || offset > static_cast<int>(m_buffer.size())) return; m_selection_end = offset; @@ -367,7 +367,7 @@ void HexEditor::keydown_event(GUI::KeyEvent& event) } if (event.key() == KeyCode::Key_Down) { - if (m_position + bytes_per_row() < m_buffer.size()) { + if (m_position + bytes_per_row() < static_cast<int>(m_buffer.size())) { m_position += bytes_per_row(); m_byte_position = 0; scroll_position_into_view(m_position); @@ -389,7 +389,7 @@ void HexEditor::keydown_event(GUI::KeyEvent& event) } if (event.key() == KeyCode::Key_Right) { - if (m_position + 1 < m_buffer.size()) { + if (m_position + 1 < static_cast<int>(m_buffer.size())) { m_position++; m_byte_position = 0; scroll_position_into_view(m_position); @@ -425,7 +425,7 @@ void HexEditor::hex_mode_keydown_event(GUI::KeyEvent& event) if (m_buffer.is_empty()) return; ASSERT(m_position >= 0); - ASSERT(m_position < m_buffer.size()); + ASSERT(m_position < static_cast<int>(m_buffer.size())); // yes, this is terrible... but it works. auto value = (event.key() >= KeyCode::Key_0 && event.key() <= KeyCode::Key_9) @@ -438,7 +438,7 @@ void HexEditor::hex_mode_keydown_event(GUI::KeyEvent& event) m_byte_position++; } else { m_buffer.data()[m_position] = (m_buffer.data()[m_position] & 0xF0) | value; // save the first 4 bits, OR the new value in the last 4 - if (m_position + 1 < m_buffer.size()) + if (m_position + 1 < static_cast<int>(m_buffer.size())) m_position++; m_byte_position = 0; } @@ -454,11 +454,11 @@ void HexEditor::text_mode_keydown_event(GUI::KeyEvent& event) if (m_buffer.is_empty()) return; ASSERT(m_position >= 0); - ASSERT(m_position < m_buffer.size()); + ASSERT(m_position < static_cast<int>(m_buffer.size())); m_tracked_changes.set(m_position, m_buffer.data()[m_position]); m_buffer.data()[m_position] = (u8)event.text().characters()[0]; // save the first 4 bits, OR the new value in the last 4 - if (m_position + 1 < m_buffer.size()) + if (m_position + 1 < static_cast<int>(m_buffer.size())) m_position++; m_byte_position = 0; @@ -534,7 +534,7 @@ void HexEditor::paint_event(GUI::PaintEvent& event) for (int i = min_row; i < max_row; i++) { for (int j = 0; j < bytes_per_row(); j++) { auto byte_position = (i * bytes_per_row()) + j; - if (byte_position >= m_buffer.size()) + if (byte_position >= static_cast<int>(m_buffer.size())) return; Color text_color = palette().color(foreground_role()); diff --git a/Applications/IRCClient/IRCClient.cpp b/Applications/IRCClient/IRCClient.cpp index be027cfe45..0aa6fc4812 100644 --- a/Applications/IRCClient/IRCClient.cpp +++ b/Applications/IRCClient/IRCClient.cpp @@ -142,7 +142,7 @@ void IRCClient::process_line(ByteBuffer&& line) } state = Start; - for (int i = 0; i < line.size(); ++i) { + for (size_t i = 0; i < line.size(); ++i) { char ch = line[i]; if (ch == '\r') continue; diff --git a/DevTools/IPCCompiler/main.cpp b/DevTools/IPCCompiler/main.cpp index d495f2efed..26edb81709 100644 --- a/DevTools/IPCCompiler/main.cpp +++ b/DevTools/IPCCompiler/main.cpp @@ -79,9 +79,9 @@ int main(int argc, char** argv) Vector<char> buffer; - int index = 0; + size_t index = 0; - auto peek = [&](int offset = 0) -> char { + auto peek = [&](size_t offset = 0) -> char { if ((index + offset) < file_contents.size()) return file_contents[index + offset]; return 0; diff --git a/DevTools/Inspector/RemoteProcess.cpp b/DevTools/Inspector/RemoteProcess.cpp index 49ec3312ee..abf1098504 100644 --- a/DevTools/Inspector/RemoteProcess.cpp +++ b/DevTools/Inspector/RemoteProcess.cpp @@ -121,7 +121,7 @@ void RemoteProcess::update() return; } - i32 length; + u32 length; int nread = m_socket->read((u8*)&length, sizeof(length)); ASSERT(nread == sizeof(length)); diff --git a/Kernel/FileSystem/Ext2FileSystem.cpp b/Kernel/FileSystem/Ext2FileSystem.cpp index 8778c2ca05..5bfb9cd4aa 100644 --- a/Kernel/FileSystem/Ext2FileSystem.cpp +++ b/Kernel/FileSystem/Ext2FileSystem.cpp @@ -923,8 +923,10 @@ bool Ext2FSInode::write_directory(const Vector<FS::DirectoryEntry>& entries) stream.fill_to_end(0); ssize_t nwritten = write_bytes(0, directory_data.size(), directory_data.data(), nullptr); + if (nwritten < 0) + return false; set_metadata_dirty(true); - return nwritten == directory_data.size(); + return static_cast<size_t>(nwritten) == directory_data.size(); } KResult Ext2FSInode::add_child(InodeIdentifier child_id, const StringView& name, mode_t mode) diff --git a/Kernel/FileSystem/FileDescription.cpp b/Kernel/FileSystem/FileDescription.cpp index 30cba4dd13..1bd1659563 100644 --- a/Kernel/FileSystem/FileDescription.cpp +++ b/Kernel/FileSystem/FileDescription.cpp @@ -182,7 +182,10 @@ ssize_t FileDescription::get_dir_entries(u8* buffer, ssize_t size) if (!metadata.is_valid()) return -EIO; - int size_to_allocate = max(PAGE_SIZE, metadata.size); + if (size < 0) + return -EINVAL; + + size_t size_to_allocate = max(PAGE_SIZE, metadata.size); auto temp_buffer = ByteBuffer::create_uninitialized(size_to_allocate); BufferStream stream(temp_buffer); @@ -195,8 +198,8 @@ ssize_t FileDescription::get_dir_entries(u8* buffer, ssize_t size) }); stream.snip(); - if (size < temp_buffer.size()) - return -1; + if (static_cast<size_t>(size) < temp_buffer.size()) + return -EINVAL; copy_to_user(buffer, temp_buffer.data(), temp_buffer.size()); return stream.offset(); diff --git a/Libraries/LibCore/EventLoop.cpp b/Libraries/LibCore/EventLoop.cpp index 2b604e7f14..733acf0e11 100644 --- a/Libraries/LibCore/EventLoop.cpp +++ b/Libraries/LibCore/EventLoop.cpp @@ -90,7 +90,7 @@ public: s_rpc_clients.set(m_client_id, this); add_child(*m_socket); m_socket->on_ready_to_read = [this] { - i32 length; + u32 length; int nread = m_socket->read((u8*)&length, sizeof(length)); if (nread == 0) { dbg() << "RPC client disconnected"; @@ -117,7 +117,7 @@ public: void send_response(const JsonObject& response) { auto serialized = response.to_string(); - i32 length = serialized.length(); + u32 length = serialized.length(); m_socket->write((const u8*)&length, sizeof(length)); m_socket->write(serialized); } diff --git a/Libraries/LibCore/Gzip.cpp b/Libraries/LibCore/Gzip.cpp index 27238d6c19..28a2de7152 100644 --- a/Libraries/LibCore/Gzip.cpp +++ b/Libraries/LibCore/Gzip.cpp @@ -40,7 +40,7 @@ bool CGzip::is_compressed(const ByteBuffer& data) // see: https://tools.ietf.org/html/rfc1952#page-5 static Optional<ByteBuffer> get_gzip_payload(const ByteBuffer& data) { - int current = 0; + size_t current = 0; auto read_byte = [&]() { if (current >= data.size()) { ASSERT_NOT_REACHED(); diff --git a/Libraries/LibCore/HttpRequest.cpp b/Libraries/LibCore/HttpRequest.cpp index c1209b0ea5..14ea2ff816 100644 --- a/Libraries/LibCore/HttpRequest.cpp +++ b/Libraries/LibCore/HttpRequest.cpp @@ -82,7 +82,7 @@ Optional<HttpRequest> HttpRequest::from_raw_request(const ByteBuffer& raw_reques }; State state { State::InMethod }; - int index = 0; + size_t index = 0; auto peek = [&](int offset = 0) -> u8 { if (index + offset >= raw_request.size()) diff --git a/Libraries/LibCore/Socket.cpp b/Libraries/LibCore/Socket.cpp index 4810f1cd53..35590d541a 100644 --- a/Libraries/LibCore/Socket.cpp +++ b/Libraries/LibCore/Socket.cpp @@ -162,12 +162,12 @@ ByteBuffer Socket::receive(int max_size) bool Socket::send(const ByteBuffer& data) { - int nsent = ::send(fd(), data.data(), data.size(), 0); + ssize_t nsent = ::send(fd(), data.data(), data.size(), 0); if (nsent < 0) { set_error(errno); return false; } - ASSERT(nsent == data.size()); + ASSERT(static_cast<size_t>(nsent) == data.size()); return true; } diff --git a/Userland/dmesg.cpp b/Userland/dmesg.cpp index f116e3a3c5..1557398fa2 100644 --- a/Userland/dmesg.cpp +++ b/Userland/dmesg.cpp @@ -53,7 +53,7 @@ int main(int argc, char** argv) return 1; } const auto& b = f->read_all(); - for (auto i = 0; i < b.size(); ++i) + for (size_t i = 0; i < b.size(); ++i) putchar(b[i]); return 0; } diff --git a/Userland/rpcdump.cpp b/Userland/rpcdump.cpp index 3a2b82bcb4..7975911e2a 100644 --- a/Userland/rpcdump.cpp +++ b/Userland/rpcdump.cpp @@ -81,7 +81,7 @@ int main(int argc, char** argv) auto data = socket->read_all(); - for (int i = 0; i < data.size(); ++i) + for (size_t i = 0; i < data.size(); ++i) putchar(data[i]); printf("\n"); |