Fix: unchecked buffer size for communication header
[lttng-tools.git] / src / common / payload-view.c
CommitLineData
c0a66c84
JG
1/*
2 * Copyright (C) 2020 Jérémie Galarneau <jeremie.galarneau@efficios.com>
3 *
4 * SPDX-License-Identifier: LGPL-2.1-only
5 *
6 */
7
5a2f5f00 8#include <common/dynamic-array.h>
c0a66c84
JG
9#include <common/buffer-view.h>
10#include "payload-view.h"
11#include "payload.h"
12#include <stddef.h>
13
3e6e0df2
JG
14LTTNG_HIDDEN
15bool lttng_payload_view_is_valid(const struct lttng_payload_view *view)
16{
17 return view && lttng_buffer_view_is_valid(&view->buffer);
18}
19
c0a66c84
JG
20LTTNG_HIDDEN
21struct lttng_payload_view lttng_payload_view_from_payload(
22 const struct lttng_payload *payload, size_t offset,
23 ptrdiff_t len)
24{
3e6e0df2 25 return payload ? (struct lttng_payload_view) {
c0a66c84
JG
26 .buffer = lttng_buffer_view_from_dynamic_buffer(
27 &payload->buffer, offset, len),
fe489250 28 ._fd_handles = payload->_fd_handles,
3e6e0df2 29 } : (struct lttng_payload_view) {};
c0a66c84
JG
30}
31
32LTTNG_HIDDEN
33struct lttng_payload_view lttng_payload_view_from_view(
34 struct lttng_payload_view *view, size_t offset,
35 ptrdiff_t len)
36{
3e6e0df2 37 return view ? (struct lttng_payload_view) {
c0a66c84 38 .buffer = lttng_buffer_view_from_view(
ddab38d1 39 &view->buffer, offset, len),
fe489250
JG
40 ._fd_handles = view->_fd_handles,
41 ._iterator.p_fd_handles_position = view->_iterator.p_fd_handles_position ?:
42 &view->_iterator.fd_handles_position,
3e6e0df2 43 } : (struct lttng_payload_view) {};
c0a66c84
JG
44}
45
46LTTNG_HIDDEN
47struct lttng_payload_view lttng_payload_view_from_dynamic_buffer(
48 const struct lttng_dynamic_buffer *buffer, size_t offset,
49 ptrdiff_t len)
50{
3e6e0df2 51 return buffer ? (struct lttng_payload_view) {
c0a66c84
JG
52 .buffer = lttng_buffer_view_from_dynamic_buffer(
53 buffer, offset, len)
3e6e0df2 54 } : (struct lttng_payload_view) {};
c0a66c84
JG
55}
56
5a2f5f00
JG
57LTTNG_HIDDEN
58struct lttng_payload_view lttng_payload_view_from_buffer_view(
59 const struct lttng_buffer_view *view, size_t offset,
60 ptrdiff_t len)
61{
3e6e0df2 62 return view ? (struct lttng_payload_view) {
5a2f5f00
JG
63 .buffer = lttng_buffer_view_from_view(
64 view, offset, len)
3e6e0df2 65 } : (struct lttng_payload_view) {};
5a2f5f00
JG
66}
67
e368fb43
JG
68LTTNG_HIDDEN
69struct lttng_payload_view lttng_payload_view_init_from_buffer(
70 const char *src, size_t offset, ptrdiff_t len)
71{
72 return (struct lttng_payload_view) {
73 .buffer = lttng_buffer_view_init(
74 src, offset, len)
75 };
76}
77
5a2f5f00 78LTTNG_HIDDEN
18eec1c9
JG
79int lttng_payload_view_get_fd_handle_count(
80 const struct lttng_payload_view *payload_view)
5a2f5f00
JG
81{
82 int ret;
83 size_t position;
84
85 if (!payload_view) {
86 ret = -1;
87 goto end;
88 }
89
fe489250 90 ret = lttng_dynamic_pointer_array_get_count(&payload_view->_fd_handles);
5a2f5f00
JG
91 if (ret < 0) {
92 goto end;
93 }
94
fe489250
JG
95 position = payload_view->_iterator.p_fd_handles_position ?
96 *payload_view->_iterator.p_fd_handles_position :
97 payload_view->_iterator.fd_handles_position;
5a2f5f00
JG
98 ret = ret - (int) position;
99end:
100 return ret;
101}
102
c0a66c84 103LTTNG_HIDDEN
fe489250
JG
104struct fd_handle *lttng_payload_view_pop_fd_handle(
105 struct lttng_payload_view *view)
c0a66c84 106{
fe489250
JG
107 struct fd_handle *handle = NULL;
108 size_t fd_handle_count;
c0a66c84
JG
109 size_t *pos;
110
111 if (!view) {
c0a66c84
JG
112 goto end;
113 }
114
fe489250
JG
115 fd_handle_count = lttng_payload_view_get_fd_handle_count(view);
116 if (fd_handle_count == 0) {
c0a66c84
JG
117 goto end;
118 }
119
fe489250
JG
120 pos = view->_iterator.p_fd_handles_position ?
121 view->_iterator.p_fd_handles_position :
122 &view->_iterator.fd_handles_position;
123 handle = lttng_dynamic_pointer_array_get_pointer(&view->_fd_handles,
124 *pos);
c0a66c84 125 (*pos)++;
fe489250 126 fd_handle_get(handle);
c0a66c84 127end:
fe489250 128 return handle;
c0a66c84 129}
This page took 0.042113 seconds and 4 git commands to generate.