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

Side by Side Diff: mojo/public/cpp/bindings/lib/map_serialization.h

Issue 2036623002: Validate map keys in C++ Mojo bindings (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Address review comments Created 4 years, 6 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
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 #ifndef MOJO_PUBLIC_CPP_BINDINGS_LIB_MAP_SERIALIZATION_H_ 5 #ifndef MOJO_PUBLIC_CPP_BINDINGS_LIB_MAP_SERIALIZATION_H_
6 #define MOJO_PUBLIC_CPP_BINDINGS_LIB_MAP_SERIALIZATION_H_ 6 #define MOJO_PUBLIC_CPP_BINDINGS_LIB_MAP_SERIALIZATION_H_
7 7
8 #include <type_traits> 8 #include <type_traits>
9 9
10 #include "mojo/public/cpp/bindings/array.h" 10 #include "mojo/public/cpp/bindings/array.h"
(...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after
106 106
107 return struct_overhead + keys_size + values_size; 107 return struct_overhead + keys_size + values_size;
108 } 108 }
109 109
110 // We don't need an ArrayValidateParams instance for key validation since 110 // We don't need an ArrayValidateParams instance for key validation since
111 // we can deduce it from the Key type. (which can only be primitive types or 111 // we can deduce it from the Key type. (which can only be primitive types or
112 // non-nullable strings.) 112 // non-nullable strings.)
113 static void Serialize(MaybeConstUserType& input, 113 static void Serialize(MaybeConstUserType& input,
114 Buffer* buf, 114 Buffer* buf,
115 Data** output, 115 Data** output,
116 const ArrayValidateParams* value_validate_params, 116 const ArrayValidateParams* validate_params,
117 SerializationContext* context) { 117 SerializationContext* context) {
118 DCHECK(validate_params->key_validate_params);
119 DCHECK(validate_params->element_validate_params);
118 if (CallIsNullIfExists<Traits>(input)) { 120 if (CallIsNullIfExists<Traits>(input)) {
119 *output = nullptr; 121 *output = nullptr;
120 return; 122 return;
121 } 123 }
122 124
123 auto result = Data::New(buf); 125 auto result = Data::New(buf);
124 if (result) { 126 if (result) {
125 result->keys.ptr = Array<Key>::Data_::New(Traits::GetSize(input), buf); 127 result->keys.ptr = Array<Key>::Data_::New(Traits::GetSize(input), buf);
126 if (result->keys.ptr) { 128 if (result->keys.ptr) {
127 const ArrayValidateParams* key_validate_params =
128 MapKeyValidateParamsFactory<
129 typename GetDataTypeAsArrayElement<Key>::Data>::Get();
130 MapKeyReader<MaybeConstUserType> key_reader(input); 129 MapKeyReader<MaybeConstUserType> key_reader(input);
131 KeyArraySerializer::SerializeElements( 130 KeyArraySerializer::SerializeElements(
132 &key_reader, buf, result->keys.ptr, key_validate_params, context); 131 &key_reader, buf, result->keys.ptr,
132 validate_params->key_validate_params, context);
133 } 133 }
134 134
135 result->values.ptr = 135 result->values.ptr =
136 Array<Value>::Data_::New(Traits::GetSize(input), buf); 136 Array<Value>::Data_::New(Traits::GetSize(input), buf);
137 if (result->values.ptr) { 137 if (result->values.ptr) {
138 MapValueReader<MaybeConstUserType> value_reader(input); 138 MapValueReader<MaybeConstUserType> value_reader(input);
139 ValueArraySerializer::SerializeElements(&value_reader, buf, 139 ValueArraySerializer::SerializeElements(
140 result->values.ptr, 140 &value_reader, buf, result->values.ptr,
141 value_validate_params, context); 141 validate_params->element_validate_params, context);
142 } 142 }
143 } 143 }
144 *output = result; 144 *output = result;
145 } 145 }
146 146
147 static bool Deserialize(Data* input, 147 static bool Deserialize(Data* input,
148 UserType* output, 148 UserType* output,
149 SerializationContext* context) { 149 SerializationContext* context) {
150 if (!input) 150 if (!input)
151 return CallSetToNullIfExists<Traits>(output); 151 return CallSetToNullIfExists<Traits>(output);
(...skipping 15 matching lines...) Expand all
167 for (size_t i = 0; i < size; ++i) 167 for (size_t i = 0; i < size; ++i)
168 Traits::Insert(*output, std::move(keys[i]), std::move(values[i])); 168 Traits::Insert(*output, std::move(keys[i]), std::move(values[i]));
169 return true; 169 return true;
170 } 170 }
171 }; 171 };
172 172
173 } // namespace internal 173 } // namespace internal
174 } // namespace mojo 174 } // namespace mojo
175 175
176 #endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_MAP_SERIALIZATION_H_ 176 #endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_MAP_SERIALIZATION_H_
OLDNEW
« no previous file with comments | « mojo/public/cpp/bindings/lib/map_data_internal.h ('k') | mojo/public/cpp/bindings/lib/validate_params.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698