summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTom <tomut@yahoo.com>2020-08-30 09:00:57 -0600
committerAndreas Kling <kling@serenityos.org>2020-08-30 17:30:48 +0200
commitf5bc7dbfda0fe011cafd507ed890d4f909918f35 (patch)
tree59b7dbb7d2c56b165a00fe6d7b63757bac3f088b
parent9ad5a261f7fe03aac6d2f6261eb74026145781ea (diff)
downloadserenity-f5bc7dbfda0fe011cafd507ed890d4f909918f35.zip
AK: Fix ByteBuffer zero bytes allocations
-rw-r--r--AK/ByteBuffer.h22
1 files changed, 17 insertions, 5 deletions
diff --git a/AK/ByteBuffer.h b/AK/ByteBuffer.h
index 43a79c2df1..ca4a615fbb 100644
--- a/AK/ByteBuffer.h
+++ b/AK/ByteBuffer.h
@@ -256,7 +256,8 @@ private:
inline ByteBufferImpl::ByteBufferImpl(size_t size)
: m_size(size)
{
- m_data = static_cast<u8*>(kmalloc(size));
+ if (size != 0)
+ m_data = static_cast<u8*>(kmalloc(size));
m_owned = true;
}
@@ -264,8 +265,10 @@ inline ByteBufferImpl::ByteBufferImpl(const void* data, size_t size, Constructio
: m_size(size)
{
ASSERT(mode == Copy);
- m_data = static_cast<u8*>(kmalloc(size));
- __builtin_memcpy(m_data, data, size);
+ if (size != 0) {
+ m_data = static_cast<u8*>(kmalloc(size));
+ __builtin_memcpy(m_data, data, size);
+ }
m_owned = true;
}
@@ -284,12 +287,20 @@ inline void ByteBufferImpl::grow(size_t size)
{
ASSERT(size > m_size);
ASSERT(m_owned);
+ if (size == 0) {
+ if (m_data)
+ kfree(m_data);
+ m_data = nullptr;
+ m_size = 0;
+ return;
+ }
u8* new_data = static_cast<u8*>(kmalloc(size));
__builtin_memcpy(new_data, m_data, m_size);
u8* old_data = m_data;
m_data = new_data;
m_size = size;
- kfree(old_data);
+ if (old_data)
+ kfree(old_data);
}
inline NonnullRefPtr<ByteBufferImpl> ByteBufferImpl::create_uninitialized(size_t size)
@@ -300,7 +311,8 @@ inline NonnullRefPtr<ByteBufferImpl> ByteBufferImpl::create_uninitialized(size_t
inline NonnullRefPtr<ByteBufferImpl> ByteBufferImpl::create_zeroed(size_t size)
{
auto buffer = ::adopt(*new ByteBufferImpl(size));
- __builtin_memset(buffer->data(), 0, size);
+ if (size != 0)
+ __builtin_memset(buffer->data(), 0, size);
return buffer;
}