Skip to content

Commit

Permalink
Fix bug memcpy call in a template.
Browse files Browse the repository at this point in the history
  • Loading branch information
cosin15 committed Jan 12, 2025
1 parent d15214a commit 3031239
Showing 1 changed file with 28 additions and 20 deletions.
48 changes: 28 additions & 20 deletions src/util/pointer.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,12 @@ 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 @@ -62,10 +64,9 @@ class 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);
memcpy(data, t, sizeof(T) * size);
}
else
data = nullptr;
Expand All @@ -82,14 +83,14 @@ class 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 +100,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 Down Expand Up @@ -156,12 +157,15 @@ 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,8 +177,10 @@ class SharedBuffer
}
SharedBuffer & operator=(const SharedBuffer & buffer)
{
if(this == &buffer)
if(this == &buffer) {
return *this;
}

drop();
m_size = buffer.m_size;
data = buffer.data;
Expand All @@ -188,13 +194,13 @@ class SharedBuffer
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);
memcpy(data, t, sizeof(T) * m_size);
}
else
else {
data = nullptr;
}
refcount = new unsigned int;
(*refcount) = 1;
}
Expand All @@ -205,11 +211,13 @@ class SharedBuffer
{
m_size = buffer.getSize();
if (m_size != 0) {
data = new T[m_size];
memcpy(data, *buffer, buffer.getSize());
data = new T[m_size];
memcpy(data, *buffer, sizeof(T) * m_size);
}
else
else {
data = nullptr;
}

refcount = new unsigned int;
(*refcount) = 1;
}
Expand Down

0 comments on commit 3031239

Please sign in to comment.