Ref T730, replaced uint array by vector to avoid memory leak

* the heap array was not deleted
* using QVector<uint> now
* const correctness
This commit is contained in:
Klaus Basan
2019-09-29 01:01:28 +02:00
committed by Mat Sutcliffe
parent c3684a2f8c
commit 5f6aebebe8
2 changed files with 20 additions and 23 deletions

View File

@@ -16,28 +16,26 @@ namespace BlackCore
{ {
CCryptoDtoChannel::CCryptoDtoChannel(QString channelTag, const QByteArray &aeadReceiveKey, const QByteArray &aeadTransmitKey, int receiveSequenceHistorySize) CCryptoDtoChannel::CCryptoDtoChannel(QString channelTag, const QByteArray &aeadReceiveKey, const QByteArray &aeadTransmitKey, int receiveSequenceHistorySize)
{ {
m_channelTag = channelTag; m_channelTag = channelTag;
m_aeadReceiveKey = aeadReceiveKey; m_aeadReceiveKey = aeadReceiveKey;
m_aeadTransmitKey = aeadTransmitKey; m_aeadTransmitKey = aeadTransmitKey;
m_receiveSequenceSizeMaxSize = receiveSequenceHistorySize; m_receiveSequenceSizeMaxSize = receiveSequenceHistorySize;
if (m_receiveSequenceSizeMaxSize < 1) if (m_receiveSequenceSizeMaxSize < 1) { m_receiveSequenceSizeMaxSize = 1; }
m_receiveSequenceSizeMaxSize = 1; m_receiveSequenceHistory.fill(0, m_receiveSequenceSizeMaxSize);
m_receiveSequenceHistory = new uint[m_receiveSequenceSizeMaxSize];
m_receiveSequenceHistoryDepth = 0; m_receiveSequenceHistoryDepth = 0;
} }
CCryptoDtoChannel::CCryptoDtoChannel(CryptoDtoChannelConfigDto channelConfig, int receiveSequenceHistorySize) CCryptoDtoChannel::CCryptoDtoChannel(CryptoDtoChannelConfigDto channelConfig, int receiveSequenceHistorySize)
{ {
m_channelTag = channelConfig.channelTag; m_channelTag = channelConfig.channelTag;
m_aeadReceiveKey = channelConfig.aeadReceiveKey; m_aeadReceiveKey = channelConfig.aeadReceiveKey;
m_aeadTransmitKey = channelConfig.aeadTransmitKey; m_aeadTransmitKey = channelConfig.aeadTransmitKey;
m_hmacKey = channelConfig.hmacKey; m_hmacKey = channelConfig.hmacKey;
m_receiveSequenceSizeMaxSize = receiveSequenceHistorySize; m_receiveSequenceSizeMaxSize = receiveSequenceHistorySize;
if (m_receiveSequenceSizeMaxSize < 1) if (m_receiveSequenceSizeMaxSize < 1) { m_receiveSequenceSizeMaxSize = 1; }
m_receiveSequenceSizeMaxSize = 1; m_receiveSequenceHistory.fill(0, m_receiveSequenceSizeMaxSize);
m_receiveSequenceHistory = new uint[m_receiveSequenceSizeMaxSize];
m_receiveSequenceHistoryDepth = 0; m_receiveSequenceHistoryDepth = 0;
} }
@@ -113,20 +111,19 @@ namespace BlackCore
return true; return true;
} }
bool CCryptoDtoChannel::contains(uint sequence) bool CCryptoDtoChannel::contains(uint sequence) const
{ {
for (int i = 0; i < m_receiveSequenceHistoryDepth; i++) for (int i = 0; i < m_receiveSequenceHistoryDepth; i++)
{ {
if (m_receiveSequenceHistory[i] == sequence) if (m_receiveSequenceHistory[i] == sequence) { return true; }
return true;
} }
return false; return false;
} }
uint CCryptoDtoChannel::getMin(int &minIndex) uint CCryptoDtoChannel::getMin(int &minIndex) const
{ {
uint minValue = std::numeric_limits<uint>::max(); uint minValue = std::numeric_limits<uint>::max();
minIndex = -1; minIndex = -1;
int index = -1; int index = -1;
for (int i = 0; i < m_receiveSequenceHistoryDepth; i++) for (int i = 0; i < m_receiveSequenceHistoryDepth; i++)
@@ -140,7 +137,6 @@ namespace BlackCore
} }
return minValue; return minValue;
} }
} // ns } // ns
} // ns } // ns
} // ns } // ns

View File

@@ -16,6 +16,7 @@
#include <QDateTime> #include <QDateTime>
#include <QByteArray> #include <QByteArray>
#include <QVector>
#include <limits> #include <limits>
@@ -50,21 +51,21 @@ namespace BlackCore
bool checkReceivedSequence(uint sequenceReceived); bool checkReceivedSequence(uint sequenceReceived);
private: private:
bool contains(uint sequence); bool contains(uint sequence) const;
uint getMin(int &minIndex); uint getMin(int &minIndex) const;
QByteArray m_aeadTransmitKey; QByteArray m_aeadTransmitKey;
QByteArray m_aeadReceiveKey; QByteArray m_aeadReceiveKey;
uint m_transmitSequence = 0; uint m_transmitSequence = 0;
uint *m_receiveSequenceHistory; QVector<uint> m_receiveSequenceHistory;
int m_receiveSequenceHistoryDepth; int m_receiveSequenceHistoryDepth;
int m_receiveSequenceSizeMaxSize; int m_receiveSequenceSizeMaxSize;
QByteArray m_hmacKey; QByteArray m_hmacKey;
QString m_channelTag; QString m_channelTag;
QDateTime m_LastTransmitUtc; QDateTime m_LastTransmitUtc;
QDateTime m_lastReceiveUtc; QDateTime m_lastReceiveUtc;
}; };
} // ns } // ns
} // ns } // ns