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

Side by Side Diff: base/pickle.cc

Issue 12079010: Avoid undefined behavior when checking for pointer wraparound (Closed) Base URL: http://src.chromium.org/svn/trunk/src/
Patch Set: Created 7 years, 1 month 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 | « AUTHORS ('k') | no next file » | 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
(...skipping 17 matching lines...) Expand all
28 if (sizeof(Type) > sizeof(uint32)) 28 if (sizeof(Type) > sizeof(uint32))
29 memcpy(result, read_from, sizeof(*result)); 29 memcpy(result, read_from, sizeof(*result));
30 else 30 else
31 *result = *reinterpret_cast<const Type*>(read_from); 31 *result = *reinterpret_cast<const Type*>(read_from);
32 return true; 32 return true;
33 } 33 }
34 34
35 template<typename Type> 35 template<typename Type>
36 inline const char* PickleIterator::GetReadPointerAndAdvance() { 36 inline const char* PickleIterator::GetReadPointerAndAdvance() {
37 const char* current_read_ptr = read_ptr_; 37 const char* current_read_ptr = read_ptr_;
38 if (read_ptr_ + sizeof(Type) > read_end_ptr_) 38 if (sizeof(Type) > static_cast<size_t>(read_end_ptr_ - read_ptr_))
39 return NULL; 39 return NULL;
40 if (sizeof(Type) < sizeof(uint32)) 40 if (sizeof(Type) < sizeof(uint32))
41 read_ptr_ += AlignInt(sizeof(Type), sizeof(uint32)); 41 read_ptr_ += AlignInt(sizeof(Type), sizeof(uint32));
42 else 42 else
43 read_ptr_ += sizeof(Type); 43 read_ptr_ += sizeof(Type);
44 return current_read_ptr; 44 return current_read_ptr;
45 } 45 }
46 46
47 const char* PickleIterator::GetReadPointerAndAdvance(int num_bytes) { 47 const char* PickleIterator::GetReadPointerAndAdvance(int num_bytes) {
48 if (num_bytes < 0 || read_end_ptr_ - read_ptr_ < num_bytes) 48 if (num_bytes < 0 || read_end_ptr_ - read_ptr_ < num_bytes)
(...skipping 235 matching lines...) Expand 10 before | Expand all | Expand 10 after
284 const char* start, 284 const char* start,
285 const char* end) { 285 const char* end) {
286 DCHECK_EQ(header_size, AlignInt(header_size, sizeof(uint32))); 286 DCHECK_EQ(header_size, AlignInt(header_size, sizeof(uint32)));
287 DCHECK_LE(header_size, static_cast<size_t>(kPayloadUnit)); 287 DCHECK_LE(header_size, static_cast<size_t>(kPayloadUnit));
288 288
289 if (static_cast<size_t>(end - start) < sizeof(Header)) 289 if (static_cast<size_t>(end - start) < sizeof(Header))
290 return NULL; 290 return NULL;
291 291
292 const Header* hdr = reinterpret_cast<const Header*>(start); 292 const Header* hdr = reinterpret_cast<const Header*>(start);
293 const char* payload_base = start + header_size; 293 const char* payload_base = start + header_size;
294 const char* payload_end = payload_base + hdr->payload_size; 294 if (hdr->payload_size > static_cast<size_t>(end - payload_base))
295 if (payload_end < payload_base)
296 return NULL; 295 return NULL;
297 296
298 return (payload_end > end) ? NULL : payload_end; 297 return payload_base + hdr->payload_size;
299 } 298 }
300 299
301 template <size_t length> void Pickle::WriteBytesStatic(const void* data) { 300 template <size_t length> void Pickle::WriteBytesStatic(const void* data) {
302 WriteBytesCommon(data, length); 301 WriteBytesCommon(data, length);
303 } 302 }
304 303
305 template void Pickle::WriteBytesStatic<2>(const void* data); 304 template void Pickle::WriteBytesStatic<2>(const void* data);
306 template void Pickle::WriteBytesStatic<4>(const void* data); 305 template void Pickle::WriteBytesStatic<4>(const void* data);
307 template void Pickle::WriteBytesStatic<8>(const void* data); 306 template void Pickle::WriteBytesStatic<8>(const void* data);
308 307
(...skipping 10 matching lines...) Expand all
319 if (new_size > capacity_after_header_) { 318 if (new_size > capacity_after_header_) {
320 Resize(std::max(capacity_after_header_ * 2, new_size)); 319 Resize(std::max(capacity_after_header_ * 2, new_size));
321 } 320 }
322 321
323 char* write = mutable_payload() + write_offset_; 322 char* write = mutable_payload() + write_offset_;
324 memcpy(write, data, length); 323 memcpy(write, data, length);
325 memset(write + length, 0, data_len - length); 324 memset(write + length, 0, data_len - length);
326 header_->payload_size = static_cast<uint32>(write_offset_ + length); 325 header_->payload_size = static_cast<uint32>(write_offset_ + length);
327 write_offset_ = new_size; 326 write_offset_ = new_size;
328 } 327 }
OLDNEW
« no previous file with comments | « AUTHORS ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698