Re: [PATCH] selftests/bpf: close the file descriptor to avoid resource leaks

From: Hou Tao
Date: Sun Apr 06 2025 - 21:37:12 EST


Hi,

On 4/5/2025 1:59 PM, malaya kumar rout wrote:
> On Fri, Apr 4, 2025 at 9:22 PM Andrii Nakryiko
> <andrii.nakryiko@xxxxxxxxx> wrote:
>> On Sun, Mar 23, 2025 at 11:43 PM Malaya Kumar Rout
>> <malayarout91@xxxxxxxxx> 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@xxxxxxxxx>
>>> ---
>>> 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.

I think Andrii was proposing the following solution:

{
        /* ...... */
        got = read(fd, buf, sizeof(buf) - 1);
        close(fd);
        if (got <= 0) {
                *value = 0;
                return;
        }
        buf[got] = 0;

        *value = strtoull(buf, NULL, 0);
}

It only invokes close(fd) once to handle both the failed case and the
successful case.
>
>> 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
>>>
> .