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

Side by Side Diff: base/pickle.cc

Issue 1619363002: Add compile time checks against longs being used in IPC structs on 32 bit Android. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: one more per Dmitry Created 4 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
« no previous file with comments | « base/pickle.h ('k') | base/pickle_unittest.cc » ('j') | 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 #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
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);
Tom Sepez 2016/01/27 18:23:20 Maybe we return FALSE if the value is too big to f
jam 2016/01/27 18:42:55 good question: i wanted to avoid this because it l
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) { 120 bool PickleIterator::ReadSizeT(size_t* result) {
112 // Always read size_t as a 64-bit value to ensure compatibility between 32-bit 121 // Always read size_t as a 64-bit value to ensure compatibility between 32-bit
113 // and 64-bit processes. 122 // and 64-bit processes.
114 uint64_t result_uint64 = 0; 123 uint64_t result_uint64 = 0;
115 bool success = ReadBuiltinType(&result_uint64); 124 if (!ReadBuiltinType(&result_uint64))
116 *result = static_cast<size_t>(result_uint64); 125 return false;
117 // Fail if the cast above truncates the value. 126 // CHECK if the cast truncates the value so that we know to change this IPC
118 return success && (*result == result_uint64); 127 // parameter to use uint64_t.
Tom Sepez 2016/01/27 18:23:20 Same here.
128 *result = base::checked_cast<size_t>(result_uint64);
129 return true;
119 } 130 }
120 131
121 bool PickleIterator::ReadFloat(float* result) { 132 bool PickleIterator::ReadFloat(float* result) {
122 // crbug.com/315213 133 // crbug.com/315213
123 // The source data may not be properly aligned, and unaligned float reads 134 // 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 135 // cause SIGBUS on some ARM platforms, so force using memcpy to copy the data
125 // into the result. 136 // into the result.
126 const char* read_from = GetReadPointerAndAdvance<float>(); 137 const char* read_from = GetReadPointerAndAdvance<float>();
127 if (!read_from) 138 if (!read_from)
128 return false; 139 return false;
(...skipping 290 matching lines...) Expand 10 before | Expand all | Expand 10 after
419 430
420 inline void Pickle::WriteBytesCommon(const void* data, size_t length) { 431 inline void Pickle::WriteBytesCommon(const void* data, size_t length) {
421 DCHECK_NE(kCapacityReadOnly, capacity_after_header_) 432 DCHECK_NE(kCapacityReadOnly, capacity_after_header_)
422 << "oops: pickle is readonly"; 433 << "oops: pickle is readonly";
423 MSAN_CHECK_MEM_IS_INITIALIZED(data, length); 434 MSAN_CHECK_MEM_IS_INITIALIZED(data, length);
424 void* write = ClaimUninitializedBytesInternal(length); 435 void* write = ClaimUninitializedBytesInternal(length);
425 memcpy(write, data, length); 436 memcpy(write, data, length);
426 } 437 }
427 438
428 } // namespace base 439 } // namespace base
OLDNEW
« no previous file with comments | « base/pickle.h ('k') | base/pickle_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698