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

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, 10 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 | « 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 size_t s;
jar (doing other things) 2013/03/04 19:25:51 nit: style guide suggests avoiding single characte
39 if (sizeof(Type) < sizeof(uint32))
40 s = AlignInt(sizeof(Type), sizeof(uint32));
jar (doing other things) 2013/03/04 19:25:51 Am I correct in reading AlignInt(smaller, larger)
41 else
42 s = sizeof(Type);
43 if (s > static_cast<size_t>(read_end_ptr_ - read_ptr_))
39 return NULL; 44 return NULL;
40 if (sizeof(Type) < sizeof(uint32)) 45 read_ptr_ += s;
41 read_ptr_ += AlignInt(sizeof(Type), sizeof(uint32));
42 else
43 read_ptr_ += sizeof(Type);
44 return current_read_ptr; 46 return current_read_ptr;
45 } 47 }
46 48
47 const char* PickleIterator::GetReadPointerAndAdvance(int num_bytes) { 49 const char* PickleIterator::GetReadPointerAndAdvance(int num_bytes) {
48 if (num_bytes < 0 || read_end_ptr_ - read_ptr_ < num_bytes) 50 if (num_bytes < 0 || read_end_ptr_ - read_ptr_ < num_bytes)
49 return NULL; 51 return NULL;
50 const char* current_read_ptr = read_ptr_; 52 const char* current_read_ptr = read_ptr_;
51 read_ptr_ += AlignInt(num_bytes, sizeof(uint32)); 53 read_ptr_ += AlignInt(num_bytes, sizeof(uint32));
52 return current_read_ptr; 54 return current_read_ptr;
53 } 55 }
(...skipping 292 matching lines...) Expand 10 before | Expand all | Expand 10 after
346 const char* start, 348 const char* start,
347 const char* end) { 349 const char* end) {
348 DCHECK_EQ(header_size, AlignInt(header_size, sizeof(uint32))); 350 DCHECK_EQ(header_size, AlignInt(header_size, sizeof(uint32)));
349 DCHECK_LE(header_size, static_cast<size_t>(kPayloadUnit)); 351 DCHECK_LE(header_size, static_cast<size_t>(kPayloadUnit));
350 352
351 if (static_cast<size_t>(end - start) < sizeof(Header)) 353 if (static_cast<size_t>(end - start) < sizeof(Header))
352 return NULL; 354 return NULL;
353 355
354 const Header* hdr = reinterpret_cast<const Header*>(start); 356 const Header* hdr = reinterpret_cast<const Header*>(start);
355 const char* payload_base = start + header_size; 357 const char* payload_base = start + header_size;
356 const char* payload_end = payload_base + hdr->payload_size; 358 if (hdr->payload_size > static_cast<size_t>(end - payload_base))
357 if (payload_end < payload_base)
358 return NULL; 359 return NULL;
359 360
360 return (payload_end > end) ? NULL : payload_end; 361 return payload_base + hdr->payload_size;
361 } 362 }
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