[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <CAE2+fR83Y8ZKk8fqM0WgZeK4Zm4PZjBzoPMyMptVHfk81eXEtw@mail.gmail.com>
Date: Sat, 5 Apr 2025 11:29:44 +0530
From: malaya kumar rout <malayarout91@...il.com>
To: Andrii Nakryiko <andrii.nakryiko@...il.com>
Cc: Alexei Starovoitov <ast@...nel.org>, Daniel Borkmann <daniel@...earbox.net>,
Andrii Nakryiko <andrii@...nel.org>, Martin KaFai Lau <martin.lau@...ux.dev>,
Eduard Zingerman <eddyz87@...il.com>, Song Liu <song@...nel.org>,
Yonghong Song <yonghong.song@...ux.dev>, John Fastabend <john.fastabend@...il.com>,
KP Singh <kpsingh@...nel.org>, Stanislav Fomichev <sdf@...ichev.me>, Hao Luo <haoluo@...gle.com>,
Jiri Olsa <jolsa@...nel.org>, Mykola Lysenko <mykolal@...com>, Shuah Khan <shuah@...nel.org>,
Geliang Tang <geliang@...nel.org>, bpf@...r.kernel.org, linux-kselftest@...r.kernel.org,
linux-kernel@...r.kernel.org
Subject: Re: [PATCH] selftests/bpf: close the file descriptor to avoid
resource leaks
On Fri, Apr 4, 2025 at 9:22 PM Andrii Nakryiko
<andrii.nakryiko@...il.com> wrote:
>
> On Sun, Mar 23, 2025 at 11:43 PM Malaya Kumar Rout
> <malayarout91@...il.com> wrote:
> >
> > Static Analyis for bench_htab_mem.c with cppcheck:error
> > tools/testing/selftests/bpf/benchs/bench_htab_mem.c:284:3:
> > error: Resource leak: fd [resourceLeak]
> > tools/testing/selftests/bpf/prog_tests/sk_assign.c:41:3:
> > error: Resource leak: tc [resourceLeak]
> >
> > fix the issue by closing the file descriptor (fd & tc) when
> > read & fgets operation fails.
> >
> > Signed-off-by: Malaya Kumar Rout <malayarout91@...il.com>
> > ---
> > tools/testing/selftests/bpf/benchs/bench_htab_mem.c | 1 +
> > tools/testing/selftests/bpf/prog_tests/sk_assign.c | 4 +++-
> > 2 files changed, 4 insertions(+), 1 deletion(-)
> >
> > diff --git a/tools/testing/selftests/bpf/benchs/bench_htab_mem.c b/tools/testing/selftests/bpf/benchs/bench_htab_mem.c
> > index 926ee822143e..59746fd2c23a 100644
> > --- a/tools/testing/selftests/bpf/benchs/bench_htab_mem.c
> > +++ b/tools/testing/selftests/bpf/benchs/bench_htab_mem.c
> > @@ -281,6 +281,7 @@ static void htab_mem_read_mem_cgrp_file(const char *name, unsigned long *value)
> > got = read(fd, buf, sizeof(buf) - 1);
>
> It could be a bit cleaner to add close(fd) here and drop the one we
> have at the end of the function.
>
Here, close(fd) is now positioned within the error handling block,
guaranteeing that
the file descriptor will be closed prior to returning from the
function in the event of a read error.
Meanwhile, the final close(fd) at the end of the function is retained
for successful execution,
thereby avoiding any potential resource leaks.
Hence, It is essential to add the close(fd) in both locations to
prevent resource leakage.
> pw-bot: cr
>
> > if (got <= 0) {
> > *value = 0;
> > + close(fd);
> > return;
> > }
> > buf[got] = 0;
> > diff --git a/tools/testing/selftests/bpf/prog_tests/sk_assign.c b/tools/testing/selftests/bpf/prog_tests/sk_assign.c
> > index 0b9bd1d6f7cc..10a0ab954b8a 100644
> > --- a/tools/testing/selftests/bpf/prog_tests/sk_assign.c
> > +++ b/tools/testing/selftests/bpf/prog_tests/sk_assign.c
> > @@ -37,8 +37,10 @@ configure_stack(void)
> > tc = popen("tc -V", "r");
> > if (CHECK_FAIL(!tc))
> > return false;
> > - if (CHECK_FAIL(!fgets(tc_version, sizeof(tc_version), tc)))
> > + if (CHECK_FAIL(!fgets(tc_version, sizeof(tc_version), tc))) {
> > + pclose(tc);
>
> this one looks good
>
> > return false;
> > + }
> > if (strstr(tc_version, ", libbpf "))
> > prog = "test_sk_assign_libbpf.bpf.o";
> > else
> > --
> > 2.43.0
> >
Powered by blists - more mailing lists