public inbox for libc-alpha@sourceware.org
 help / color / mirror / Atom feed
From: James Tirta Halim <tirtajames45@gmail.com>
To: libc-alpha@sourceware.org
Cc: James Tirta Halim <tirtajames45@gmail.com>
Subject: [PATCH] ftw.c: Check for "." and ".." branchlessly
Date: Sun, 24 Sep 2023 13:45:16 +0700	[thread overview]
Message-ID: <20230924064516.17398-1-tirtajames45@gmail.com> (raw)

---
 io/ftw.c | 25 ++++++++++++++++++++++---
 1 file changed, 22 insertions(+), 3 deletions(-)

diff --git a/io/ftw.c b/io/ftw.c
index a72c7d5171..742369a2d2 100644
--- a/io/ftw.c
+++ b/io/ftw.c
@@ -66,6 +66,7 @@ char *alloca ();
 #include <unistd.h>
 #include <not-cancel.h>
 #include <sys/param.h>
+#include <endian.h>
 #ifdef _LIBC
 # include <include/sys/stat.h>
 #else
@@ -388,6 +389,25 @@ open_dir_stream (int *dfdp, struct ftw_data *data, struct dir_data *dirp)
 }
 
 
+#if __BYTE_ORDER == __LITTLE_ENDIAN
+#  define SH <<
+#else
+#  define SH >>
+#endif
+
+static int
+is_relative (const char *fname)
+{
+	typedef uint16_t w2 __attribute__ ((__may_alias__));
+	typedef uint32_t w4 __attribute__ ((__may_alias__));
+	const uint16_t n1 = '.' | '\0' SH 8;
+	const uint32_t n2 = '.' | '.' SH 8 | '\0' SH 16;
+	return (*(w2 *)fname == n1) | ((*(w4 *)fname | '\0' SH 24) == n2);
+}
+
+#undef SH
+
+
 static int
 process_entry (struct ftw_data *data, struct dir_data *dir, const char *name,
 	       size_t namlen, int d_type)
@@ -397,9 +417,8 @@ process_entry (struct ftw_data *data, struct dir_data *dir, const char *name,
   int flag = 0;
   size_t new_buflen;
 
-  if (name[0] == '.' && (name[1] == '\0'
-			 || (name[1] == '.' && name[2] == '\0')))
-    /* Don't process the "." and ".." entries.  */
+  /* Don't process the "." and ".." entries.  */
+  if (is_relative(name))
     return 0;
 
   new_buflen = data->ftw.base + namlen + 2;
-- 
2.42.0


             reply	other threads:[~2023-09-24  6:49 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-09-24  6:45 James Tirta Halim [this message]
2023-09-24 11:29 ` Xi Ruoyao
2023-09-24 11:43   ` Xi Ruoyao
     [not found] <CANDqPp0yVgx8hdeL9JUQgyMSww2MW7QDdPERC2Mxp5vdCOoOog@mail.gmail.com>
2023-09-24 13:14 ` Xi Ruoyao
2023-09-24 17:16 Wilco Dijkstra
2023-09-25  0:17 ` Paul Eggert
2023-09-25  6:26   ` Alexander Monakov
2023-09-25  6:45     ` Alexander Monakov
2023-09-25  7:10     ` Paul Eggert
2023-09-25  7:15       ` Andrew Pinski
2023-09-25  7:21         ` Andrew Pinski
2023-09-25  7:32         ` Paul Eggert
2023-09-25  7:32       ` Alexander Monakov
2023-09-25  8:10         ` Paul Eggert

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=20230924064516.17398-1-tirtajames45@gmail.com \
    --to=tirtajames45@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).