diff mbox series

[5/8] trace-cmd: Fix a memory leak in tracecmd_msg_send_init_data

Message ID 20190204070855.8921-6-kaslevs@vmware.com (mailing list archive)
State Accepted
Commit a29c67cf2c960e84e0ada15d4088eaed8fd970aa
Headers show
Series trace-cmd protocol fixes | expand

Commit Message

Slavomir Kaslev Feb. 4, 2019, 7:08 a.m. UTC
Fix tracecmd_msg_send_init_data leaking memory for the messages it receives.

Signed-off-by: Slavomir Kaslev <kaslevs@vmware.com>
---
 tracecmd/trace-msg.c | 36 +++++++++++++++++++++++-------------
 1 file changed, 23 insertions(+), 13 deletions(-)
diff mbox series

Patch

diff --git a/tracecmd/trace-msg.c b/tracecmd/trace-msg.c
index f7ce863..b4b58d4 100644
--- a/tracecmd/trace-msg.c
+++ b/tracecmd/trace-msg.c
@@ -373,8 +373,7 @@  static int tracecmd_msg_wait_for_msg(int fd, struct tracecmd_msg *msg)
 int tracecmd_msg_send_init_data(struct tracecmd_msg_handle *msg_handle,
 				unsigned int **client_ports)
 {
-	struct tracecmd_msg send_msg;
-	struct tracecmd_msg recv_msg;
+	struct tracecmd_msg msg;
 	int fd = msg_handle->fd;
 	unsigned int *ports;
 	int i, cpus;
@@ -382,30 +381,41 @@  int tracecmd_msg_send_init_data(struct tracecmd_msg_handle *msg_handle,
 
 	*client_ports = NULL;
 
-	tracecmd_msg_init(MSG_TINIT, &send_msg);
-	ret = make_tinit(msg_handle, &send_msg);
+	tracecmd_msg_init(MSG_TINIT, &msg);
+	ret = make_tinit(msg_handle, &msg);
 	if (ret < 0)
-		return ret;
+		goto out;
 
-	ret = tracecmd_msg_send(fd, &send_msg);
+	ret = tracecmd_msg_send(fd, &msg);
 	if (ret < 0)
-		return ret;
+		goto out;
+
+	msg_free(&msg);
 
-	ret = tracecmd_msg_wait_for_msg(fd, &recv_msg);
+	ret = tracecmd_msg_wait_for_msg(fd, &msg);
 	if (ret < 0)
-		return ret;
+		goto out;
 
-	if (ntohl(recv_msg.hdr.cmd) != MSG_RINIT)
-		return -EINVAL;
+	if (ntohl(msg.hdr.cmd) != MSG_RINIT) {
+		ret = -EINVAL;
+		goto error;
+	}
 
-	cpus = ntohl(recv_msg.rinit.cpus);
+	cpus = ntohl(msg.rinit.cpus);
 	ports = malloc_or_die(sizeof(*ports) * cpus);
 	for (i = 0; i < cpus; i++)
-		ports[i] = ntohl(recv_msg.port_array[i]);
+		ports[i] = ntohl(msg.port_array[i]);
 
 	*client_ports = ports;
 
+	msg_free(&msg);
 	return 0;
+
+error:
+	error_operation(&msg);
+out:
+	msg_free(&msg);
+	return ret;
 }
 
 static bool process_option(struct tracecmd_msg_handle *msg_handle,