@cryptotaxi247 / netdata-1 / commits / bbff41d84

Allocate buffer memory for uv_write and release in the callback function (#12688)

* Allocate memory needed for uv_write and free it in the callback function * Allocate memory needed for uv_write and free it in the callback function

Stelios Fragkakis committed Apr 19, 2022 at 11:34 UTC bbff41d8435d486f08f360acb56dd6b9cffdd6dd
2 files changed +28 -8
spawn/spawn_client.c
+11 -3
@@ -21,7 +21,10 @@ static void after_pipe_write(uv_write_t* req, int status)
21 #ifdef SPAWN_DEBUG
22 info("CLIENT %s called status=%d", __func__, status);
23 #endif
24 - freez(req->data);
24 + void **data = req->data;
25 + freez(data[0]);
26 + freez(data[1]);
27 + freez(data);
28 }
29
30 static void client_parse_spawn_protocol(unsigned source_len, char *source)
@@ -135,11 +138,16 @@ static void on_read_alloc(uv_handle_t* handle,
138 static void spawn_process_cmd(struct spawn_cmd_info *cmdinfo)
139 {
140 int ret;
138 - uv_buf_t writebuf[3];
141 + uv_buf_t *writebuf;
142 struct write_context *write_ctx;
143
144 + void **data = callocz(2, sizeof(void *));
145 + writebuf = callocz(3, sizeof(uv_buf_t));
146 write_ctx = callocz(1, sizeof(*write_ctx));
142 - write_ctx->write_req.data = write_ctx;
147 +
148 + data[0] = write_ctx;
149 + data[1] = writebuf;
150 + write_ctx->write_req.data = data;
151
152 uv_mutex_lock(&cmdinfo->mutex);
153 cmdinfo->flags |= SPAWN_CMD_PROCESSED;
spawn/spawn_server.c
+17 -5
@@ -71,12 +71,15 @@ static void after_pipe_write(uv_write_t *req, int status)
71 #ifdef SPAWN_DEBUG
72 fprintf(stderr, "SERVER %s called status=%d\n", __func__, status);
73 #endif
74 - freez(req->data);
74 + void **data = req->data;
75 + freez(data[0]);
76 + freez(data[1]);
77 + freez(data);
78 }
79
80 static void child_waited_async_cb(uv_async_t *async_handle)
81 {
79 - uv_buf_t writebuf[2];
82 + uv_buf_t *writebuf;
83 int ret;
84 struct spawn_execution_info *exec_info;
85 struct write_context *write_ctx;
@@ -84,8 +87,13 @@ static void child_waited_async_cb(uv_async_t *async_handle)
87 (void)async_handle;
88 while (NULL != (exec_info = dequeue_child_waited_list())) {
89 write_ctx = mallocz(sizeof(*write_ctx));
87 - write_ctx->write_req.data = write_ctx;
90
91 + void **data = callocz(2, sizeof(void *));
92 + writebuf = callocz(2, sizeof(uv_buf_t));
93 +
94 + data[0] = write_ctx;
95 + data[1] = writebuf;
96 + write_ctx->write_req.data = data;
97
98 write_ctx->header.opcode = SPAWN_PROT_CMD_EXIT_STATUS;
99 write_ctx->header.handle = exec_info->handle;
@@ -151,14 +159,18 @@ static void wait_children(void *arg)
159
160 void spawn_protocol_execute_command(void *handle, char *command_to_run, uint16_t command_length)
161 {
154 - uv_buf_t writebuf[2];
162 + uv_buf_t *writebuf;
163 int ret;
164 avl_t *avl_ret;
165 struct spawn_execution_info *exec_info;
166 struct write_context *write_ctx;
167
168 write_ctx = mallocz(sizeof(*write_ctx));
161 - write_ctx->write_req.data = write_ctx;
169 + void **data = callocz(2, sizeof(void *));
170 + writebuf = callocz(2, sizeof(uv_buf_t));
171 + data[0] = write_ctx;
172 + data[1] = writebuf;
173 + write_ctx->write_req.data = data;
174
175 command_to_run[command_length] = '\0';
176 #ifdef SPAWN_DEBUG