From: Sergey Bugaev <bugaevc@gmail.com>
To: bug-hurd@gnu.org, libc-alpha@sourceware.org
Cc: "Flávio Cruz" <flaviocruz@gmail.com>,
"Sergey Bugaev" <bugaevc@gmail.com>
Subject: [RFC PATCH glibc 1/12] hurd: Refactor readlinkat()
Date: Sun, 12 Feb 2023 14:10:32 +0300 [thread overview]
Message-ID: <20230212111044.610942-2-bugaevc@gmail.com> (raw)
In-Reply-To: <20230212111044.610942-1-bugaevc@gmail.com>
Make the code flow more linear using early returns where possible. This
makes it so much easier to reason about what runs on error / successful
code paths.
Signed-off-by: Sergey Bugaev <bugaevc@gmail.com>
---
sysdeps/mach/hurd/readlinkat.c | 55 ++++++++++++++++++++--------------
1 file changed, 32 insertions(+), 23 deletions(-)
diff --git a/sysdeps/mach/hurd/readlinkat.c b/sysdeps/mach/hurd/readlinkat.c
index 1efb09ca..dabdbb37 100644
--- a/sysdeps/mach/hurd/readlinkat.c
+++ b/sysdeps/mach/hurd/readlinkat.c
@@ -31,38 +31,47 @@ __readlinkat (int fd, const char *file_name, char *buf, size_t len)
error_t err;
file_t file_stat;
struct stat64 st;
+ enum retry_type doretry;
+ char retryname[1024];
+ file_t file;
+ char *rbuf = buf;
file_stat = __file_name_lookup_at (fd, 0, file_name, O_NOLINK, 0);
if (file_stat == MACH_PORT_NULL)
return -1;
err = __io_stat (file_stat, &st);
- if (! err)
- if (S_ISLNK (st.st_mode))
- {
- enum retry_type doretry;
- char retryname[1024];
- file_t file;
- char *rbuf = buf;
+ if (err)
+ goto out;
+ if (!S_ISLNK (st.st_mode))
+ {
+ err = EINVAL;
+ goto out;
+ }
- err = __dir_lookup (file_stat, "", O_READ | O_NOLINK, 0, &doretry, retryname, &file);
- if (! err && (doretry != FS_RETRY_NORMAL || retryname[0] != '\0'))
- err = EGRATUITOUS;
- if (! err)
- {
- err = __io_read (file, &rbuf, &len, 0, len);
- if (!err && rbuf != buf)
- {
- memcpy (buf, rbuf, len);
- __vm_deallocate (__mach_task_self (), (vm_address_t)rbuf, len);
- }
+ err = __dir_lookup (file_stat, "", O_READ | O_NOLINK,
+ 0, &doretry, retryname, &file);
+ if (err)
+ goto out;
+ if (doretry != FS_RETRY_NORMAL || retryname[0] != '\0')
+ {
+ err = EGRATUITOUS;
+ goto out;
+ }
+
+ err = __io_read (file, &rbuf, &len, 0, len);
+ __mach_port_deallocate (__mach_task_self (), file);
+ if (err)
+ goto out;
+
+ if (rbuf != buf)
+ {
+ memcpy (buf, rbuf, len);
+ __vm_deallocate (__mach_task_self (), (vm_address_t) rbuf, len);
+ }
- __mach_port_deallocate (__mach_task_self (), file);
- }
- }
- else
- err = EINVAL;
+ out:
__mach_port_deallocate (__mach_task_self (), file_stat);
return err ? __hurd_fail (err) : len;
--
2.39.1
next prev parent reply other threads:[~2023-02-12 11:11 UTC|newest]
Thread overview: 47+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-02-12 11:10 [RFC PATCH 0/12] Towards glibc on x86_64-gnu Sergey Bugaev
2023-02-12 11:10 ` Sergey Bugaev [this message]
2023-02-12 14:49 ` [RFC PATCH glibc 1/12] hurd: Refactor readlinkat() Samuel Thibault
2023-02-12 11:10 ` [RFC PATCH glibc 2/12] hurd: Use mach_msg_type_number_t where appropriate Sergey Bugaev
2023-02-12 14:52 ` Samuel Thibault
2023-02-12 11:10 ` [RFC PATCH glibc 3/12] mach, hurd: Cast through uintptr_t Sergey Bugaev
2023-02-12 14:55 ` Samuel Thibault
2023-02-12 11:10 ` [RFC PATCH glibc 4/12] hurd: Fix xattr error value Sergey Bugaev
2023-02-12 14:56 ` Samuel Thibault
2023-02-12 11:10 ` [RFC PATCH glibc 5/12] htl: Fix semaphore reference Sergey Bugaev
2023-02-12 14:57 ` Samuel Thibault
2023-02-12 11:10 ` [RFC PATCH hurd 6/12] hurd: Fix modes_t and speeds_t types on 64-bit Sergey Bugaev
2023-02-12 15:00 ` Samuel Thibault
2023-02-12 15:15 ` Sergey Bugaev
2023-02-12 15:22 ` Samuel Thibault
2023-02-12 16:13 ` Sergey Bugaev
2023-02-12 16:30 ` Samuel Thibault
2023-02-12 19:03 ` Samuel Thibault
2023-02-12 11:10 ` [RFC PATCH mig 7/12] Drop -undef -ansi from cpp flags Sergey Bugaev
2023-02-12 15:01 ` Samuel Thibault
2023-02-12 18:43 ` Flávio Cruz
2023-02-12 18:44 ` Samuel Thibault
2023-02-12 11:10 ` [RFC PATCH mig 8/12] Set max type alignment to sizeof(long) Sergey Bugaev
2023-02-12 11:10 ` [RFC PATCH glibc 9/12] mach: Look for mach_i386.defs on x86_64 too Sergey Bugaev
2023-02-12 15:07 ` Samuel Thibault
2023-02-12 15:38 ` Sergey Bugaev
2023-02-12 15:46 ` Samuel Thibault
2023-02-12 16:01 ` Sergey Bugaev
2023-02-16 20:22 ` Joseph Myers
2023-02-12 11:10 ` [RFC PATCH glibc 10/12] hurd: Set up the basic tree for x86_64-gnu Sergey Bugaev
2023-02-12 15:15 ` Samuel Thibault
2023-02-12 11:10 ` [RFC PATCH glibc 11/12] hurd, htl: Add some x86_64-specific code Sergey Bugaev
2023-02-12 16:11 ` Samuel Thibault
2023-02-12 16:25 ` Sergey Bugaev
2023-02-12 16:36 ` Samuel Thibault
2023-02-12 16:40 ` Florian Weimer
2023-02-12 16:46 ` Samuel Thibault
2023-02-12 19:29 ` Florian Weimer
2023-02-12 19:37 ` Samuel Thibault
2023-02-12 16:51 ` Sergey Bugaev
2023-02-12 17:02 ` Samuel Thibault
2023-02-12 17:06 ` Sergey Bugaev
2023-02-12 11:10 ` [RFC PATCH glibc 12/12] C11 thrd: Downgrade the default alignment of mtx_t Sergey Bugaev
2023-02-12 15:18 ` Samuel Thibault
2023-02-12 15:52 ` Sergey Bugaev
2023-02-12 16:29 ` Samuel Thibault
2023-02-12 16:12 ` [RFC PATCH 0/12] Towards glibc on x86_64-gnu Samuel Thibault
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20230212111044.610942-2-bugaevc@gmail.com \
--to=bugaevc@gmail.com \
--cc=bug-hurd@gnu.org \
--cc=flaviocruz@gmail.com \
--cc=libc-alpha@sourceware.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
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).