mail archive of the barebox mailing list
 help / color / mirror / Atom feed
* [PATCH master 1/4] commands: stat: fix leaking file descriptors on early exit
@ 2025-06-23  6:20 Ahmad Fatoum
  2025-06-23  6:20 ` [PATCH master 2/4] lib: idr: make idr_for_each_entry removal safe Ahmad Fatoum
                   ` (3 more replies)
  0 siblings, 4 replies; 5+ messages in thread
From: Ahmad Fatoum @ 2025-06-23  6:20 UTC (permalink / raw)
  To: barebox; +Cc: Ahmad Fatoum

We currently leak the dirfd if we encounter an error during option
parsing. Fix this by only recording the path at first and opening the
dirfd once all options have been parsed.

Fixes: 5b175c52b53b ("commands: stat: add option for statat")
Signed-off-by: Ahmad Fatoum <a.fatoum@barebox.org>
---
 commands/stat.c | 11 ++++++++---
 1 file changed, 8 insertions(+), 3 deletions(-)

diff --git a/commands/stat.c b/commands/stat.c
index 8a96bf77b0f1..581cefad871d 100644
--- a/commands/stat.c
+++ b/commands/stat.c
@@ -14,6 +14,7 @@ static int do_stat(int argc, char *argv[])
 {
 	int (*statfn)(int dirfd, const char *, struct stat *) = lstatat;
 	int ret, opt, dirfd = AT_FDCWD, extra_flags = 0, exitcode = 0;
+	const char *dir = NULL;
 	char **filename;
 	struct stat st;
 
@@ -26,9 +27,7 @@ static int do_stat(int argc, char *argv[])
 			extra_flags |= O_CHROOT;
 			fallthrough;
 		case 'c':
-			dirfd = open(optarg, O_PATH | O_DIRECTORY | extra_flags);
-			if (dirfd < 0)
-				return dirfd;
+			dir = optarg;
 			break;
 		default:
 			return COMMAND_ERROR_USAGE;
@@ -38,6 +37,12 @@ static int do_stat(int argc, char *argv[])
 	if (optind == argc)
 		return COMMAND_ERROR_USAGE;
 
+	if (dir) {
+		dirfd = open(dir, O_PATH | O_DIRECTORY | extra_flags);
+		if (dirfd < 0)
+			return dirfd;
+	}
+
 	for (filename = &argv[optind]; *filename; filename++) {
 		ret = statfn(dirfd, *filename, &st);
 
-- 
2.39.5




^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2025-06-23  8:57 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2025-06-23  6:20 [PATCH master 1/4] commands: stat: fix leaking file descriptors on early exit Ahmad Fatoum
2025-06-23  6:20 ` [PATCH master 2/4] lib: idr: make idr_for_each_entry removal safe Ahmad Fatoum
2025-06-23  6:20 ` [PATCH master 3/4] startup: track system state with regards to initcall/exitcalls Ahmad Fatoum
2025-06-23  6:20 ` [PATCH master 4/4] fs: do not skip fs_remove when calling umount ramfs Ahmad Fatoum
2025-06-23  8:36 ` [PATCH master 1/4] commands: stat: fix leaking file descriptors on early exit Sascha Hauer

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox