end:
return view;
}
+
+LTTNG_HIDDEN
+bool lttng_buffer_view_contains_string(const struct lttng_buffer_view *buf,
+ const char *str,
+ size_t len_with_null_terminator)
+{
+ const char *past_buf_end;
+ size_t max_str_len_with_null_terminator;
+ size_t str_len;
+ bool ret;
+
+ past_buf_end = buf->data + buf->size;
+
+ /* Is the start of the string in the buffer view? */
+ if (str < buf->data || str >= past_buf_end) {
+ ret = false;
+ goto end;
+ }
+
+ /*
+ * Max length the string could have to fit in the buffer, including
+ * NULL terminator.
+ */
+ max_str_len_with_null_terminator = past_buf_end - str;
+
+ /* Could the string even fit in the buffer? */
+ if (len_with_null_terminator > max_str_len_with_null_terminator) {
+ ret = false;
+ goto end;
+ }
+
+ str_len = lttng_strnlen(str, max_str_len_with_null_terminator);
+ if (str_len != (len_with_null_terminator - 1)) {
+ ret = false;
+ goto end;
+ }
+
+ ret = true;
+
+end:
+ return ret;
+}
#ifndef LTTNG_BUFFER_VIEW_H
#define LTTNG_BUFFER_VIEW_H
+#include <common/macros.h>
+#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
-#include <common/macros.h>
struct lttng_dynamic_buffer;
const struct lttng_dynamic_buffer *src, size_t offset,
ptrdiff_t len);
+/**
+ * Verify that `buf` contains a string starting at `str` of length
+ * `len_with_null_terminator`.
+ *
+ * @buf The buffer view
+ * @str The start of the string
+ * @len_with_null_terminator Expected length of the string, including the
+ * NULL terminator.
+ */
+LTTNG_HIDDEN
+bool lttng_buffer_view_contains_string(const struct lttng_buffer_view *buf,
+ const char *str,
+ size_t len_with_null_terminator);
+
#endif /* LTTNG_BUFFER_VIEW_H */
test_relayd_backward_compat_group_by_session \
ini_config/test_ini_config \
test_fd_tracker \
- test_uuid
+ test_uuid \
+ test_buffer_view
LIBTAP=$(top_builddir)/tests/utils/tap/libtap.la
test_utils_expand_path test_utils_compat_poll \
test_string_utils test_notification test_directory_handle \
test_relayd_backward_compat_group_by_session \
- test_fd_tracker test_uuid
+ test_fd_tracker test_uuid \
+ test_buffer_view
if HAVE_LIBLTTNG_UST_CTL
noinst_PROGRAMS += test_ust_data
# uuid unit test
test_uuid_SOURCES = test_uuid.c
test_uuid_LDADD = $(LIBTAP) $(LIBCOMMON)
+
+# buffer view unit test
+test_buffer_view_SOURCES = test_buffer_view.c
+test_buffer_view_LDADD = $(LIBTAP) $(LIBCOMMON)
--- /dev/null
+/*
+ * Copyright (C) 2020 EfficiOS, inc.
+ *
+ * SPDX-License-Identifier: GPL-2.0-only
+ *
+ */
+
+
+#include <common/buffer-view.h>
+#include <tap/tap.h>
+
+static const int TEST_COUNT = 5;
+
+/* For error.h */
+int lttng_opt_quiet = 1;
+int lttng_opt_verbose;
+int lttng_opt_mi;
+
+static void test_contains_string(void)
+{
+ const char buf[] = {'A', 'l', 'l', 'o', '\0'};
+ struct lttng_buffer_view view = lttng_buffer_view_init(buf, 0, 5);
+ struct lttng_buffer_view view_minus_one =
+ lttng_buffer_view_init(buf, 0, 4);
+
+ ok1(!lttng_buffer_view_contains_string(&view, buf, 4));
+ ok1(lttng_buffer_view_contains_string(&view, buf, 5));
+ ok1(!lttng_buffer_view_contains_string(&view, buf, 6));
+
+ ok1(!lttng_buffer_view_contains_string(&view_minus_one, buf, 4));
+ ok1(!lttng_buffer_view_contains_string(&view_minus_one, buf, 5));
+}
+
+int main(void)
+{
+ plan_tests(TEST_COUNT);
+
+ test_contains_string();
+
+ return exit_status();
+}