From 3887a0e91e021305b062e2ed280e05fee2a9f846 Mon Sep 17 00:00:00 2001 From: Zack Rusin Date: Sat, 27 Aug 2011 18:53:28 -0400 Subject: [PATCH] Make sure that the snappyfile doesn't try to read past the end of file. --- trace_snappyfile.cpp | 17 +++++++++++------ trace_snappyfile.hpp | 4 ++++ 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/trace_snappyfile.cpp b/trace_snappyfile.cpp index a348183..ad0a577 100644 --- a/trace_snappyfile.cpp +++ b/trace_snappyfile.cpp @@ -135,16 +135,13 @@ bool SnappyFile::rawWrite(const void *buffer, int length) bool SnappyFile::rawRead(void *buffer, int length) { - if (m_stream.eof()) { + if (endOfData()) { return false; } - if (freeCacheSize() > length) { - memcpy(buffer, m_cachePtr, length); - m_cachePtr += length; - } else if (freeCacheSize() == length) { + + if (freeCacheSize() >= length) { memcpy(buffer, m_cachePtr, length); m_cachePtr += length; - flushCache(); } else { int sizeToRead = length; int offset = 0; @@ -205,6 +202,14 @@ void SnappyFile::flushCache() size_t compressedLength; compressedLength = readCompressedLength(); m_stream.read((char*)m_compressedCache, compressedLength); + /* + * The reason we peek here is because the last read will + * read all the way until the last character, but that will not + * trigger m_stream.eof() to be set, so by calling peek + * we assure that if we in fact have read the entire stream + * then the m_stream.eof() is always set. + */ + m_stream.peek(); ::snappy::GetUncompressedLength(m_compressedCache, compressedLength, &m_cacheSize); if (m_cache) diff --git a/trace_snappyfile.hpp b/trace_snappyfile.hpp index 7fab835..c8c415b 100644 --- a/trace_snappyfile.hpp +++ b/trace_snappyfile.hpp @@ -68,6 +68,10 @@ private: else return 0; } + inline bool endOfData() const + { + return m_stream.eof() && freeCacheSize() == 0; + } void flushCache(); void createCache(size_t size); void writeCompressedLength(uint32_t num); -- 2.43.0