hashtable: silence -fsanitize=address warning for `hashlittle()` function
[lttng-tools.git] / src / common / buffer-view.c
CommitLineData
01dc0eed 1/*
ab5be9fa 2 * Copyright (C) 2017 Jérémie Galarneau <jeremie.galarneau@efficios.com>
01dc0eed 3 *
ab5be9fa 4 * SPDX-License-Identifier: LGPL-2.1-only
01dc0eed 5 *
01dc0eed
JG
6 */
7
8#include <common/buffer-view.h>
9#include <common/dynamic-buffer.h>
01dc0eed
JG
10#include <common/error.h>
11#include <assert.h>
12
b35aac84
JG
13LTTNG_HIDDEN
14struct 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
3e6e0df2
JG
21LTTNG_HIDDEN
22bool lttng_buffer_view_is_valid(const struct lttng_buffer_view *view)
23{
24 return view && view->data && view->size > 0;
25}
26
ff28f865 27LTTNG_HIDDEN
01dc0eed
JG
28struct 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;
48end:
49 return view;
50}
51
ff28f865 52LTTNG_HIDDEN
01dc0eed
JG
53struct 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;
73end:
74 return view;
75}
67d8e2ef
SM
76
77LTTNG_HIDDEN
78bool 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
115end:
116 return ret;
117}
This page took 0.037467 seconds and 4 git commands to generate.