Fix: add stricter checks on packet boundaries
[babeltrace.git] / formats / ctf / types / string.c
CommitLineData
6dc2ca62
MD
1/*
2 * Common Trace Format
3 *
4 * Strings read/write functions.
5 *
64fa3fec
MD
6 * Copyright 2010-2011 EfficiOS Inc. and Linux Foundation
7 *
8 * Author: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
6dc2ca62 9 *
ccd7e1c8
MD
10 * Permission is hereby granted, free of charge, to any person obtaining a copy
11 * of this software and associated documentation files (the "Software"), to deal
12 * in the Software without restriction, including without limitation the rights
13 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14 * copies of the Software, and to permit persons to whom the Software is
15 * furnished to do so, subject to the following conditions:
de0ba614 16 *
ccd7e1c8
MD
17 * The above copyright notice and this permission notice shall be included in
18 * all copies or substantial portions of the Software.
c462e188
MD
19 *
20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
26 * SOFTWARE.
6dc2ca62
MD
27 */
28
70bd0a12 29#include <babeltrace/babeltrace-internal.h>
d79865b9 30#include <babeltrace/ctf/types.h>
a3dbc794 31#include <limits.h> /* C99 limits */
6dc2ca62
MD
32#include <string.h>
33
0d69b916 34int ctf_string_read(struct bt_stream_pos *ppos, struct bt_definition *definition)
a52d7f6a 35{
d11e9c49
MD
36 struct definition_string *string_definition =
37 container_of(definition, struct definition_string, p);
38 const struct declaration_string *string_declaration =
39 string_definition->declaration;
40 struct ctf_stream_pos *pos = ctf_pos(ppos);
a52d7f6a 41 size_t len;
c5e74408 42 ssize_t max_len;
47e0f2e2 43 char *srcaddr;
a52d7f6a 44
70fd5a51
MD
45 if (!ctf_align_pos(pos, string_declaration->p.alignment))
46 return -EFAULT;
c5e74408 47
d11e9c49 48 srcaddr = ctf_get_pos_addr(pos);
670977d3 49 if (pos->offset == EOF)
df752f35 50 return -EFAULT;
c5e74408
MD
51 /* Not counting \0 */
52 max_len = pos->packet_size - pos->offset - 1;
53 if (max_len < 0)
54 return -EFAULT;
55 len = strnlen(srcaddr, max_len) + 1; /* Add \0 */
56 /* Truncated string, unexpected. Trace probably corrupted. */
57 if (srcaddr[len - 1] != '\0')
58 return -EFAULT;
59
d11e9c49
MD
60 if (string_definition->alloc_len < len) {
61 string_definition->value =
62 g_realloc(string_definition->value, len);
63 string_definition->alloc_len = len;
64 }
847bf71a 65 printf_debug("CTF string read %s\n", srcaddr);
d11e9c49
MD
66 memcpy(string_definition->value, srcaddr, len);
67 string_definition->len = len;
70fd5a51
MD
68 if (!ctf_move_pos(pos, len * CHAR_BIT))
69 return -EFAULT;
c5e74408 70 return 0;
6dc2ca62 71}
a52d7f6a 72
1cf393f6 73int ctf_string_write(struct bt_stream_pos *ppos,
0d69b916 74 struct bt_definition *definition)
a52d7f6a 75{
d11e9c49
MD
76 struct definition_string *string_definition =
77 container_of(definition, struct definition_string, p);
78 const struct declaration_string *string_declaration =
79 string_definition->declaration;
80 struct ctf_stream_pos *pos = ctf_pos(ppos);
a52d7f6a 81 size_t len;
47e0f2e2 82 char *destaddr;
a52d7f6a 83
70fd5a51
MD
84 if (!ctf_align_pos(pos, string_declaration->p.alignment))
85 return -EFAULT;
d11e9c49
MD
86 assert(string_definition->value != NULL);
87 len = string_definition->len;
c5e74408
MD
88
89 if (!ctf_pos_access_ok(pos, len))
90 return -EFAULT;
91
d11e9c49 92 if (pos->dummy)
a52d7f6a 93 goto end;
d11e9c49 94 destaddr = ctf_get_pos_addr(pos);
8563e754 95 memcpy(destaddr, string_definition->value, len);
a52d7f6a 96end:
70fd5a51
MD
97 if (!ctf_move_pos(pos, len * CHAR_BIT))
98 return -EFAULT;
c5e74408 99 return 0;
a52d7f6a 100}
This page took 0.033295 seconds and 4 git commands to generate.