From 303123976b6e91b1a1c108cc8b10c65a45b15608 Mon Sep 17 00:00:00 2001 From: cosinus Date: Sat, 11 Jan 2025 03:22:41 +0100 Subject: [PATCH] Fix bug memcpy call in a template. --- src/util/pointer.h | 48 +++++++++++++++++++++++++++------------------- 1 file changed, 28 insertions(+), 20 deletions(-) diff --git a/src/util/pointer.h b/src/util/pointer.h index 306ef55f97e9b..5943c9bb2b869 100644 --- a/src/util/pointer.h +++ b/src/util/pointer.h @@ -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 @@ -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; @@ -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; } @@ -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; } @@ -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) @@ -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; @@ -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; } @@ -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; }