Skip to content

Commit

Permalink
Fix qvi_bbuff assignment operator. (#272)
Browse files Browse the repository at this point in the history
The assignment operator needs to make sure that the left-hand side
doesn't have data stored in it.

Signed-off-by: Samuel K. Gutierrez <[email protected]>
  • Loading branch information
samuelkgutierrez authored Aug 28, 2024
1 parent 963302b commit 4e96f3a
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
20 changes: 18 additions & 2 deletions src/qvi-bbuff.cc
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,25 @@
#include "qvi-bbuff.h"
#include "qvi-utils.h"

qvi_bbuff::qvi_bbuff(void)
void
qvi_bbuff::init(void)
{
// Make sure we get rid of any data that may be present.
if (m_data) {
free(m_data);
m_data = nullptr;
}
m_size = 0;
m_capacity = s_min_growth;
m_data = calloc(m_capacity, sizeof(byte_t));
if (qvi_unlikely(!m_data)) throw qvi_runtime_error();
}

qvi_bbuff::qvi_bbuff(void)
{
init();
}

qvi_bbuff::qvi_bbuff(
const qvi_bbuff &src
) : qvi_bbuff()
Expand All @@ -34,13 +46,17 @@ qvi_bbuff::qvi_bbuff(

qvi_bbuff::~qvi_bbuff(void)
{
if (m_data) free(m_data);
if (m_data) {
free(m_data);
m_data = nullptr;
}
}

void
qvi_bbuff::operator=(
const qvi_bbuff &src
) {
init();
const int rc = append(src.m_data, src.m_size);
if (qvi_unlikely(rc != QV_SUCCESS)) throw qvi_runtime_error();
}
Expand Down
2 changes: 2 additions & 0 deletions src/qvi-bbuff.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ struct qvi_bbuff {
size_t m_size = 0;
/** Pointer to data backing store. */
void *m_data = nullptr;
/** Initializes the instance. */
void init(void);
public:
/** Constructor. */
qvi_bbuff(void);
Expand Down

0 comments on commit 4e96f3a

Please sign in to comment.