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

Side by Side 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, 10 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/c/bindings/BUILD.gn ('k') | mojo/public/c/bindings/lib/struct.c » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright 2016 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "mojo/public/c/bindings/message.h"
6
7 #include <stdint.h>
8
9 _Static_assert(sizeof(mojo_message_header_t) == 16u,
10 "mojo_message_header_t should be 16 bytes");
11
12 _Static_assert(sizeof(mojo_message_header_with_request_id_t) == 24u,
13 "mojo_message_header_t should be 24 bytes");
14
15 bool mojo_validate_message_header(const mojo_struct_header_t* header,
16 size_t size) {
17 if (header->num_bytes < sizeof(mojo_message_header_t) ||
18 size < sizeof(mojo_message_header_t) || size > UINT32_MAX) {
19 return false;
20 }
21
22 const mojo_message_header_t* message_header =
23 (const mojo_message_header_t*)header;
24
25 // Message expects response and message is response flags are mutually
26 // exclusive.
27 if ((message_header->flags & MOJO_MESSAGE_HEADER_FLAGS_EXPECTS_RESPONSE) &&
28 (message_header->flags & MOJO_MESSAGE_HEADER_FLAGS_IS_RESPONSE)) {
29 return false;
30 }
31
32 if (header->version == 0u) {
33 if (header->num_bytes != sizeof(mojo_message_header_t)) {
34 return false;
35 }
36
37 // Version 0 has no request id and should not have either of these flags.
38 if ((message_header->flags & MOJO_MESSAGE_HEADER_FLAGS_EXPECTS_RESPONSE) ||
39 (message_header->flags & MOJO_MESSAGE_HEADER_FLAGS_IS_RESPONSE)) {
40 return false;
41 }
42 } else if (header->version == 1u) {
43 if (header->num_bytes != sizeof(mojo_message_header_with_request_id_t)) {
44 return false;
45 }
46 }
47 // Accept unknown versions of the message header to be future-proof.
48
49 return true;
50 }
OLDNEW
« 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