From 5f6aebebe8ca9027e512530d18fee9e7ec0a139a Mon Sep 17 00:00:00 2001 From: Klaus Basan Date: Sun, 29 Sep 2019 01:01:28 +0200 Subject: [PATCH] Ref T730, replaced uint array by vector to avoid memory leak * the heap array was not deleted * using QVector now * const correctness --- src/blackcore/afv/crypto/cryptodtochannel.cpp | 30 ++++++++----------- src/blackcore/afv/crypto/cryptodtochannel.h | 13 ++++---- 2 files changed, 20 insertions(+), 23 deletions(-) diff --git a/src/blackcore/afv/crypto/cryptodtochannel.cpp b/src/blackcore/afv/crypto/cryptodtochannel.cpp index 6c762bff3..4bed8d7d5 100644 --- a/src/blackcore/afv/crypto/cryptodtochannel.cpp +++ b/src/blackcore/afv/crypto/cryptodtochannel.cpp @@ -16,28 +16,26 @@ namespace BlackCore { CCryptoDtoChannel::CCryptoDtoChannel(QString channelTag, const QByteArray &aeadReceiveKey, const QByteArray &aeadTransmitKey, int receiveSequenceHistorySize) { - m_channelTag = channelTag; - m_aeadReceiveKey = aeadReceiveKey; + m_channelTag = channelTag; + m_aeadReceiveKey = aeadReceiveKey; m_aeadTransmitKey = aeadTransmitKey; m_receiveSequenceSizeMaxSize = receiveSequenceHistorySize; - if (m_receiveSequenceSizeMaxSize < 1) - m_receiveSequenceSizeMaxSize = 1; - m_receiveSequenceHistory = new uint[m_receiveSequenceSizeMaxSize]; + if (m_receiveSequenceSizeMaxSize < 1) { m_receiveSequenceSizeMaxSize = 1; } + m_receiveSequenceHistory.fill(0, m_receiveSequenceSizeMaxSize); m_receiveSequenceHistoryDepth = 0; } CCryptoDtoChannel::CCryptoDtoChannel(CryptoDtoChannelConfigDto channelConfig, int receiveSequenceHistorySize) { - m_channelTag = channelConfig.channelTag; - m_aeadReceiveKey = channelConfig.aeadReceiveKey; + m_channelTag = channelConfig.channelTag; + m_aeadReceiveKey = channelConfig.aeadReceiveKey; m_aeadTransmitKey = channelConfig.aeadTransmitKey; - m_hmacKey = channelConfig.hmacKey; + m_hmacKey = channelConfig.hmacKey; m_receiveSequenceSizeMaxSize = receiveSequenceHistorySize; - if (m_receiveSequenceSizeMaxSize < 1) - m_receiveSequenceSizeMaxSize = 1; - m_receiveSequenceHistory = new uint[m_receiveSequenceSizeMaxSize]; + if (m_receiveSequenceSizeMaxSize < 1) { m_receiveSequenceSizeMaxSize = 1; } + m_receiveSequenceHistory.fill(0, m_receiveSequenceSizeMaxSize); m_receiveSequenceHistoryDepth = 0; } @@ -113,20 +111,19 @@ namespace BlackCore return true; } - bool CCryptoDtoChannel::contains(uint sequence) + bool CCryptoDtoChannel::contains(uint sequence) const { for (int i = 0; i < m_receiveSequenceHistoryDepth; i++) { - if (m_receiveSequenceHistory[i] == sequence) - return true; + if (m_receiveSequenceHistory[i] == sequence) { return true; } } return false; } - uint CCryptoDtoChannel::getMin(int &minIndex) + uint CCryptoDtoChannel::getMin(int &minIndex) const { uint minValue = std::numeric_limits::max(); - minIndex = -1; + minIndex = -1; int index = -1; for (int i = 0; i < m_receiveSequenceHistoryDepth; i++) @@ -140,7 +137,6 @@ namespace BlackCore } return minValue; } - } // ns } // ns } // ns diff --git a/src/blackcore/afv/crypto/cryptodtochannel.h b/src/blackcore/afv/crypto/cryptodtochannel.h index f3bde5633..8606288e6 100644 --- a/src/blackcore/afv/crypto/cryptodtochannel.h +++ b/src/blackcore/afv/crypto/cryptodtochannel.h @@ -16,6 +16,7 @@ #include #include +#include #include @@ -50,21 +51,21 @@ namespace BlackCore bool checkReceivedSequence(uint sequenceReceived); private: - bool contains(uint sequence); - uint getMin(int &minIndex); + bool contains(uint sequence) const; + uint getMin(int &minIndex) const; QByteArray m_aeadTransmitKey; QByteArray m_aeadReceiveKey; uint m_transmitSequence = 0; - uint *m_receiveSequenceHistory; + QVector m_receiveSequenceHistory; int m_receiveSequenceHistoryDepth; int m_receiveSequenceSizeMaxSize; QByteArray m_hmacKey; - QString m_channelTag; - QDateTime m_LastTransmitUtc; - QDateTime m_lastReceiveUtc; + QString m_channelTag; + QDateTime m_LastTransmitUtc; + QDateTime m_lastReceiveUtc; }; } // ns } // ns