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

Unified Diff: mojo/public/cpp/bindings/lib/array_serialization.h

Issue 1520153002: [mojo] Allow value deserialization to fail (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@bindings-3-misc-support
Patch Set: merge Created 5 years 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | mojo/public/cpp/bindings/lib/map_serialization.h » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: mojo/public/cpp/bindings/lib/array_serialization.h
diff --git a/mojo/public/cpp/bindings/lib/array_serialization.h b/mojo/public/cpp/bindings/lib/array_serialization.h
index b6168033b606035fdeb72b8ea0a17213e121c200..8759f6dc7cd33806be31586d4bfb27503f5c1fc3 100644
--- a/mojo/public/cpp/bindings/lib/array_serialization.h
+++ b/mojo/public/cpp/bindings/lib/array_serialization.h
@@ -30,7 +30,7 @@ inline void SerializeArray_(
const internal::ArrayValidateParams* validate_params);
template <typename E, typename F>
-inline void Deserialize_(internal::Array_Data<F>* data,
+inline bool Deserialize_(internal::Array_Data<F>* data,
Array<E>* output,
internal::SerializationContext* context);
@@ -62,13 +62,14 @@ struct ArraySerializer<E, F, false> {
if (input.size())
memcpy(output->storage(), &input.storage()[0], input.size() * sizeof(E));
}
- static void DeserializeElements(Array_Data<F>* input,
+ static bool DeserializeElements(Array_Data<F>* input,
Array<E>* output,
SerializationContext* context) {
std::vector<E> result(input->size());
if (input->size())
memcpy(&result[0], input->storage(), input->size() * sizeof(E));
output->Swap(&result);
+ return true;
}
};
@@ -92,7 +93,7 @@ struct ArraySerializer<bool, bool, false> {
for (size_t i = 0; i < input.size(); ++i)
output->at(i) = input[i];
}
- static void DeserializeElements(Array_Data<bool>* input,
+ static bool DeserializeElements(Array_Data<bool>* input,
Array<bool>* output,
SerializationContext* context) {
Array<bool> result(input->size());
@@ -100,6 +101,7 @@ struct ArraySerializer<bool, bool, false> {
for (size_t i = 0; i < input->size(); ++i)
result.at(i) = input->at(i);
output->Swap(&result);
+ return true;
}
};
@@ -127,13 +129,14 @@ struct ArraySerializer<ScopedHandleBase<H>, H, false> {
i));
}
}
- static void DeserializeElements(Array_Data<H>* input,
+ static bool DeserializeElements(Array_Data<H>* input,
Array<ScopedHandleBase<H>>* output,
SerializationContext* context) {
Array<ScopedHandleBase<H>> result(input->size());
for (size_t i = 0; i < input->size(); ++i)
result.at(i) = MakeScopedHandle(FetchAndReset(&input->at(i)));
output->Swap(&result);
+ return true;
}
};
@@ -171,14 +174,20 @@ struct ArraySerializer<
input.size(), i));
}
}
- static void DeserializeElements(Array_Data<S_Data*>* input,
+ static bool DeserializeElements(Array_Data<S_Data*>* input,
Array<S>* output,
SerializationContext* context) {
+ bool success = true;
Array<S> result(input->size());
for (size_t i = 0; i < input->size(); ++i) {
- Deserialize_(input->at(i), &result[i], context);
+ // Note that we rely on complete deserialization taking place in order to
+ // transfer ownership of all encoded handles. Therefore we don't
+ // short-circuit on failure here.
+ if (!Deserialize_(input->at(i), &result[i], context))
+ success = false;
}
output->Swap(&result);
+ return success;
}
private:
@@ -244,14 +253,20 @@ struct ArraySerializer<U, U_Data, true> {
}
}
- static void DeserializeElements(Array_Data<U_Data>* input,
+ static bool DeserializeElements(Array_Data<U_Data>* input,
Array<U>* output,
SerializationContext* context) {
+ bool success = true;
Array<U> result(input->size());
for (size_t i = 0; i < input->size(); ++i) {
- Deserialize_(&input->at(i), &result[i], context);
+ // Note that we rely on complete deserialization taking place in order to
+ // transfer ownership of all encoded handles. Therefore we don't
+ // short-circuit on failure here.
+ if (!Deserialize_(&input->at(i), &result[i], context))
+ success = false;
}
output->Swap(&result);
+ return success;
}
};
@@ -288,13 +303,17 @@ struct ArraySerializer<String, String_Data*> {
input.size(), i));
}
}
- static void DeserializeElements(Array_Data<String_Data*>* input,
+ static bool DeserializeElements(Array_Data<String_Data*>* input,
Array<String>* output,
SerializationContext* context) {
Array<String> result(input->size());
- for (size_t i = 0; i < input->size(); ++i)
- Deserialize_(input->at(i), &result[i], context);
+ for (size_t i = 0; i < input->size(); ++i) {
+ // It's OK to short-circuit here since string data cannot contain handles.
+ if (!Deserialize_(input->at(i), &result[i], context))
+ return false;
+ }
output->Swap(&result);
+ return true;
}
};
@@ -336,15 +355,18 @@ inline void SerializeArray_(
}
template <typename E, typename F>
-inline void Deserialize_(internal::Array_Data<F>* input,
+inline bool Deserialize_(internal::Array_Data<F>* input,
Array<E>* output,
internal::SerializationContext* context) {
if (input) {
- internal::ArraySerializer<E, F>::DeserializeElements(input, output,
- context);
+ if (!internal::ArraySerializer<E, F>::DeserializeElements(input, output,
+ context)) {
+ return false;
+ }
} else {
output->reset();
}
+ return true;
}
} // namespace mojo
« no previous file with comments | « no previous file | mojo/public/cpp/bindings/lib/map_serialization.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698