X-Git-Url: https://git.lttng.org/?a=blobdiff_plain;f=src%2Fcommon%2Fbuffer-view.c;h=bfd5ac6d707d0a5f2adb18cf2dbef24f5da52a74;hb=3afa94aeca5a0daae40fd7b6cc96b7e4c150c7d8;hp=4bdb1eb7d75d48be514689200954d69c44a7809b;hpb=ab5be9fa2eb5ba9600a82cd18fd3cfcbac69169a;p=lttng-tools.git diff --git a/src/common/buffer-view.c b/src/common/buffer-view.c index 4bdb1eb7d..bfd5ac6d7 100644 --- a/src/common/buffer-view.c +++ b/src/common/buffer-view.c @@ -8,9 +8,7 @@ #include #include #include -#include -LTTNG_HIDDEN struct lttng_buffer_view lttng_buffer_view_init( const char *src, size_t offset, ptrdiff_t len) { @@ -18,22 +16,28 @@ struct lttng_buffer_view lttng_buffer_view_init( return view; } -LTTNG_HIDDEN +bool lttng_buffer_view_is_valid(const struct lttng_buffer_view *view) +{ + return view && view->data && view->size > 0; +} + struct lttng_buffer_view lttng_buffer_view_from_view( const struct lttng_buffer_view *src, size_t offset, ptrdiff_t len) { struct lttng_buffer_view view = { .data = NULL, .size = 0 }; - assert(src); + LTTNG_ASSERT(src); if (offset > src->size) { - ERR("Attempt to create buffer view with invalid offset"); + ERR("Attempt to create buffer view from another view with invalid offset (offset > source size): source size = %zu, offset in source = %zu, length = %zd", + src->size, offset, len); goto end; } if (len != -1 && len > (src->size - offset)) { - ERR("Attempt to create buffer view with invalid length"); + 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", + src->size, offset, len); goto end; } @@ -43,22 +47,23 @@ end: return view; } -LTTNG_HIDDEN struct lttng_buffer_view lttng_buffer_view_from_dynamic_buffer( const struct lttng_dynamic_buffer *src, size_t offset, ptrdiff_t len) { struct lttng_buffer_view view = { .data = NULL, .size = 0 }; - assert(src); + LTTNG_ASSERT(src); if (offset > src->size) { - ERR("Attempt to create buffer view with invalid offset"); + 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", + src->size, offset, len); goto end; } if (len != -1 && len > (src->size - offset)) { - ERR("Attempt to create buffer view with invalid length"); + 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", + src->size, offset, len); goto end; } @@ -67,3 +72,44 @@ struct lttng_buffer_view lttng_buffer_view_from_dynamic_buffer( end: return view; } + +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; +}