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

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: Address comments. DeserializeWithoutValidation returns void, doesn't take in buf_size. 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 size_t* bytes_written) {
10 MOJO_DCHECK(buf);
11
9 mojo::internal::FixedBuffer overlay_buf; 12 mojo::internal::FixedBuffer overlay_buf;
10 overlay_buf.Initialize(buf, buf_size); 13 overlay_buf.Initialize(buf, buf_size);
11 14
12 internal::{{struct.name}}_Data* output_ptr; 15 internal::{{struct.name}}_Data* output_ptr;
13 if (Serialize_(this, &overlay_buf, &output_ptr) != 16 auto err = Serialize_(this, &overlay_buf, &output_ptr);
14 mojo::internal::ValidationError::NONE) { 17 if (err != mojo::internal::ValidationError::NONE) {
15 // TODO(vardhan): Once Serialize_() outputs handles that it serialized 18 // TODO(vardhan): Once Serialize_() outputs handles that it serialized
16 // (even partially, if there are failures), we should CHECK fail here if 19 // (even partially, if there are failures), we should CHECK fail here if
17 // handles are non-empty. 20 // handles are non-empty.
21 MOJO_DLOG(ERROR) << "Could not serialize: " <<
22 mojo::internal::ValidationErrorToString(err);
23
24 if (bytes_written)
25 *bytes_written = overlay_buf.BytesUsed();
18 return false; 26 return false;
19 } 27 }
20 28
21 std::vector<mojo::Handle> handles; 29 std::vector<mojo::Handle> handles;
22 output_ptr->EncodePointersAndHandles(&handles); 30 output_ptr->EncodePointersAndHandles(&handles);
23
24 MOJO_CHECK(handles.empty()) << "Serialize() does not support handles."; 31 MOJO_CHECK(handles.empty()) << "Serialize() does not support handles.";
25 32
33 if (bytes_written)
34 *bytes_written = overlay_buf.BytesUsed();
26 return true; 35 return true;
27 } 36 }
28 37
29 void {{struct.name}}::Deserialize(void* buf) { 38 bool {{struct.name}}::Deserialize(void* buf, size_t buf_size) {
39 MOJO_DCHECK(buf);
40
41 mojo::internal::BoundsChecker checker(buf, buf_size, 0);
42 std::string err_str;
viettrungluu 2016/03/30 17:49:01 You probably shouldn't unconditionally have an |st
vardhan 2016/03/30 22:49:07 Done.
43 mojo::internal::ValidationError err =
44 internal::{{struct.name}}_Data::Validate(buf, &checker, &err_str);
45 if (err != mojo::internal::ValidationError::NONE) {
46 MOJO_DLOG(ERROR) << "Deserialization error "
47 << mojo::internal::ValidationErrorToString(err)
48 << ": " << err_str;
49 return false;
50 }
51
52 DeserializeWithoutValidation(buf);
53 return true;
54 }
55
56 // TODO(vardhan): Make this |buf| a |const void*| once deserialization becomes
57 // immutable.
58 void {{struct.name}}::DeserializeWithoutValidation(void* buf) {
59 MOJO_DCHECK(buf);
60
30 internal::{{struct.name}}_Data* input = 61 internal::{{struct.name}}_Data* input =
31 static_cast<internal::{{struct.name}}_Data*>(buf); 62 static_cast<internal::{{struct.name}}_Data*>(buf);
32
33 std::vector<mojo::Handle> handles; 63 std::vector<mojo::Handle> handles;
34 input->DecodePointersAndHandles(&handles); 64 input->DecodePointersAndHandles(&handles);
35 MOJO_CHECK(handles.empty()) << "Deserialize() does not support handles."; 65 MOJO_CHECK(handles.empty()) << "Deserialization does not support handles.";
36 66
37 Deserialize_(input, this); 67 Deserialize_(input, this);
38 } 68 }
39 69
40 size_t GetSerializedSize_(const {{struct.name}}& input) { 70 size_t GetSerializedSize_(const {{struct.name}}& input) {
41 {{struct_macros.get_serialized_size(struct, "input.%s")}} 71 {{struct_macros.get_serialized_size(struct, "input.%s")}}
42 return size; 72 return size;
43 } 73 }
44 74
45 mojo::internal::ValidationError Serialize_( 75 mojo::internal::ValidationError Serialize_(
46 {{struct.name}}* input, 76 {{struct.name}}* input,
47 mojo::internal::Buffer* buf, 77 mojo::internal::Buffer* buf,
48 internal::{{struct.name}}_Data** output) { 78 internal::{{struct.name}}_Data** output) {
49 if (input) { 79 if (input) {
50 {{struct_macros.serialize(struct, struct.name ~ " struct", "input->%s", "res ult", "buf", true)|indent(2)}} 80 {{struct_macros.serialize(struct, struct.name ~ " struct", "input->%s", "res ult", "buf", true)|indent(2)}}
51 *output = result; 81 *output = result;
52 } else { 82 } else {
53 *output = nullptr; 83 *output = nullptr;
54 } 84 }
55 return mojo::internal::ValidationError::NONE; 85 return mojo::internal::ValidationError::NONE;
56 } 86 }
57 87
58 void Deserialize_(internal::{{struct.name}}_Data* input, 88 void Deserialize_(internal::{{struct.name}}_Data* input,
59 {{struct.name}}* result) { 89 {{struct.name}}* result) {
60 if (input) { 90 if (input) {
61 {{struct_macros.deserialize(struct, "input", "result->%s")|indent(2)}} 91 {{struct_macros.deserialize(struct, "input", "result->%s")|indent(2)}}
62 } 92 }
63 } 93 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698