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

Side by Side 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 unified diff | Download patch
OLDNEW
1 /* 1 /*
2 * Copyright (C) 2015 Google Inc. All rights reserved. 2 * Copyright (C) 2015 Google Inc. All rights reserved.
3 * 3 *
4 * Redistribution and use in source and binary forms, with or without 4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are 5 * modification, are permitted provided that the following conditions are
6 * met: 6 * met:
7 * 7 *
8 * * Redistributions of source code must retain the above copyright 8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer. 9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above 10 * * Redistributions in binary form must reproduce the above
(...skipping 17 matching lines...) Expand all
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */ 29 */
30 30
31 #include "config.h" 31 #include "config.h"
32 #include "platform/image-decoders/FastSharedBufferReader.h" 32 #include "platform/image-decoders/FastSharedBufferReader.h"
33 33
34 namespace blink { 34 namespace blink {
35 35
36 FastSharedBufferReader::FastSharedBufferReader(PassRefPtr<SharedBuffer> data) 36 FastSharedBufferReader::FastSharedBufferReader(PassRefPtr<SharedBuffer> data)
37 : m_data(data) 37 : m_data(data)
38 , m_segment(0)
39 , m_segmentLength(0)
40 , m_dataPosition(0)
41 { 38 {
42 } 39 }
43 40
41 FastSharedBufferReader::Cache::Cache()
42 : segment(0)
43 , segmentLength(0)
44 , dataPosition(0)
45 {
46 }
47
44 const char* FastSharedBufferReader::getConsecutiveData(size_t dataPosition, size _t length, char* buffer) 48 const char* FastSharedBufferReader::getConsecutiveData(size_t dataPosition, size _t length, char* buffer)
45 { 49 {
46 RELEASE_ASSERT(dataPosition + length <= m_data->size()); 50 RELEASE_ASSERT(dataPosition + length <= m_data->size());
47 51
48 // Use the cached segment if it can serve the request. 52 // Use the cached segment if it can serve the request.
49 if (dataPosition >= m_dataPosition && dataPosition + length <= m_dataPositio n + m_segmentLength) 53 size_t cacheEndPosition = m_cache.dataPosition + m_cache.segmentLength;
50 return m_segment + dataPosition - m_dataPosition; 54 if (dataPosition >= m_cache.dataPosition && dataPosition + length <= cacheEn dPosition) {
55 size_t skip = dataPosition - m_cache.dataPosition;
56 return m_cache.segment + skip;
57 }
Peter Kasting 2015/03/25 19:53:11 Nit: The old code was more readable than this more
51 58
52 // Return a pointer into |m_data| if the request doesn't span segments. 59 // Return a pointer into |m_data| if the request doesn't span segments.
53 m_dataPosition = dataPosition; 60 getSomeDataIntoCache(dataPosition);
54 m_segmentLength = m_data->getSomeData(m_segment, m_dataPosition); 61 if (length <= m_cache.segmentLength)
55 ASSERT(m_segmentLength); 62 return m_cache.segment;
56 if (length <= m_segmentLength)
57 return m_segment;
58 63
59 for (char* tempBuffer = buffer; length;) { 64 for (const char* dest = buffer; ; ) {
60 size_t copy = std::min(length, m_segmentLength); 65 size_t copy = std::min(length, m_cache.segmentLength);
61 memcpy(tempBuffer, m_segment, copy); 66 memcpy(dest, m_cache.segment, copy);
62 m_dataPosition += copy;
63 length -= copy; 67 length -= copy;
64 tempBuffer += copy; 68 if (!length) {
69 // Done.
Peter Kasting 2015/03/25 19:53:11 Nit: This comment adds nothing, remove it.
70 return buffer;
71 }
65 72
66 m_segmentLength = m_data->getSomeData(m_segment, m_dataPosition); 73 // Continue reading the next segment.
67 ASSERT(m_segmentLength); 74 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.
75 dataPosition += copy;
Peter Kasting 2015/03/25 19:53:11 Nit: Can omit this line and just pass (m_dataPosit
76 getSomeDataIntoCache(dataPosition);
68 } 77 }
69 return buffer;
70 } 78 }
71 79
72 size_t FastSharedBufferReader::getSomeData(const char*& someData, size_t dataPos ition) 80 size_t FastSharedBufferReader::getSomeData(const char*& someData, size_t dataPos ition)
73 { 81 {
74 m_segmentLength = m_data->getSomeData(m_segment, dataPosition); 82 getSomeDataIntoCache(dataPosition);
75 someData = m_segment; 83 someData = m_cache.segment;
76 m_dataPosition = dataPosition; 84 return m_cache.segmentLength;
77 ASSERT(m_segmentLength); 85 }
78 return m_segmentLength; 86
87 void FastSharedBufferReader::getSomeDataIntoCache(unsigned dataPosition)
88 {
89 m_cache.dataPosition = dataPosition;
90 m_cache.segmentLength = m_data->getSomeData(m_cache.segment, dataPosition);
91 ASSERT(m_cache.segmentLength);
79 } 92 }
80 93
81 } // namespace blink 94 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698