OLD | NEW |
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 #include <limits> | 10 #include <limits> |
11 | 11 |
12 #include "base/bits.h" | 12 #include "base/bits.h" |
13 #include "base/macros.h" | 13 #include "base/macros.h" |
| 14 #include "base/numerics/safe_conversions.h" |
14 #include "build/build_config.h" | 15 #include "build/build_config.h" |
15 | 16 |
16 namespace base { | 17 namespace base { |
17 | 18 |
18 // static | 19 // static |
19 const int Pickle::kPayloadUnit = 64; | 20 const int Pickle::kPayloadUnit = 64; |
20 | 21 |
21 static const size_t kCapacityReadOnly = static_cast<size_t>(-1); | 22 static const size_t kCapacityReadOnly = static_cast<size_t>(-1); |
22 | 23 |
23 PickleIterator::PickleIterator(const Pickle& pickle) | 24 PickleIterator::PickleIterator(const Pickle& pickle) |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
82 | 83 |
83 bool PickleIterator::ReadBool(bool* result) { | 84 bool PickleIterator::ReadBool(bool* result) { |
84 return ReadBuiltinType(result); | 85 return ReadBuiltinType(result); |
85 } | 86 } |
86 | 87 |
87 bool PickleIterator::ReadInt(int* result) { | 88 bool PickleIterator::ReadInt(int* result) { |
88 return ReadBuiltinType(result); | 89 return ReadBuiltinType(result); |
89 } | 90 } |
90 | 91 |
91 bool PickleIterator::ReadLong(long* result) { | 92 bool PickleIterator::ReadLong(long* result) { |
92 return ReadBuiltinType(result); | 93 // Always read long as a 64-bit value to ensure compatibility between 32-bit |
| 94 // and 64-bit processes. |
| 95 int64_t result_int64 = 0; |
| 96 if (!ReadBuiltinType(&result_int64)) |
| 97 return false; |
| 98 // CHECK if the cast truncates the value so that we know to change this IPC |
| 99 // parameter to use int64_t. |
| 100 *result = base::checked_cast<long>(result_int64); |
| 101 return true; |
93 } | 102 } |
94 | 103 |
95 bool PickleIterator::ReadUInt16(uint16_t* result) { | 104 bool PickleIterator::ReadUInt16(uint16_t* result) { |
96 return ReadBuiltinType(result); | 105 return ReadBuiltinType(result); |
97 } | 106 } |
98 | 107 |
99 bool PickleIterator::ReadUInt32(uint32_t* result) { | 108 bool PickleIterator::ReadUInt32(uint32_t* result) { |
100 return ReadBuiltinType(result); | 109 return ReadBuiltinType(result); |
101 } | 110 } |
102 | 111 |
103 bool PickleIterator::ReadInt64(int64_t* result) { | 112 bool PickleIterator::ReadInt64(int64_t* result) { |
104 return ReadBuiltinType(result); | 113 return ReadBuiltinType(result); |
105 } | 114 } |
106 | 115 |
107 bool PickleIterator::ReadUInt64(uint64_t* result) { | 116 bool PickleIterator::ReadUInt64(uint64_t* result) { |
108 return ReadBuiltinType(result); | 117 return ReadBuiltinType(result); |
109 } | 118 } |
110 | 119 |
111 bool PickleIterator::ReadSizeT(size_t* result) { | |
112 // Always read size_t as a 64-bit value to ensure compatibility between 32-bit | |
113 // and 64-bit processes. | |
114 uint64_t result_uint64 = 0; | |
115 bool success = ReadBuiltinType(&result_uint64); | |
116 *result = static_cast<size_t>(result_uint64); | |
117 // Fail if the cast above truncates the value. | |
118 return success && (*result == result_uint64); | |
119 } | |
120 | |
121 bool PickleIterator::ReadFloat(float* result) { | 120 bool PickleIterator::ReadFloat(float* result) { |
122 // crbug.com/315213 | 121 // crbug.com/315213 |
123 // The source data may not be properly aligned, and unaligned float reads | 122 // The source data may not be properly aligned, and unaligned float reads |
124 // cause SIGBUS on some ARM platforms, so force using memcpy to copy the data | 123 // cause SIGBUS on some ARM platforms, so force using memcpy to copy the data |
125 // into the result. | 124 // into the result. |
126 const char* read_from = GetReadPointerAndAdvance<float>(); | 125 const char* read_from = GetReadPointerAndAdvance<float>(); |
127 if (!read_from) | 126 if (!read_from) |
128 return false; | 127 return false; |
129 memcpy(result, read_from, sizeof(*result)); | 128 memcpy(result, read_from, sizeof(*result)); |
130 return true; | 129 return true; |
(...skipping 338 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
469 | 468 |
470 inline void Pickle::WriteBytesCommon(const void* data, size_t length) { | 469 inline void Pickle::WriteBytesCommon(const void* data, size_t length) { |
471 DCHECK_NE(kCapacityReadOnly, capacity_after_header_) | 470 DCHECK_NE(kCapacityReadOnly, capacity_after_header_) |
472 << "oops: pickle is readonly"; | 471 << "oops: pickle is readonly"; |
473 MSAN_CHECK_MEM_IS_INITIALIZED(data, length); | 472 MSAN_CHECK_MEM_IS_INITIALIZED(data, length); |
474 void* write = ClaimUninitializedBytesInternal(length); | 473 void* write = ClaimUninitializedBytesInternal(length); |
475 memcpy(write, data, length); | 474 memcpy(write, data, length); |
476 } | 475 } |
477 | 476 |
478 } // namespace base | 477 } // namespace base |
OLD | NEW |