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

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

Issue 2286513002: Mojo C++ bindings: fix inlined union validation. (Closed)
Patch Set: add a test case Created 4 years, 3 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 | « mojo/public/interfaces/bindings/tests/validation_test_interfaces.mojom ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 {%- import "validation_macros.tmpl" as validation_macros %} 1 {%- import "validation_macros.tmpl" as validation_macros %}
2 {%- set class_name = union.name ~ "_Data" %} 2 {%- set class_name = union.name ~ "_Data" %}
3 {%- set enum_name = union.name ~ "_Tag" -%} 3 {%- set enum_name = union.name ~ "_Tag" -%}
4 4
5 // static 5 // static
6 bool {{class_name}}::Validate( 6 bool {{class_name}}::Validate(
7 const void* data, 7 const void* data,
8 mojo::internal::ValidationContext* validation_context, 8 mojo::internal::ValidationContext* validation_context,
9 bool inlined) { 9 bool inlined) {
10 if (!data) 10 if (!data) {
11 DCHECK(!inlined);
11 return true; 12 return true;
13 }
12 14
13 if (!ValidateUnionHeaderAndClaimMemory(data, inlined, validation_context)) 15 // If it is inlined, the alignment is already enforced by its enclosing
16 // object. We don't have to validate that.
17 DCHECK(!inlined || mojo::internal::IsAligned(data));
18
19 if (!inlined &&
20 !mojo::internal::ValidateNonInlinedUnionHeaderAndClaimMemory(
21 data, validation_context)) {
14 return false; 22 return false;
23 }
15 24
16 const {{class_name}}* object = static_cast<const {{class_name}}*>(data); 25 const {{class_name}}* object = static_cast<const {{class_name}}*>(data);
17 ALLOW_UNUSED_LOCAL(object); 26 ALLOW_UNUSED_LOCAL(object);
18 27
28 if (inlined && object->is_null())
29 return true;
30
19 switch (object->tag) { 31 switch (object->tag) {
20 {% for field in union.fields %} 32 {% for field in union.fields %}
21 case {{enum_name}}::{{field.name|upper}}: { 33 case {{enum_name}}::{{field.name|upper}}: {
22 {%- set field_expr = "object->data.f_" ~ field.name %} 34 {%- set field_expr = "object->data.f_" ~ field.name %}
23 {{validation_macros.validate_field(field, field_expr, union.name, false)|indent( 4)}} 35 {{validation_macros.validate_field(field, field_expr, union.name, false)|indent( 4)}}
24 return true; 36 return true;
25 } 37 }
26 {%- endfor %} 38 {%- endfor %}
27 default: { 39 default: {
28 ReportValidationError( 40 ReportValidationError(
29 validation_context, 41 validation_context,
30 mojo::internal::VALIDATION_ERROR_UNKNOWN_UNION_TAG, 42 mojo::internal::VALIDATION_ERROR_UNKNOWN_UNION_TAG,
31 "unknown tag in {{union.name}}"); 43 "unknown tag in {{union.name}}");
32 return false; 44 return false;
33 } 45 }
34 } 46 }
35 } 47 }
OLDNEW
« no previous file with comments | « mojo/public/interfaces/bindings/tests/validation_test_interfaces.mojom ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698