Is
 
if (0 <= origfd)
...
close (origfd);
 
correct in this context?
 
This can close the standard fd 0 (stdin).
 
The GNU manual states:
 
When a new file descriptor is allocated, the operating system chooses the smallest non-negative integer that does not yet correspond to an open file descriptor. So, when a given fd (0, 1, or 2) is closed, opening a new file descriptor may assign the new file descriptor to this fd. This can have unintended effects, because now standard input/output/error of your process is referring to a file that was not meant to be used in that role.
 
Best regards,
 
Marius
 
Gesendet: Montag, 9. März 2026 um 12:37
Von: "Weixie Cui" <[email protected]>
CC: "Weixie Cui" <[email protected]>
Betreff: [PATCH] Fix wrong fd in restore_parent_fd fallback path
From: Weixie Cui <[email protected]>

Use fstat(origfd) instead of fstat(parentfd) when verifying the
alternatively-opened directory; parentfd is invalid in this branch.
---
src/create.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/src/create.c b/src/create.c
index a962ed75..71f183a5 100644
--- a/src/create.c
+++ b/src/create.c
@@ -1588,7 +1588,7 @@ restore_parent_fd (struct tar_stat_info const *st)
: openat (f.fd, f.base, open_searchdir_how.flags));
if (0 <= origfd)
{
- if (fstat (parentfd, &parentstat) < 0
+ if (fstat (origfd, &parentstat) < 0
|| !psame_inode (&parent->stat, &parentstat))
close (origfd);
else
--
2.39.5 (Apple Git-154)


Reply via email to