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

Side by Side Diff: mojo/public/tools/bindings/generators/cpp_templates/struct_serialization_definition.tmpl

Issue 1800753005: C++ bindings: A struct's Deserialize() now does validation before deserializing. (Closed) Base URL: https://github.com/domokit/mojo.git@master
Patch Set: Created 4 years, 9 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 {%- import "struct_macros.tmpl" as struct_macros %} 1 {%- import "struct_macros.tmpl" as struct_macros %}
2 2
3 size_t {{struct.name}}::GetSerializedSize() const { 3 size_t {{struct.name}}::GetSerializedSize() const {
4 return GetSerializedSize_(*this); 4 return GetSerializedSize_(*this);
5 } 5 }
6 6
7 bool {{struct.name}}::Serialize(void* buf, 7 bool {{struct.name}}::Serialize(void* buf,
8 size_t buf_size) { 8 size_t buf_size) {
9 MOJO_DCHECK(buf);
10
9 mojo::internal::FixedBuffer overlay_buf; 11 mojo::internal::FixedBuffer overlay_buf;
10 overlay_buf.Initialize(buf, buf_size); 12 overlay_buf.Initialize(buf, buf_size);
11 13
12 internal::{{struct.name}}_Data* output_ptr; 14 internal::{{struct.name}}_Data* output_ptr;
13 if (Serialize_(this, &overlay_buf, &output_ptr) != 15 auto err = Serialize_(this, &overlay_buf, &output_ptr);
14 mojo::internal::ValidationError::NONE) { 16 if (err != mojo::internal::ValidationError::NONE) {
15 // TODO(vardhan): Once Serialize_() outputs handles that it serialized 17 // TODO(vardhan): Once Serialize_() outputs handles that it serialized
16 // (even partially, if there are failures), we should CHECK fail here if 18 // (even partially, if there are failures), we should CHECK fail here if
17 // handles are non-empty. 19 // handles are non-empty.
20 MOJO_DLOG(ERROR) << "Could not serialize: " <<
21 mojo::internal::ValidationErrorToString(err);
18 return false; 22 return false;
19 } 23 }
20 24
21 std::vector<mojo::Handle> handles; 25 std::vector<mojo::Handle> handles;
22 output_ptr->EncodePointersAndHandles(&handles); 26 output_ptr->EncodePointersAndHandles(&handles);
23
24 MOJO_CHECK(handles.empty()) << "Serialize() does not support handles."; 27 MOJO_CHECK(handles.empty()) << "Serialize() does not support handles.";
25 28
26 return true; 29 return true;
27 } 30 }
28 31
29 void {{struct.name}}::Deserialize(void* buf) { 32 bool {{struct.name}}::Deserialize(void* buf, size_t buf_size) {
viettrungluu 2016/03/14 22:56:23 I do wonder whether you shouldn't expose three thi
vardhan 2016/03/14 23:11:33 Initially, I was thinking about passing in a Deser
33 MOJO_DCHECK(buf);
34
30 internal::{{struct.name}}_Data* input = 35 internal::{{struct.name}}_Data* input =
31 static_cast<internal::{{struct.name}}_Data*>(buf); 36 static_cast<internal::{{struct.name}}_Data*>(buf);
37 mojo::internal::BoundsChecker checker(buf, buf_size, 0);
38 std::string err_str;
39 mojo::internal::ValidationError err =
40 internal::{{struct.name}}_Data::Validate(buf, &checker, &err_str);
41 if (err != mojo::internal::ValidationError::NONE) {
42 MOJO_DLOG(ERROR) << "Deserialization error "
43 << mojo::internal::ValidationErrorToString(err)
44 << ": " << err_str;
45 return false;
46 }
32 47
33 std::vector<mojo::Handle> handles; 48 std::vector<mojo::Handle> handles;
34 input->DecodePointersAndHandles(&handles); 49 input->DecodePointersAndHandles(&handles);
35 MOJO_CHECK(handles.empty()) << "Deserialize() does not support handles."; 50 MOJO_CHECK(handles.empty()) << "Deserialize() does not support handles.";
36 51
37 Deserialize_(input, this); 52 Deserialize_(input, this);
53 return true;
38 } 54 }
39 55
40 size_t GetSerializedSize_(const {{struct.name}}& input) { 56 size_t GetSerializedSize_(const {{struct.name}}& input) {
41 {{struct_macros.get_serialized_size(struct, "input.%s")}} 57 {{struct_macros.get_serialized_size(struct, "input.%s")}}
42 return size; 58 return size;
43 } 59 }
44 60
45 mojo::internal::ValidationError Serialize_( 61 mojo::internal::ValidationError Serialize_(
46 {{struct.name}}* input, 62 {{struct.name}}* input,
47 mojo::internal::Buffer* buf, 63 mojo::internal::Buffer* buf,
48 internal::{{struct.name}}_Data** output) { 64 internal::{{struct.name}}_Data** output) {
49 if (input) { 65 if (input) {
50 {{struct_macros.serialize(struct, struct.name ~ " struct", "input->%s", "res ult", "buf", true)|indent(2)}} 66 {{struct_macros.serialize(struct, struct.name ~ " struct", "input->%s", "res ult", "buf", true)|indent(2)}}
51 *output = result; 67 *output = result;
52 } else { 68 } else {
53 *output = nullptr; 69 *output = nullptr;
54 } 70 }
55 return mojo::internal::ValidationError::NONE; 71 return mojo::internal::ValidationError::NONE;
56 } 72 }
57 73
58 void Deserialize_(internal::{{struct.name}}_Data* input, 74 void Deserialize_(internal::{{struct.name}}_Data* input,
59 {{struct.name}}* result) { 75 {{struct.name}}* result) {
60 if (input) { 76 if (input) {
61 {{struct_macros.deserialize(struct, "input", "result->%s")|indent(2)}} 77 {{struct_macros.deserialize(struct, "input", "result->%s")|indent(2)}}
62 } 78 }
63 } 79 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698