Fix: sessiond: leak of trigger on registration error
[lttng-tools.git] / src / common / buffer-view.c
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 #include <assert.h>
12
13 LTTNG_HIDDEN
14 struct lttng_buffer_view lttng_buffer_view_init(
15 const char *src, size_t offset, ptrdiff_t len)
16 {
17 struct lttng_buffer_view view = { .data = src + offset, .size = len };
18 return view;
19 }
20
21 LTTNG_HIDDEN
22 bool lttng_buffer_view_is_valid(const struct lttng_buffer_view *view)
23 {
24 return view && view->data && view->size > 0;
25 }
26
27 LTTNG_HIDDEN
28 struct lttng_buffer_view lttng_buffer_view_from_view(
29 const struct lttng_buffer_view *src, size_t offset,
30 ptrdiff_t len)
31 {
32 struct lttng_buffer_view view = { .data = NULL, .size = 0 };
33
34 assert(src);
35
36 if (offset > src->size) {
37 ERR("Attempt to create buffer view with invalid offset");
38 goto end;
39 }
40
41 if (len != -1 && len > (src->size - offset)) {
42 ERR("Attempt to create buffer view with invalid length");
43 goto end;
44 }
45
46 view.data = src->data + offset;
47 view.size = len == -1 ? (src->size - offset) : len;
48 end:
49 return view;
50 }
51
52 LTTNG_HIDDEN
53 struct lttng_buffer_view lttng_buffer_view_from_dynamic_buffer(
54 const struct lttng_dynamic_buffer *src, size_t offset,
55 ptrdiff_t len)
56 {
57 struct lttng_buffer_view view = { .data = NULL, .size = 0 };
58
59 assert(src);
60
61 if (offset > src->size) {
62 ERR("Attempt to create buffer view with invalid offset");
63 goto end;
64 }
65
66 if (len != -1 && len > (src->size - offset)) {
67 ERR("Attempt to create buffer view with invalid length");
68 goto end;
69 }
70
71 view.data = src->data + offset;
72 view.size = len == -1 ? (src->size - offset) : len;
73 end:
74 return view;
75 }
76
77 LTTNG_HIDDEN
78 bool lttng_buffer_view_contains_string(const struct lttng_buffer_view *buf,
79 const char *str,
80 size_t len_with_null_terminator)
81 {
82 const char *past_buf_end;
83 size_t max_str_len_with_null_terminator;
84 size_t str_len;
85 bool ret;
86
87 past_buf_end = buf->data + buf->size;
88
89 /* Is the start of the string in the buffer view? */
90 if (str < buf->data || str >= past_buf_end) {
91 ret = false;
92 goto end;
93 }
94
95 /*
96 * Max length the string could have to fit in the buffer, including
97 * NULL terminator.
98 */
99 max_str_len_with_null_terminator = past_buf_end - str;
100
101 /* Could the string even fit in the buffer? */
102 if (len_with_null_terminator > max_str_len_with_null_terminator) {
103 ret = false;
104 goto end;
105 }
106
107 str_len = lttng_strnlen(str, max_str_len_with_null_terminator);
108 if (str_len != (len_with_null_terminator - 1)) {
109 ret = false;
110 goto end;
111 }
112
113 ret = true;
114
115 end:
116 return ret;
117 }
This page took 0.042971 seconds and 4 git commands to generate.