From patchwork Sun Sep 24 06:45:16 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: James Tirta Halim X-Patchwork-Id: 76613 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 32C393857026 for ; Sun, 24 Sep 2023 06:49:27 +0000 (GMT) X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-oi1-x233.google.com (mail-oi1-x233.google.com [IPv6:2607:f8b0:4864:20::233]) by sourceware.org (Postfix) with ESMTPS id 501993857342 for ; Sun, 24 Sep 2023 06:49:14 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 501993857342 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=gmail.com Received: by mail-oi1-x233.google.com with SMTP id 5614622812f47-3ae35773a04so1114417b6e.0 for ; Sat, 23 Sep 2023 23:49:14 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1695538153; x=1696142953; darn=sourceware.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=NhA/iKUXG3Knnk7fBKjFj70SZEsRmzoILEunxAp4B5M=; b=gEKPcjoulamLPU+JOnNBZxxdLu2n9pBFmhqSZAKjfzDpku2DhNUr1MeeSvB0F+w7rm 4n/UhiFZcicltxEK5E+zDGPHGrTzfDZu5Zzg1JsP0gTs3DhM6TwZdQAdqSFkUZAA7SAe fHVCbf+6hQiSjPEyv344prHHMEKxPr3np7IIDmSECJGj/RbHnztaeqMu8ikVirLKRgO7 WTTO0WJNyE/zApw6JDBZXIaCRVXJj6v/bm1hXp0LHaochIwrKlnZ/BWpG0W1IKo8zDgU F0p+y7fFbu/g9bUnxLOSpL9rYKbMQ+LI5Pt0phDR0aRTw2FffkC4kvvoIu0HB10MGpLz 5IgA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1695538153; x=1696142953; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=NhA/iKUXG3Knnk7fBKjFj70SZEsRmzoILEunxAp4B5M=; b=JPxN25+xEOuDfFbCZ/TjBt676nqBL4MpbdOFIfiWAm2woS8jWFBx9Sw246ywdX7/3c fX1E+UOuPULf7diEBtSUwQRFv7iz1y2TVGoz6covZS6cc6TLrbSEUsiIA9u290WxDSly OVM3AUB6oeywX9233RDU+bWQ8BLrY1KNlrnLweqchMPJpjLNKlg96BYxWKZzlE2goofc Ar6Y71295Fl349G8fJ5umEwTp0IAb+O4rNJ5wvPwDc+7658ta3X9BO94o6UAy+hRdTFz 5F6fMyi/dkzD51OAzH6vVio1/iQanViP/vKSzdLVwNWMJVR5uD0IDpsnK+FZt/RB21IV 0yow== X-Gm-Message-State: AOJu0Ywl4BoEQfTKBOIL1gf5hC6J3NrABmI13T2bsbidkg3mqDwg5TBI cY7o+LmLqOMeJ6zwCp+qqkDiSxzFYztTHA== X-Google-Smtp-Source: AGHT+IEV0b/15rt5GrGhG0LF/bvZW/HFQ+gLKTPn9XyrOHSnebLZKu9OKiFxv/UoCENnEPTuvXSebQ== X-Received: by 2002:a05:6808:23cc:b0:3a9:307a:62aa with SMTP id bq12-20020a05680823cc00b003a9307a62aamr5895491oib.22.1695538152847; Sat, 23 Sep 2023 23:49:12 -0700 (PDT) Received: from localhost.localdomain ([2001:448a:20a0:8e97:e977:ed1e:d425:86dd]) by smtp.gmail.com with ESMTPSA id h11-20020a170902f7cb00b001acae9734c0sm6335412plw.266.2023.09.23.23.49.11 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 23 Sep 2023 23:49:12 -0700 (PDT) From: James Tirta Halim To: libc-alpha@sourceware.org Cc: James Tirta Halim Subject: [PATCH] ftw.c: Check for "." and ".." branchlessly Date: Sun, 24 Sep 2023 13:45:16 +0700 Message-ID: <20230924064516.17398-1-tirtajames45@gmail.com> X-Mailer: git-send-email 2.42.0 MIME-Version: 1.0 X-Spam-Status: No, score=-12.4 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_ENVFROM_END_DIGIT, FREEMAIL_FROM, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libc-alpha-bounces+patchwork=sourceware.org@sourceware.org --- 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 #include #include +#include #ifdef _LIBC # include #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;