From 4e96f3a8e983555e69fbaf7c654d5c81c0a58fa1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Samuel=20K=2E=20Guti=C3=A9rrez?= Date: Wed, 28 Aug 2024 11:04:14 -0600 Subject: [PATCH] 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 --- src/qvi-bbuff.cc | 20 ++++++++++++++++++-- src/qvi-bbuff.h | 2 ++ 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/src/qvi-bbuff.cc b/src/qvi-bbuff.cc index b50bf65..39104e2 100644 --- a/src/qvi-bbuff.cc +++ b/src/qvi-bbuff.cc @@ -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() @@ -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(); } diff --git a/src/qvi-bbuff.h b/src/qvi-bbuff.h index 6f3b3e1..ffe103b 100644 --- a/src/qvi-bbuff.h +++ b/src/qvi-bbuff.h @@ -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);