lttng: list valid condition / action names if missing or unknown
[lttng-tools.git] / src / common / buffer-view.cpp
1 /*
2 * Copyright (C) 2017 Jérémie Galarneau <jeremie.galarneau@efficios.com>
3 *
4 * SPDX-License-Identifier: LGPL-2.1-only
5 *
6 */
7
8 #include <common/buffer-view.h>
9 #include <common/dynamic-buffer.h>
10 #include <common/error.h>
11
12 struct lttng_buffer_view lttng_buffer_view_init(
13 const char *src, size_t offset, ptrdiff_t len)
14 {
15 struct lttng_buffer_view view = { .data = src + offset, .size = (size_t) len };
16 return view;
17 }
18
19 bool lttng_buffer_view_is_valid(const struct lttng_buffer_view *view)
20 {
21 return view && view->data && view->size > 0;
22 }
23
24 struct lttng_buffer_view lttng_buffer_view_from_view(
25 const struct lttng_buffer_view *src, size_t offset,
26 ptrdiff_t len)
27 {
28 struct lttng_buffer_view view = { .data = NULL, .size = 0 };
29
30 LTTNG_ASSERT(src);
31
32 if (offset > src->size) {
33 ERR("Attempt to create buffer view from another view with invalid offset (offset > source size): source size = %zu, offset in source = %zu, length = %zd",
34 src->size, offset, len);
35 goto end;
36 }
37
38 if (len != -1 && len > (src->size - offset)) {
39 ERR("Attempt to create buffer view from another view with invalid length (length > space left after offset in source): source size = %zu, offset in source = %zu, length = %zd",
40 src->size, offset, len);
41 goto end;
42 }
43
44 view.data = src->data + offset;
45 view.size = len == -1 ? (src->size - offset) : len;
46 end:
47 return view;
48 }
49
50 struct lttng_buffer_view lttng_buffer_view_from_dynamic_buffer(
51 const struct lttng_dynamic_buffer *src, size_t offset,
52 ptrdiff_t len)
53 {
54 struct lttng_buffer_view view = { .data = NULL, .size = 0 };
55
56 LTTNG_ASSERT(src);
57
58 if (offset > src->size) {
59 ERR("Attempt to create buffer view from a dynamic buffer with invalid offset (offset > source size): source size = %zu, offset in source = %zu, length = %zd",
60 src->size, offset, len);
61 goto end;
62 }
63
64 if (len != -1 && len > (src->size - offset)) {
65 ERR("Attempt to create buffer view from a dynamic buffer with invalid length (length > space left after offset in source): source size = %zu, offset in source = %zu, length = %zd",
66 src->size, offset, len);
67 goto end;
68 }
69
70 view.data = src->data + offset;
71 view.size = len == -1 ? (src->size - offset) : len;
72 end:
73 return view;
74 }
75
76 bool lttng_buffer_view_contains_string(const struct lttng_buffer_view *buf,
77 const char *str,
78 size_t len_with_null_terminator)
79 {
80 const char *past_buf_end;
81 size_t max_str_len_with_null_terminator;
82 size_t str_len;
83 bool ret;
84
85 past_buf_end = buf->data + buf->size;
86
87 /* Is the start of the string in the buffer view? */
88 if (str < buf->data || str >= past_buf_end) {
89 ret = false;
90 goto end;
91 }
92
93 /*
94 * Max length the string could have to fit in the buffer, including
95 * NULL terminator.
96 */
97 max_str_len_with_null_terminator = past_buf_end - str;
98
99 /* Could the string even fit in the buffer? */
100 if (len_with_null_terminator > max_str_len_with_null_terminator) {
101 ret = false;
102 goto end;
103 }
104
105 str_len = lttng_strnlen(str, max_str_len_with_null_terminator);
106 if (str_len != (len_with_null_terminator - 1)) {
107 ret = false;
108 goto end;
109 }
110
111 ret = true;
112
113 end:
114 return ret;
115 }
This page took 0.047522 seconds and 4 git commands to generate.