Skip to content

Commit

Permalink
Fix buggy memcpy call in a template (#15672)
Browse files Browse the repository at this point in the history
  • Loading branch information
cosin15 authored Jan 14, 2025
1 parent 2bfcd45 commit 7053348
Showing 1 changed file with 34 additions and 45 deletions.
79 changes: 34 additions & 45 deletions src/util/pointer.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,11 @@ class Buffer
Buffer(unsigned int size)
{
m_size = size;
if(size != 0)
if (size != 0) {
data = new T[size];
else
} else {
data = nullptr;
}
}

// Disable class copy
Expand All @@ -49,26 +50,24 @@ class Buffer
Buffer(Buffer &&buffer)
{
m_size = buffer.m_size;
if(m_size != 0)
{
if (m_size != 0) {
data = buffer.data;
buffer.data = nullptr;
buffer.m_size = 0;
}
else
} else {
data = nullptr;
}
}
// Copies whole buffer
Buffer(const T *t, unsigned int size)
{
m_size = size;
if(size != 0)
{
if (size != 0) {
data = new T[size];
memcpy(data, t, size);
}
else
memcpy(data, t, sizeof(T) * size);
} else {
data = nullptr;
}
}

~Buffer()
Expand All @@ -78,18 +77,18 @@ class Buffer

Buffer& operator=(Buffer &&buffer)
{
if(this == &buffer)
if (this == &buffer) {
return *this;
}
drop();
m_size = buffer.m_size;
if(m_size != 0)
{
if (m_size != 0) {
data = buffer.data;
buffer.data = nullptr;
buffer.m_size = 0;
}
else
} else {
data = nullptr;
}
return *this;
}

Expand All @@ -99,7 +98,7 @@ class Buffer
buffer.m_size = m_size;
if (m_size != 0) {
buffer.data = new T[m_size];
memcpy(buffer.data, data, m_size);
memcpy(buffer.data, data, sizeof(T) * m_size);
} else {
buffer.data = nullptr;
}
Expand All @@ -121,8 +120,9 @@ class Buffer

operator std::string_view() const
{
if (!data)
if (!data) {
return std::string_view();
}
return std::string_view(reinterpret_cast<char*>(data), m_size);
}

Expand Down Expand Up @@ -156,12 +156,14 @@ class SharedBuffer
SharedBuffer(unsigned int size)
{
m_size = size;
if(m_size != 0)
if (m_size != 0) {
data = new T[m_size];
else
} else {
data = nullptr;
}

refcount = new unsigned int;
memset(data,0,sizeof(T)*m_size);
memset(data, 0, sizeof(T) * m_size);
(*refcount) = 1;
}
SharedBuffer(const SharedBuffer &buffer)
Expand All @@ -173,45 +175,33 @@ class SharedBuffer
}
SharedBuffer & operator=(const SharedBuffer & buffer)
{
if(this == &buffer)
if (this == &buffer) {
return *this;
}

drop();
m_size = buffer.m_size;
data = buffer.data;
refcount = buffer.refcount;
(*refcount)++;
return *this;
}
/*
Copies whole buffer
*/
//! Copies whole buffer
SharedBuffer(const T *t, unsigned int size)
{
m_size = size;
if(m_size != 0)
{
if (m_size != 0) {
data = new T[m_size];
memcpy(data, t, m_size);
}
else
memcpy(data, t, sizeof(T) * m_size);
} else {
data = nullptr;
}
refcount = new unsigned int;
(*refcount) = 1;
}
/*
Copies whole buffer
*/
SharedBuffer(const Buffer<T> &buffer)
//! Copies whole buffer
SharedBuffer(const Buffer<T> &buffer) : SharedBuffer(*buffer, buffer.getSize())
{
m_size = buffer.getSize();
if (m_size != 0) {
data = new T[m_size];
memcpy(data, *buffer, buffer.getSize());
}
else
data = nullptr;
refcount = new unsigned int;
(*refcount) = 1;
}
~SharedBuffer()
{
Expand Down Expand Up @@ -239,8 +229,7 @@ class SharedBuffer
{
assert((*refcount) > 0);
(*refcount)--;
if(*refcount == 0)
{
if (*refcount == 0) {
delete[] data;
delete refcount;
}
Expand Down

0 comments on commit 7053348

Please sign in to comment.