Skip to content

Commit 4e96f3a

Browse files
Fix qvi_bbuff assignment operator. (#272)
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]>
1 parent 963302b commit 4e96f3a

File tree

2 files changed

+20
-2
lines changed

2 files changed

+20
-2
lines changed

src/qvi-bbuff.cc

+18-2
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,25 @@
1717
#include "qvi-bbuff.h"
1818
#include "qvi-utils.h"
1919

20-
qvi_bbuff::qvi_bbuff(void)
20+
void
21+
qvi_bbuff::init(void)
2122
{
23+
// Make sure we get rid of any data that may be present.
24+
if (m_data) {
25+
free(m_data);
26+
m_data = nullptr;
27+
}
28+
m_size = 0;
2229
m_capacity = s_min_growth;
2330
m_data = calloc(m_capacity, sizeof(byte_t));
2431
if (qvi_unlikely(!m_data)) throw qvi_runtime_error();
2532
}
2633

34+
qvi_bbuff::qvi_bbuff(void)
35+
{
36+
init();
37+
}
38+
2739
qvi_bbuff::qvi_bbuff(
2840
const qvi_bbuff &src
2941
) : qvi_bbuff()
@@ -34,13 +46,17 @@ qvi_bbuff::qvi_bbuff(
3446

3547
qvi_bbuff::~qvi_bbuff(void)
3648
{
37-
if (m_data) free(m_data);
49+
if (m_data) {
50+
free(m_data);
51+
m_data = nullptr;
52+
}
3853
}
3954

4055
void
4156
qvi_bbuff::operator=(
4257
const qvi_bbuff &src
4358
) {
59+
init();
4460
const int rc = append(src.m_data, src.m_size);
4561
if (qvi_unlikely(rc != QV_SUCCESS)) throw qvi_runtime_error();
4662
}

src/qvi-bbuff.h

+2
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ struct qvi_bbuff {
3131
size_t m_size = 0;
3232
/** Pointer to data backing store. */
3333
void *m_data = nullptr;
34+
/** Initializes the instance. */
35+
void init(void);
3436
public:
3537
/** Constructor. */
3638
qvi_bbuff(void);

0 commit comments

Comments
 (0)