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