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

Side by Side Diff: util/mac/checked_mach_address_range.cc

Issue 513453002: Add CheckedMachAddressRange and its test (Closed) Base URL: https://chromium.googlesource.com/crashpad/crashpad@master
Patch Set: Created 6 years, 3 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
(Empty)
1 // Copyright 2014 The Crashpad Authors. All rights reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 #include "util/mac/checked_mach_address_range.h"
16
17 #include "base/logging.h"
18 #include "base/numerics/safe_conversions.h"
19
20 namespace crashpad {
21
22 CheckedMachAddressRange::CheckedMachAddressRange()
23 : range_32_(0, 0), is_64_bit_(false), range_ok_(true) {
24 }
25
26 CheckedMachAddressRange::CheckedMachAddressRange(ProcessReader* process_reader,
27 mach_vm_address_t base,
28 mach_vm_size_t size) {
29 SetRange(process_reader, base, size);
30 }
31
32 void CheckedMachAddressRange::SetRange(ProcessReader* process_reader,
33 mach_vm_address_t base,
34 mach_vm_size_t size) {
35 is_64_bit_ = process_reader->Is64Bit();
36 if (is_64_bit_) {
37 range_64_.SetRange(base, size);
38 range_ok_ = true;
39 } else {
40 range_32_.SetRange(base, size);
41 range_ok_ = base::IsValueInRangeForNumericType<uint32_t>(base) &&
42 base::IsValueInRangeForNumericType<uint32_t>(size);
43 }
44 }
45
46 mach_vm_address_t CheckedMachAddressRange::Base() const {
47 return is_64_bit_ ? range_64_.base() : range_32_.base();
48 }
49
50 mach_vm_size_t CheckedMachAddressRange::Size() const {
51 return is_64_bit_ ? range_64_.size() : range_32_.size();
52 }
53
54 mach_vm_address_t CheckedMachAddressRange::End() const {
55 return is_64_bit_ ? range_64_.end() : range_32_.end();
56 }
57
58 bool CheckedMachAddressRange::IsValid() const {
59 return range_ok_ && (is_64_bit_ ? range_64_.IsValid() : range_32_.IsValid());
60 }
61
62 bool CheckedMachAddressRange::ContainsValue(mach_vm_address_t value) const {
63 DCHECK(range_ok_);
64
65 if (is_64_bit_) {
66 return range_64_.ContainsValue(value);
67 }
68
69 if (!base::IsValueInRangeForNumericType<uint32_t>(value)) {
70 return false;
71 }
72
73 return range_32_.ContainsValue(value);
74 }
75
76 bool CheckedMachAddressRange::ContainsRange(
77 const CheckedMachAddressRange& that) const {
78 DCHECK_EQ(is_64_bit_, that.is_64_bit_);
79 DCHECK(range_ok_);
80 DCHECK(that.range_ok_);
81
82 return is_64_bit_ ? range_64_.ContainsRange(that.range_64_)
83 : range_32_.ContainsRange(that.range_32_);
84 }
85
86 } // namespace crashpad
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698