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

Side by Side Diff: base/pickle.cc

Issue 290173008: Refactor PickleIterator. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: Created 6 years, 6 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 | Annotate | Revision Log
« no previous file with comments | « base/pickle.h ('k') | base/pickle_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "base/pickle.h" 5 #include "base/pickle.h"
6 6
7 #include <stdlib.h> 7 #include <stdlib.h>
8 8
9 #include <algorithm> // for max() 9 #include <algorithm> // for max()
10 10
11 //------------------------------------------------------------------------------ 11 //------------------------------------------------------------------------------
12 12
13 using base::char16; 13 using base::char16;
14 using base::string16; 14 using base::string16;
15 15
16 // static 16 // static
17 const int Pickle::kPayloadUnit = 64; 17 const int Pickle::kPayloadUnit = 64;
18 18
19 static const size_t kCapacityReadOnly = static_cast<size_t>(-1); 19 static const size_t kCapacityReadOnly = static_cast<size_t>(-1);
20 20
21 PickleIterator::PickleIterator(const Pickle& pickle) 21 PickleIterator::PickleIterator(const Pickle& pickle)
22 : read_ptr_(pickle.payload()), 22 : payload_(pickle.payload()),
23 read_end_ptr_(pickle.end_of_payload()) { 23 read_index_(0),
24 end_index_(pickle.payload_size()) {
24 } 25 }
25 26
26 template <typename Type> 27 template <typename Type>
27 inline bool PickleIterator::ReadBuiltinType(Type* result) { 28 inline bool PickleIterator::ReadBuiltinType(Type* result) {
28 const char* read_from = GetReadPointerAndAdvance<Type>(); 29 const char* read_from = GetReadPointerAndAdvance<Type>();
29 if (!read_from) 30 if (!read_from)
30 return false; 31 return false;
31 if (sizeof(Type) > sizeof(uint32)) 32 if (sizeof(Type) > sizeof(uint32))
32 memcpy(result, read_from, sizeof(*result)); 33 memcpy(result, read_from, sizeof(*result));
33 else 34 else
34 *result = *reinterpret_cast<const Type*>(read_from); 35 *result = *reinterpret_cast<const Type*>(read_from);
35 return true; 36 return true;
36 } 37 }
37 38
39 inline void PickleIterator::Advance(size_t size) {
40 size_t aligned_size = AlignInt(size, sizeof(uint32_t));
41 if (end_index_ - read_index_ < aligned_size) {
42 read_index_ = end_index_;
43 } else {
44 read_index_ += aligned_size;
45 }
46 }
47
38 template<typename Type> 48 template<typename Type>
39 inline const char* PickleIterator::GetReadPointerAndAdvance() { 49 inline const char* PickleIterator::GetReadPointerAndAdvance() {
40 const char* current_read_ptr = read_ptr_; 50 if (sizeof(Type) > end_index_ - read_index_) {
41 if (read_ptr_ + sizeof(Type) > read_end_ptr_) 51 read_index_ = end_index_;
42 return NULL; 52 return NULL;
43 if (sizeof(Type) < sizeof(uint32)) 53 }
44 read_ptr_ += AlignInt(sizeof(Type), sizeof(uint32)); 54 const char* current_read_ptr = payload_ + read_index_;
45 else 55 Advance(sizeof(Type));
cpu_(ooo_6.6-7.5) 2014/05/29 17:14:58 the two versions do not seem equivalent. Maybe I a
halyavin 2014/05/29 17:20:26 It is intentional. The previous code was wrong.
46 read_ptr_ += sizeof(Type);
47 return current_read_ptr; 56 return current_read_ptr;
48 } 57 }
49 58
50 const char* PickleIterator::GetReadPointerAndAdvance(int num_bytes) { 59 const char* PickleIterator::GetReadPointerAndAdvance(int num_bytes) {
51 if (num_bytes < 0 || read_end_ptr_ - read_ptr_ < num_bytes) 60 if (num_bytes < 0 ||
61 end_index_ - read_index_ < static_cast<size_t>(num_bytes)) {
62 read_index_ = end_index_;
52 return NULL; 63 return NULL;
53 const char* current_read_ptr = read_ptr_; 64 }
54 read_ptr_ += AlignInt(num_bytes, sizeof(uint32)); 65 const char* current_read_ptr = payload_ + read_index_;
66 Advance(num_bytes);
55 return current_read_ptr; 67 return current_read_ptr;
56 } 68 }
57 69
58 inline const char* PickleIterator::GetReadPointerAndAdvance(int num_elements, 70 inline const char* PickleIterator::GetReadPointerAndAdvance(
jschuh 2014/05/30 13:50:31 Nit: Not security related, but does the explicit i
halyavin 2014/05/30 15:20:24 I removed inline from *.h file. I leave it here so
59 size_t size_element) { 71 int num_elements,
72 size_t size_element) {
60 // Check for int32 overflow. 73 // Check for int32 overflow.
61 int64 num_bytes = static_cast<int64>(num_elements) * size_element; 74 int64 num_bytes = static_cast<int64>(num_elements) * size_element;
62 int num_bytes32 = static_cast<int>(num_bytes); 75 int num_bytes32 = static_cast<int>(num_bytes);
63 if (num_bytes != static_cast<int64>(num_bytes32)) 76 if (num_bytes != static_cast<int64>(num_bytes32))
64 return NULL; 77 return NULL;
65 return GetReadPointerAndAdvance(num_bytes32); 78 return GetReadPointerAndAdvance(num_bytes32);
66 } 79 }
67 80
68 bool PickleIterator::ReadBool(bool* result) { 81 bool PickleIterator::ReadBool(bool* result) {
69 return ReadBuiltinType(result); 82 return ReadBuiltinType(result);
(...skipping 255 matching lines...) Expand 10 before | Expand all | Expand 10 after
325 #endif 338 #endif
326 DCHECK_LE(write_offset_, kuint32max - data_len); 339 DCHECK_LE(write_offset_, kuint32max - data_len);
327 size_t new_size = write_offset_ + data_len; 340 size_t new_size = write_offset_ + data_len;
328 if (new_size > capacity_after_header_) { 341 if (new_size > capacity_after_header_) {
329 Resize(std::max(capacity_after_header_ * 2, new_size)); 342 Resize(std::max(capacity_after_header_ * 2, new_size));
330 } 343 }
331 344
332 char* write = mutable_payload() + write_offset_; 345 char* write = mutable_payload() + write_offset_;
333 memcpy(write, data, length); 346 memcpy(write, data, length);
334 memset(write + length, 0, data_len - length); 347 memset(write + length, 0, data_len - length);
335 header_->payload_size = static_cast<uint32>(write_offset_ + length); 348 header_->payload_size = static_cast<uint32>(new_size);
336 write_offset_ = new_size; 349 write_offset_ = new_size;
337 } 350 }
OLDNEW
« no previous file with comments | « base/pickle.h ('k') | base/pickle_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698