Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "mojo/public/cpp/bindings/lib/bounds_checker.h" | 5 #include "mojo/public/cpp/bindings/lib/bounds_checker.h" |
| 6 | 6 |
| 7 #include <assert.h> | |
| 8 | |
| 9 #include "mojo/public/cpp/bindings/lib/bindings_serialization.h" | 7 #include "mojo/public/cpp/bindings/lib/bindings_serialization.h" |
| 10 #include "mojo/public/cpp/system/core.h" | 8 #include "mojo/public/cpp/environment/logging.h" |
| 9 #include "mojo/public/cpp/system/handle.h" | |
| 11 | 10 |
| 12 namespace mojo { | 11 namespace mojo { |
| 13 namespace internal { | 12 namespace internal { |
| 14 | 13 |
| 15 BoundsChecker::BoundsChecker(const void* data, uint32_t data_num_bytes, | 14 BoundsChecker::BoundsChecker(const void* data, uint32_t data_num_bytes, |
| 16 size_t num_handles) | 15 size_t num_handles) |
| 17 : data_begin_(reinterpret_cast<uintptr_t>(data)), | 16 : data_begin_(reinterpret_cast<uintptr_t>(data)), |
| 18 data_end_(data_begin_ + data_num_bytes), | 17 data_end_(data_begin_ + data_num_bytes), |
| 19 handle_begin_(0), | 18 handle_begin_(0), |
| 20 handle_end_(static_cast<uint32_t>(num_handles)) { | 19 handle_end_(static_cast<uint32_t>(num_handles)) { |
| 21 if (data_end_ < data_begin_) { | 20 if (data_end_ < data_begin_) { |
| 22 // The calculation of |data_end_| overflowed. | 21 // The calculation of |data_end_| overflowed. |
| 23 // It shouldn't happen but if it does, set the range to empty so | 22 // It shouldn't happen but if it does, set the range to empty so |
| 24 // IsValidRange() and ClaimMemory() always fail. | 23 // IsValidRange() and ClaimMemory() always fail. |
| 25 assert(false); // Not reached. | 24 MOJO_DCHECK(false) << "Not reached"; |
|
yzshen1
2014/07/18 20:56:45
It seems nice to have a NOTREACHED macro.
| |
| 26 data_end_ = data_begin_; | 25 data_end_ = data_begin_; |
| 27 } | 26 } |
| 28 if (handle_end_ < num_handles) { | 27 if (handle_end_ < num_handles) { |
| 29 // Assigning |num_handles| to |handle_end_| overflowed. | 28 // Assigning |num_handles| to |handle_end_| overflowed. |
| 30 // It shouldn't happen but if it does, set the handle index range to empty. | 29 // It shouldn't happen but if it does, set the handle index range to empty. |
| 31 assert(false); // Not reached. | 30 MOJO_DCHECK(false) << "Not reached"; |
| 32 handle_end_ = 0; | 31 handle_end_ = 0; |
| 33 } | 32 } |
| 34 } | 33 } |
| 35 | 34 |
| 36 BoundsChecker::~BoundsChecker() { | 35 BoundsChecker::~BoundsChecker() { |
| 37 } | 36 } |
| 38 | 37 |
| 39 bool BoundsChecker::ClaimMemory(const void* position, uint32_t num_bytes) { | 38 bool BoundsChecker::ClaimMemory(const void* position, uint32_t num_bytes) { |
| 40 uintptr_t begin = reinterpret_cast<uintptr_t>(position); | 39 uintptr_t begin = reinterpret_cast<uintptr_t>(position); |
| 41 uintptr_t end = begin + num_bytes; | 40 uintptr_t end = begin + num_bytes; |
| (...skipping 26 matching lines...) Expand all Loading... | |
| 68 | 67 |
| 69 return InternalIsValidRange(begin, end); | 68 return InternalIsValidRange(begin, end); |
| 70 } | 69 } |
| 71 | 70 |
| 72 bool BoundsChecker::InternalIsValidRange(uintptr_t begin, uintptr_t end) const { | 71 bool BoundsChecker::InternalIsValidRange(uintptr_t begin, uintptr_t end) const { |
| 73 return end > begin && begin >= data_begin_ && end <= data_end_; | 72 return end > begin && begin >= data_begin_ && end <= data_end_; |
| 74 } | 73 } |
| 75 | 74 |
| 76 } // namespace internal | 75 } // namespace internal |
| 77 } // namespace mojo | 76 } // namespace mojo |
| OLD | NEW |