Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(515)

Unified Diff: Source/platform/image-decoders/FastSharedBufferReader.cpp

Issue 1011113003: Fix potential bug in FastSharedBufferReader::getConsecutiveData (Closed) Base URL: https://chromium.googlesource.com/chromium/blink.git@master
Patch Set: fix my bug too... Created 5 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: Source/platform/image-decoders/FastSharedBufferReader.cpp
diff --git a/Source/platform/image-decoders/FastSharedBufferReader.cpp b/Source/platform/image-decoders/FastSharedBufferReader.cpp
index 4475960fdcb4a12dab70112a482d779630b558e4..ac026c76ac642c8afc0f1815db77e751bc0ee5b9 100644
--- a/Source/platform/image-decoders/FastSharedBufferReader.cpp
+++ b/Source/platform/image-decoders/FastSharedBufferReader.cpp
@@ -35,9 +35,13 @@ namespace blink {
FastSharedBufferReader::FastSharedBufferReader(PassRefPtr<SharedBuffer> data)
: m_data(data)
- , m_segment(0)
- , m_segmentLength(0)
- , m_dataPosition(0)
+{
+}
+
+FastSharedBufferReader::Cache::Cache()
+ : segment(0)
+ , segmentLength(0)
+ , dataPosition(0)
{
}
@@ -46,36 +50,45 @@ const char* FastSharedBufferReader::getConsecutiveData(size_t dataPosition, size
RELEASE_ASSERT(dataPosition + length <= m_data->size());
// Use the cached segment if it can serve the request.
- if (dataPosition >= m_dataPosition && dataPosition + length <= m_dataPosition + m_segmentLength)
- return m_segment + dataPosition - m_dataPosition;
+ size_t cacheEndPosition = m_cache.dataPosition + m_cache.segmentLength;
+ if (dataPosition >= m_cache.dataPosition && dataPosition + length <= cacheEndPosition) {
+ size_t skip = dataPosition - m_cache.dataPosition;
+ return m_cache.segment + skip;
+ }
Peter Kasting 2015/03/25 19:53:11 Nit: The old code was more readable than this more
// Return a pointer into |m_data| if the request doesn't span segments.
- m_dataPosition = dataPosition;
- m_segmentLength = m_data->getSomeData(m_segment, m_dataPosition);
- ASSERT(m_segmentLength);
- if (length <= m_segmentLength)
- return m_segment;
+ getSomeDataIntoCache(dataPosition);
+ if (length <= m_cache.segmentLength)
+ return m_cache.segment;
- for (char* tempBuffer = buffer; length;) {
- size_t copy = std::min(length, m_segmentLength);
- memcpy(tempBuffer, m_segment, copy);
- m_dataPosition += copy;
+ for (const char* dest = buffer; ; ) {
+ size_t copy = std::min(length, m_cache.segmentLength);
+ memcpy(dest, m_cache.segment, copy);
length -= copy;
- tempBuffer += copy;
+ if (!length) {
+ // Done.
Peter Kasting 2015/03/25 19:53:11 Nit: This comment adds nothing, remove it.
+ return buffer;
+ }
- m_segmentLength = m_data->getSomeData(m_segment, m_dataPosition);
- ASSERT(m_segmentLength);
+ // Continue reading the next segment.
+ dest += copy;
Peter Kasting 2015/03/25 19:53:11 Nit: Can place this into the for loop declaration.
kbalazs 2015/03/25 21:05:19 copy is local to the loop
Peter Kasting 2015/03/25 21:10:47 True. Ignore that.
+ dataPosition += copy;
Peter Kasting 2015/03/25 19:53:11 Nit: Can omit this line and just pass (m_dataPosit
+ getSomeDataIntoCache(dataPosition);
}
- return buffer;
}
size_t FastSharedBufferReader::getSomeData(const char*& someData, size_t dataPosition)
{
- m_segmentLength = m_data->getSomeData(m_segment, dataPosition);
- someData = m_segment;
- m_dataPosition = dataPosition;
- ASSERT(m_segmentLength);
- return m_segmentLength;
+ getSomeDataIntoCache(dataPosition);
+ someData = m_cache.segment;
+ return m_cache.segmentLength;
+}
+
+void FastSharedBufferReader::getSomeDataIntoCache(unsigned dataPosition)
+{
+ m_cache.dataPosition = dataPosition;
+ m_cache.segmentLength = m_data->getSomeData(m_cache.segment, dataPosition);
+ ASSERT(m_cache.segmentLength);
}
} // namespace blink

Powered by Google App Engine
This is Rietveld 408576698