* [PATCH] support: Close fds in copy_func
@ 2021-05-12 4:47 Siddhesh Poyarekar
2021-05-12 19:36 ` DJ Delorie
0 siblings, 1 reply; 2+ messages in thread
From: Siddhesh Poyarekar @ 2021-05-12 4:47 UTC (permalink / raw)
To: libc-alpha
copy_func may leave file descriptors open on error, so close them on
function exit.
---
support/shell-container.c | 21 +++++++++++++--------
1 file changed, 13 insertions(+), 8 deletions(-)
diff --git a/support/shell-container.c b/support/shell-container.c
index b64974086a..b2a4324dc7 100644
--- a/support/shell-container.c
+++ b/support/shell-container.c
@@ -93,8 +93,9 @@ copy_func (char **argv)
{
char *sname = argv[0];
char *dname = argv[1];
- int sfd, dfd;
+ int sfd = -1, dfd = -1;
struct stat st;
+ int ret = 1;
sfd = open (sname, O_RDONLY);
if (sfd < 0)
@@ -108,7 +109,7 @@ copy_func (char **argv)
{
fprintf (stderr, "cp: unable to fstat %s: %s\n",
sname, strerror (errno));
- return 1;
+ goto out;
}
dfd = open (dname, O_WRONLY | O_TRUNC | O_CREAT, 0600);
@@ -116,22 +117,26 @@ copy_func (char **argv)
{
fprintf (stderr, "cp: unable to open %s for writing: %s\n",
dname, strerror (errno));
- return 1;
+ goto out;
}
if (support_copy_file_range (sfd, 0, dfd, 0, st.st_size, 0) != st.st_size)
{
fprintf (stderr, "cp: cannot copy file %s to %s: %s\n",
sname, dname, strerror (errno));
- return 1;
+ goto out;
}
- close (sfd);
- close (dfd);
-
+ ret = 0;
chmod (dname, st.st_mode & 0777);
- return 0;
+out:
+ if (sfd >= 0)
+ close (sfd);
+ if (dfd >= 0)
+ close (dfd);
+
+ return ret;
}
--
2.31.1
^ permalink raw reply [flat|nested] 2+ messages in thread
* Re: [PATCH] support: Close fds in copy_func
2021-05-12 4:47 [PATCH] support: Close fds in copy_func Siddhesh Poyarekar
@ 2021-05-12 19:36 ` DJ Delorie
0 siblings, 0 replies; 2+ messages in thread
From: DJ Delorie @ 2021-05-12 19:36 UTC (permalink / raw)
To: Siddhesh Poyarekar; +Cc: libc-alpha
Siddhesh Poyarekar via Libc-alpha <libc-alpha@sourceware.org> writes:
> copy_func may leave file descriptors open on error, so close them on
> function exit.
LGTM.
Reviewed-by: DJ Delorie <dj@redhat.com>
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2021-05-12 19:36 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-05-12 4:47 [PATCH] support: Close fds in copy_func Siddhesh Poyarekar
2021-05-12 19:36 ` DJ Delorie
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).