bpf.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Mykola Lysenko <mykolal@fb.com>
To: <bpf@vger.kernel.org>, <ast@kernel.org>, <andrii@kernel.org>,
	<daniel@iogearbox.net>
Cc: <kernel-team@fb.com>, Mykola Lysenko <mykolal@fb.com>
Subject: [PATCH bpf-next] selftests/bpf: fix two memory leaks in prog_tests
Date: Thu, 28 Apr 2022 15:57:44 -0700	[thread overview]
Message-ID: <20220428225744.1961643-1-mykolal@fb.com> (raw)

Fix log_fp memory leak in dispatch_thread_read_log.
Remove obsolete log_fp clean-up code in dispatch_thread.

Also, release memory of subtest_selector. This can be
reproduced with -n 2/1 parameters.

Signed-off-by: Mykola Lysenko <mykolal@fb.com>
---
 tools/testing/selftests/bpf/test_progs.c | 17 +++++++++++------
 1 file changed, 11 insertions(+), 6 deletions(-)

diff --git a/tools/testing/selftests/bpf/test_progs.c b/tools/testing/selftests/bpf/test_progs.c
index 22fe283710fa..a07da648af3b 100644
--- a/tools/testing/selftests/bpf/test_progs.c
+++ b/tools/testing/selftests/bpf/test_progs.c
@@ -77,10 +77,12 @@ static void stdio_restore_cleanup(void)
 
 	if (env.subtest_state) {
 		fclose(env.subtest_state->stdout);
+		env.subtest_state->stdout = NULL;
 		stdout = env.test_state->stdout;
 		stderr = env.test_state->stdout;
 	} else {
 		fclose(env.test_state->stdout);
+		env.test_state->stdout = NULL;
 	}
 #endif
 }
@@ -1077,6 +1079,7 @@ static int read_prog_test_msg(int sock_fd, struct msg *msg, enum msg_type type)
 static int dispatch_thread_read_log(int sock_fd, char **log_buf, size_t *log_cnt)
 {
 	FILE *log_fp = NULL;
+	int result = 0;
 
 	log_fp = open_memstream(log_buf, log_cnt);
 	if (!log_fp)
@@ -1085,16 +1088,20 @@ static int dispatch_thread_read_log(int sock_fd, char **log_buf, size_t *log_cnt
 	while (true) {
 		struct msg msg;
 
-		if (read_prog_test_msg(sock_fd, &msg, MSG_TEST_LOG))
-			return 1;
+		if (read_prog_test_msg(sock_fd, &msg, MSG_TEST_LOG)) {
+			result = 1;
+			goto out;
+		}
 
 		fprintf(log_fp, "%s", msg.test_log.log_buf);
 		if (msg.test_log.is_last)
 			break;
 	}
+
+out:
 	fclose(log_fp);
 	log_fp = NULL;
-	return 0;
+	return result;
 }
 
 static int dispatch_thread_send_subtests(int sock_fd, struct test_state *state)
@@ -1132,7 +1139,6 @@ static void *dispatch_thread(void *ctx)
 {
 	struct dispatch_data *data = ctx;
 	int sock_fd;
-	FILE *log_fp = NULL;
 
 	sock_fd = data->sock_fd;
 
@@ -1214,8 +1220,6 @@ static void *dispatch_thread(void *ctx)
 	if (env.debug)
 		fprintf(stderr, "[%d]: Protocol/IO error: %s.\n", data->worker_id, strerror(errno));
 
-	if (log_fp)
-		fclose(log_fp);
 done:
 	{
 		struct msg msg_exit;
@@ -1685,6 +1689,7 @@ int main(int argc, char **argv)
 		unload_bpf_testmod();
 
 	free_test_selector(&env.test_selector);
+	free_test_selector(&env.subtest_selector);
 	free_test_states();
 
 	if (env.succ_cnt + env.fail_cnt + env.skip_cnt == 0)
-- 
2.30.2


             reply	other threads:[~2022-04-28 23:00 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-04-28 22:57 Mykola Lysenko [this message]
2022-04-29  5:00 ` [PATCH bpf-next] selftests/bpf: fix two memory leaks in prog_tests patchwork-bot+netdevbpf

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20220428225744.1961643-1-mykolal@fb.com \
    --to=mykolal@fb.com \
    --cc=andrii@kernel.org \
    --cc=ast@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=daniel@iogearbox.net \
    --cc=kernel-team@fb.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).