AK: Use size_t for ByteBuffer sizes

This matches what we already do for string types.
This commit is contained in:
Andreas Kling 2020-02-20 12:54:15 +01:00
parent 1dfc66c7cc
commit 88b9fcb976
Notes: sideshowbarker 2024-07-19 09:12:19 +09:00
14 changed files with 75 additions and 70 deletions

View file

@ -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 };
};

View file

@ -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));
}

View file

@ -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());

View file

@ -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;

View file

@ -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;

View file

@ -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));

View file

@ -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)

View file

@ -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();

View file

@ -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);
}

View file

@ -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();

View file

@ -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())

View file

@ -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;
}

View file

@ -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;
}

View file

@ -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");