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

Unified Diff: mojo/public/c/bindings/lib/message.c

Issue 1654373002: C types and utilities to validate and access struct and message headers (Closed) Base URL: git@github.com:domokit/mojo.git@master
Patch Set: review feedback, for trybots Created 4 years, 11 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « mojo/public/c/bindings/BUILD.gn ('k') | mojo/public/c/bindings/lib/struct.c » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: mojo/public/c/bindings/lib/message.c
diff --git a/mojo/public/c/bindings/lib/message.c b/mojo/public/c/bindings/lib/message.c
new file mode 100644
index 0000000000000000000000000000000000000000..cfbe01f8d5a4280ca3bb8039e4f0ffdf08682700
--- /dev/null
+++ b/mojo/public/c/bindings/lib/message.c
@@ -0,0 +1,50 @@
+// Copyright 2016 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "mojo/public/c/bindings/message.h"
+
+#include <stdint.h>
+
+_Static_assert(sizeof(mojo_message_header_t) == 16u,
+ "mojo_message_header_t should be 16 bytes");
+
+_Static_assert(sizeof(mojo_message_header_with_request_id_t) == 24u,
+ "mojo_message_header_t should be 24 bytes");
+
+bool mojo_validate_message_header(const mojo_struct_header_t* header,
+ size_t size) {
+ if (header->num_bytes < sizeof(mojo_message_header_t) ||
+ size < sizeof(mojo_message_header_t) || size > UINT32_MAX) {
+ return false;
+ }
+
+ const mojo_message_header_t* message_header =
+ (const mojo_message_header_t*)header;
+
+ // Message expects response and message is response flags are mutually
+ // exclusive.
+ if ((message_header->flags & MOJO_MESSAGE_HEADER_FLAGS_EXPECTS_RESPONSE) &&
+ (message_header->flags & MOJO_MESSAGE_HEADER_FLAGS_IS_RESPONSE)) {
+ return false;
+ }
+
+ if (header->version == 0u) {
+ if (header->num_bytes != sizeof(mojo_message_header_t)) {
+ return false;
+ }
+
+ // Version 0 has no request id and should not have either of these flags.
+ if ((message_header->flags & MOJO_MESSAGE_HEADER_FLAGS_EXPECTS_RESPONSE) ||
+ (message_header->flags & MOJO_MESSAGE_HEADER_FLAGS_IS_RESPONSE)) {
+ return false;
+ }
+ } else if (header->version == 1u) {
+ if (header->num_bytes != sizeof(mojo_message_header_with_request_id_t)) {
+ return false;
+ }
+ }
+ // Accept unknown versions of the message header to be future-proof.
+
+ return true;
+}
« no previous file with comments | « mojo/public/c/bindings/BUILD.gn ('k') | mojo/public/c/bindings/lib/struct.c » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698