[PATCH] tools/firewire: nosy-dump: fix input file handle leak

longlong yan posted 1 patch 3 weeks, 3 days ago
tools/firewire/nosy-dump.c | 7 ++++++-
1 file changed, 6 insertions(+), 1 deletion(-)
[PATCH] tools/firewire: nosy-dump: fix input file handle leak
Posted by longlong yan 3 weeks, 3 days ago
The input file handle opened via the --input option is leaked on
multiple exit paths:

1. When fopen() for the output file fails, the already-opened input
   handle is not closed before returning.

2. When fread() reaches EOF while reading from the input file, the
   main loop returns directly, bypassing the cleanup section entirely.

3. On normal exit (e.g., SIGINT), the cleanup section closes output
   and fd but never closes input.

Fix this by closing the input handle on the output-fopen error path
and in the cleanup section, and by changing the fread EOF early return
to a break so that the cleanup section runs.

Signed-off-by: longlong yan <yanlonglong@kylinos.cn>
---
 tools/firewire/nosy-dump.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)

diff --git a/tools/firewire/nosy-dump.c b/tools/firewire/nosy-dump.c
index 9cc8626a7e94..7ddb50521c29 100644
--- a/tools/firewire/nosy-dump.c
+++ b/tools/firewire/nosy-dump.c
@@ -947,6 +947,8 @@ int main(int argc, const char *argv[])
 		output = fopen(option_output, "w");
 		if (output == NULL) {
 			fprintf(stderr, "Could not open %s, %m\n", option_output);
+			if (input != NULL)
+				fclose(input);
 			return -1;
 		}
 	}
@@ -973,7 +975,7 @@ int main(int argc, const char *argv[])
 	while (run) {
 		if (input != NULL) {
 			if (fread(&length, sizeof length, 1, input) != 1)
-				return 0;
+				break;
 			fread(buf, 1, length, input);
 		} else {
 			poll(pollfds, 2, -1);
@@ -1014,6 +1016,9 @@ int main(int argc, const char *argv[])
 	if (output != NULL)
 		fclose(output);

+	if (input != NULL)
+		fclose(input);
+
 	if (fd >= 0)
 		close(fd);

--
2.45.2
Re: [PATCH] tools/firewire: nosy-dump: fix input file handle leak
Posted by Takashi Sakamoto 3 weeks, 2 days ago
Hi,

On Wed, Sep 02, 2026 at 10:39:13AM +0800, longlong yan wrote:
> The input file handle opened via the --input option is leaked on
> multiple exit paths:
> 
> 1. When fopen() for the output file fails, the already-opened input
>    handle is not closed before returning.
> 
> 2. When fread() reaches EOF while reading from the input file, the
>    main loop returns directly, bypassing the cleanup section entirely.
> 
> 3. On normal exit (e.g., SIGINT), the cleanup section closes output
>    and fd but never closes input.
> 
> Fix this by closing the input handle on the output-fopen error path
> and in the cleanup section, and by changing the fread EOF early return
> to a break so that the cleanup section runs.
> 
> Signed-off-by: longlong yan <yanlonglong@kylinos.cn>
> ---
>  tools/firewire/nosy-dump.c | 7 ++++++-
>  1 file changed, 6 insertions(+), 1 deletion(-)

Applied to for-next branch.


Thanks

Takashi Sakamoto