Fix: sessiond: use after free
[deliverable/lttng-tools.git] / src / common / payload.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
8#include "payload.h"
e368fb43
JG
9#include <common/dynamic-array.h>
10#include <common/dynamic-buffer.h>
11#include <common/error.h>
c0a66c84 12
fe489250
JG
13static
14void release_fd_handle_ref(void *ptr)
15{
16 struct fd_handle *fd_handle = ptr;
17
18 fd_handle_put(fd_handle);
19}
20
c0a66c84
JG
21LTTNG_HIDDEN
22void lttng_payload_init(struct lttng_payload *payload)
23{
24 assert(payload);
25 lttng_dynamic_buffer_init(&payload->buffer);
fe489250
JG
26 lttng_dynamic_pointer_array_init(&payload->_fd_handles,
27 release_fd_handle_ref);
c0a66c84
JG
28}
29
e368fb43
JG
30LTTNG_HIDDEN
31int lttng_payload_copy(const struct lttng_payload *src_payload,
32 struct lttng_payload *dst_payload)
33{
34 int ret;
35 size_t i;
36
e368fb43
JG
37 ret = lttng_dynamic_buffer_append_buffer(
38 &dst_payload->buffer, &src_payload->buffer);
39 if (ret) {
fe489250 40 goto end;
e368fb43
JG
41 }
42
fe489250
JG
43 for (i = 0; i < lttng_dynamic_pointer_array_get_count(
44 &src_payload->_fd_handles);
e368fb43 45 i++) {
fe489250
JG
46 struct fd_handle *new_fd_handle;
47 const struct fd_handle *src_fd_handle =
48 lttng_dynamic_pointer_array_get_pointer(
49 &src_payload->_fd_handles, i);
50
51 new_fd_handle = fd_handle_copy(src_fd_handle);
52 if (!new_fd_handle) {
53 PERROR("Failed to copy fd_handle while copying a payload");
54 ret = -1;
55 goto end;
e368fb43
JG
56 }
57
fe489250
JG
58 ret = lttng_payload_push_fd_handle(dst_payload, new_fd_handle);
59 fd_handle_put(new_fd_handle);
e368fb43 60 if (ret) {
fe489250 61 goto end;
e368fb43
JG
62 }
63 }
64
65end:
66 return ret;
e368fb43
JG
67}
68
c0a66c84
JG
69LTTNG_HIDDEN
70void lttng_payload_reset(struct lttng_payload *payload)
71{
72 if (!payload) {
73 return;
74 }
75
76 lttng_dynamic_buffer_reset(&payload->buffer);
fe489250 77 lttng_dynamic_pointer_array_reset(&payload->_fd_handles);
c0a66c84
JG
78}
79
80LTTNG_HIDDEN
fe489250
JG
81void lttng_payload_clear(struct lttng_payload *payload)
82{
035cc184 83 (void) lttng_dynamic_buffer_set_size(&payload->buffer, 0);
fe489250
JG
84 lttng_dynamic_pointer_array_clear(&payload->_fd_handles);
85}
86
87LTTNG_HIDDEN
88int lttng_payload_push_fd_handle(struct lttng_payload *payload,
89 struct fd_handle *fd_handle)
c0a66c84
JG
90{
91 int ret;
92
93 if (!payload) {
94 ret = -1;
95 goto end;
96 }
97
fe489250
JG
98 ret = lttng_dynamic_pointer_array_add_pointer(
99 &payload->_fd_handles, fd_handle);
100 if (ret) {
101 goto end;
102 }
103
104 fd_handle_get(fd_handle);
c0a66c84
JG
105end:
106 return ret;
107}
This page took 0.040165 seconds and 5 git commands to generate.