From 9ce9d9b7fc9ceb55be54f5175849d4136923c4bc Mon Sep 17 00:00:00 2001 From: orignal Date: Wed, 8 Apr 2015 09:39:02 -0400 Subject: [PATCH] variable length buffer for LeaseSet --- LeaseSet.cpp | 13 +++++++++++-- LeaseSet.h | 9 ++++----- 2 files changed, 15 insertions(+), 7 deletions(-) diff --git a/LeaseSet.cpp b/LeaseSet.cpp index b9c2b132..3bc5d306 100644 --- a/LeaseSet.cpp +++ b/LeaseSet.cpp @@ -14,8 +14,9 @@ namespace i2p namespace data { - LeaseSet::LeaseSet (const uint8_t * buf, int len) + LeaseSet::LeaseSet (const uint8_t * buf, size_t len) { + m_Buffer = new uint8_t[len]; memcpy (m_Buffer, buf, len); m_BufferLen = len; ReadFromBuffer (); @@ -27,10 +28,12 @@ namespace data const i2p::data::LocalDestination * localDestination = pool.GetLocalDestination (); if (!localDestination) { + m_Buffer = nullptr; m_BufferLen = 0; LogPrint (eLogError, "Destination for local LeaseSet doesn't exist"); return; } + m_Buffer = new uint8_t[localDestination->GetIdentity ().GetFullLen ()]; m_BufferLen = localDestination->GetIdentity ().ToBuffer (m_Buffer, MAX_LS_BUFFER_SIZE); memcpy (m_Buffer + m_BufferLen, localDestination->GetEncryptionPublicKey (), 256); m_BufferLen += 256; @@ -62,9 +65,15 @@ namespace data ReadFromBuffer (); } - void LeaseSet::Update (const uint8_t * buf, int len) + void LeaseSet::Update (const uint8_t * buf, size_t len) { m_Leases.clear (); + if (len > m_BufferLen) + { + auto oldBuffer = m_Buffer; + m_Buffer = new uint8_t[len]; + delete[] oldBuffer; + } memcpy (m_Buffer, buf, len); m_BufferLen = len; ReadFromBuffer (); diff --git a/LeaseSet.h b/LeaseSet.h index eac5fc28..d1164449 100644 --- a/LeaseSet.h +++ b/LeaseSet.h @@ -36,11 +36,10 @@ namespace data { public: - LeaseSet (const uint8_t * buf, int len); - LeaseSet (const LeaseSet& ) = default; + LeaseSet (const uint8_t * buf, size_t len); LeaseSet (const i2p::tunnel::TunnelPool& pool); - LeaseSet& operator=(const LeaseSet& ) = default; - void Update (const uint8_t * buf, int len); + ~LeaseSet () { delete[] m_Buffer; }; + void Update (const uint8_t * buf, size_t len); const IdentityEx& GetIdentity () const { return m_Identity; }; const uint8_t * GetBuffer () const { return m_Buffer; }; @@ -64,7 +63,7 @@ namespace data std::vector m_Leases; IdentityEx m_Identity; uint8_t m_EncryptionKey[256]; - uint8_t m_Buffer[MAX_LS_BUFFER_SIZE]; + uint8_t * m_Buffer; size_t m_BufferLen; }; }