Re: [PATCH v2] selftests/rtc:Fix a resource leak

From: Alexandre Belloni
Date: Wed Jul 10 2024 - 03:55:27 EST


On 10/07/2024 00:43:09-0700, Zhu Jun wrote:
> The opened file should be closed before exit, otherwise resource leak
> will occur that this problem was discovered by code reading

The question is still why should it be closed before exit as it will be
closed on exit?

>
> Signed-off-by: Zhu Jun <zhujun2@xxxxxxxxxxxxxxxxxxxx>
> ---
> From a good programming practice perspective, especially in more complex programs,
> explicitly freeing allocated memory is a good habit!
>
>
> tools/testing/selftests/rtc/setdate.c | 1 +
> 1 file changed, 1 insertion(+)
>
> diff --git a/tools/testing/selftests/rtc/setdate.c b/tools/testing/selftests/rtc/setdate.c
> index b303890b3de2..17a00affb0ec 100644
> --- a/tools/testing/selftests/rtc/setdate.c
> +++ b/tools/testing/selftests/rtc/setdate.c
> @@ -65,6 +65,7 @@ int main(int argc, char **argv)
> retval = ioctl(fd, RTC_RD_TIME, &current);
> if (retval == -1) {
> perror("RTC_RD_TIME ioctl");
> + close(fd);
> exit(errno);
> }
>
> --
> 2.17.1
>
>
>

--
Alexandre Belloni, co-owner and COO, Bootlin
Embedded Linux and Kernel engineering
https://bootlin.com