Chromium Code Reviews

Unified 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.
Jump to:
View side-by-side diff with in-line comments
Index: mojo/public/tools/bindings/generators/cpp_templates/struct_serialization_definition.tmpl
diff --git a/mojo/public/tools/bindings/generators/cpp_templates/struct_serialization_definition.tmpl b/mojo/public/tools/bindings/generators/cpp_templates/struct_serialization_definition.tmpl
index 6ac6a3ea7ab800b3cc6c36cc7b4f17847e995f78..c8c9d3dff8acd80e692c9721a51136c144a5ced4 100644
--- a/mojo/public/tools/bindings/generators/cpp_templates/struct_serialization_definition.tmpl
+++ b/mojo/public/tools/bindings/generators/cpp_templates/struct_serialization_definition.tmpl
@@ -6,35 +6,51 @@ size_t {{struct.name}}::GetSerializedSize() const {
bool {{struct.name}}::Serialize(void* buf,
size_t buf_size) {
+ MOJO_DCHECK(buf);
+
mojo::internal::FixedBuffer overlay_buf;
overlay_buf.Initialize(buf, buf_size);
internal::{{struct.name}}_Data* output_ptr;
- if (Serialize_(this, &overlay_buf, &output_ptr) !=
- mojo::internal::ValidationError::NONE) {
+ auto err = Serialize_(this, &overlay_buf, &output_ptr);
+ if (err != mojo::internal::ValidationError::NONE) {
// TODO(vardhan): Once Serialize_() outputs handles that it serialized
// (even partially, if there are failures), we should CHECK fail here if
// handles are non-empty.
+ MOJO_DLOG(ERROR) << "Could not serialize: " <<
+ mojo::internal::ValidationErrorToString(err);
return false;
}
std::vector<mojo::Handle> handles;
output_ptr->EncodePointersAndHandles(&handles);
-
MOJO_CHECK(handles.empty()) << "Serialize() does not support handles.";
return true;
}
-void {{struct.name}}::Deserialize(void* buf) {
+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
+ MOJO_DCHECK(buf);
+
internal::{{struct.name}}_Data* input =
static_cast<internal::{{struct.name}}_Data*>(buf);
+ mojo::internal::BoundsChecker checker(buf, buf_size, 0);
+ std::string err_str;
+ mojo::internal::ValidationError err =
+ internal::{{struct.name}}_Data::Validate(buf, &checker, &err_str);
+ if (err != mojo::internal::ValidationError::NONE) {
+ MOJO_DLOG(ERROR) << "Deserialization error "
+ << mojo::internal::ValidationErrorToString(err)
+ << ": " << err_str;
+ return false;
+ }
std::vector<mojo::Handle> handles;
input->DecodePointersAndHandles(&handles);
MOJO_CHECK(handles.empty()) << "Deserialize() does not support handles.";
Deserialize_(input, this);
+ return true;
}
size_t GetSerializedSize_(const {{struct.name}}& input) {

Powered by Google App Engine