From 06708ea1c044215eee37a2703b6764cf2e89d53b Mon Sep 17 00:00:00 2001 From: Dan Engelbrecht Date: Sat, 13 Apr 2024 13:53:24 +0200 Subject: Validate input buffer size when trying to parse package message (#47) * add validation of input buffer size when trying to parse package message * avoid doing memcopy when parsing package message --- src/zencore/include/zencore/stream.h | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) (limited to 'src/zencore/include') diff --git a/src/zencore/include/zencore/stream.h b/src/zencore/include/zencore/stream.h index a28290041..fae393666 100644 --- a/src/zencore/include/zencore/stream.h +++ b/src/zencore/include/zencore/stream.h @@ -70,14 +70,25 @@ public: inline void Read(void* DataPtr, size_t ByteCount) { + ZEN_ASSERT(m_Offset + ByteCount <= m_BufferSize); memcpy(DataPtr, m_BufferBase + m_Offset, ByteCount); m_Offset += ByteCount; } + inline MemoryView GetView(size_t ByteCount) const + { + ZEN_ASSERT(m_Offset + ByteCount <= m_BufferSize); + return MemoryView((const void*)(m_BufferBase + m_Offset), (const void*)(m_BufferBase + m_Offset + ByteCount)); + } inline uint64_t Size() const { return m_BufferSize; } inline uint64_t GetSize() const { return Size(); } inline uint64_t CurrentOffset() const { return m_Offset; } - inline void Skip(size_t ByteCount) { m_Offset += ByteCount; }; + inline uint64_t Remaining() const { return m_BufferSize - m_Offset; } + inline void Skip(size_t ByteCount) + { + ZEN_ASSERT(m_Offset + ByteCount <= m_BufferSize); + m_Offset += ByteCount; + }; protected: const uint8_t* m_BufferBase; -- cgit v1.2.3