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

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

Issue 659313006: mojo: Fix c++ bindings so serialization of empty arrays doesn't crash. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git/+/master
Patch Set: Created 6 years, 2 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 | « no previous file | mojo/public/cpp/bindings/tests/array_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 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_ARRAY_SERIALIZATION_H_ 5 #ifndef MOJO_PUBLIC_CPP_BINDINGS_LIB_ARRAY_SERIALIZATION_H_
6 #define MOJO_PUBLIC_CPP_BINDINGS_LIB_ARRAY_SERIALIZATION_H_ 6 #define MOJO_PUBLIC_CPP_BINDINGS_LIB_ARRAY_SERIALIZATION_H_
7 7
8 #include <string.h> // For |memcpy()|. 8 #include <string.h> // For |memcpy()|.
9 9
10 #include <vector> 10 #include <vector>
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after
56 } 56 }
57 template <bool element_is_nullable, typename ElementValidateParams> 57 template <bool element_is_nullable, typename ElementValidateParams>
58 static void SerializeElements(Array<E> input, 58 static void SerializeElements(Array<E> input,
59 Buffer* buf, 59 Buffer* buf,
60 Array_Data<F>* output) { 60 Array_Data<F>* output) {
61 static_assert(!element_is_nullable, 61 static_assert(!element_is_nullable,
62 "Primitive type should be non-nullable"); 62 "Primitive type should be non-nullable");
63 static_assert((IsSame<ElementValidateParams, NoValidateParams>::value), 63 static_assert((IsSame<ElementValidateParams, NoValidateParams>::value),
64 "Primitive type should not have array validate params"); 64 "Primitive type should not have array validate params");
65 65
66 memcpy(output->storage(), &input.storage()[0], input.size() * sizeof(E)); 66 if (input.size())
67 memcpy(output->storage(), &input.storage()[0], input.size() * sizeof(E));
67 } 68 }
68 static void DeserializeElements(Array_Data<F>* input, Array<E>* output) { 69 static void DeserializeElements(Array_Data<F>* input, Array<E>* output) {
69 std::vector<E> result(input->size()); 70 std::vector<E> result(input->size());
70 memcpy(&result[0], input->storage(), input->size() * sizeof(E)); 71 if (input->size())
72 memcpy(&result[0], input->storage(), input->size() * sizeof(E));
71 output->Swap(&result); 73 output->Swap(&result);
72 } 74 }
73 }; 75 };
74 76
75 template <> 77 template <>
76 struct ArraySerializer<bool, bool, false> { 78 struct ArraySerializer<bool, bool, false> {
77 static size_t GetSerializedSize(const Array<bool>& input) { 79 static size_t GetSerializedSize(const Array<bool>& input) {
78 return sizeof(Array_Data<bool>) + Align((input.size() + 7) / 8); 80 return sizeof(Array_Data<bool>) + Align((input.size() + 7) / 8);
79 } 81 }
80 template <bool element_is_nullable, typename ElementValidateParams> 82 template <bool element_is_nullable, typename ElementValidateParams>
(...skipping 209 matching lines...) Expand 10 before | Expand all | Expand 10 after
290 if (input) { 292 if (input) {
291 internal::ArraySerializer<E, F>::DeserializeElements(input, output); 293 internal::ArraySerializer<E, F>::DeserializeElements(input, output);
292 } else { 294 } else {
293 output->reset(); 295 output->reset();
294 } 296 }
295 } 297 }
296 298
297 } // namespace mojo 299 } // namespace mojo
298 300
299 #endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_ARRAY_SERIALIZATION_H_ 301 #endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_ARRAY_SERIALIZATION_H_
OLDNEW
« no previous file with comments | « no previous file | mojo/public/cpp/bindings/tests/array_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698