From: Paul Eggert Date: Wed, 2 Dec 2020 22:25:43 +0000 (-0800) Subject: canonicalize: refactor can_mode flag X-Git-Tag: v1.0~3431 X-Git-Url: https://gitweb.git.savannah.gnu.org/gitweb/?a=commitdiff_plain;h=fdc6f3f80ed6b2eb2777bfca11e94e19d639ed21;p=gnulib.git canonicalize: refactor can_mode flag * lib/canonicalize.c (MULTIPLE_BITS_SET): Remove, replacing with ... (multiple_bits_set): ... this new static function. Uses changed. (canonicalize_filename_mode): Refactor for clarity to avoid modifying the CAN_MODE argument. --- diff --git a/ChangeLog b/ChangeLog index 625bae7374..d5ee34f1cd 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,11 @@ 2020-12-02 Paul Eggert + canonicalize: refactor can_mode flag + * lib/canonicalize.c (MULTIPLE_BITS_SET): Remove, replacing with ... + (multiple_bits_set): ... this new static function. Uses changed. + (canonicalize_filename_mode): Refactor for clarity to avoid + modifying the CAN_MODE argument. + canonicalize: prefer signed integer types * lib/canonicalize.c: Include stddef.h, for ptrdiff_t. (seen_triple, canonicalize_filename_mode): Prefer signed to diff --git a/lib/canonicalize.c b/lib/canonicalize.c index e363cfef33..d12fc6b8ff 100644 --- a/lib/canonicalize.c +++ b/lib/canonicalize.c @@ -33,8 +33,6 @@ #include "xgetcwd.h" #include "filename.h" -#define MULTIPLE_BITS_SET(i) (((i) & ((i) - 1)) != 0) - /* In this file, we cannot handle file names longer than PATH_MAX. On systems with no file name length limit, use a fallback. */ #ifndef PATH_MAX @@ -65,6 +63,12 @@ canonicalize_file_name (const char *name) } #endif /* !HAVE_CANONICALIZE_FILE_NAME */ +static bool +multiple_bits_set (canonicalize_mode_t i) +{ + return (i & (i - 1)) != 0; +} + /* Return true if we've already seen the triple, . If *HT is not initialized, initialize it. */ static bool @@ -106,14 +110,12 @@ canonicalize_filename_mode (const char *name, canonicalize_mode_t can_mode) ptrdiff_t extra_len = 0; Hash_table *ht = NULL; int saved_errno; - int can_flags = can_mode & ~CAN_MODE_MASK; - bool logical = can_flags & CAN_NOLINKS; + bool logical = (can_mode & CAN_NOLINKS) != 0; int num_links = 0; ptrdiff_t prefix_len; - can_mode &= CAN_MODE_MASK; - - if (MULTIPLE_BITS_SET (can_mode)) + canonicalize_mode_t can_exist = can_mode & CAN_MODE_MASK; + if (multiple_bits_set (can_exist)) { errno = EINVAL; return NULL; @@ -276,7 +278,7 @@ canonicalize_filename_mode (const char *name, canonicalize_mode_t can_mode) indicate a loop. */ if (seen_triple (&ht, start, &st)) { - if (can_mode == CAN_MISSING) + if (can_exist == CAN_MISSING) continue; saved_errno = ELOOP; free (buf); @@ -334,7 +336,7 @@ canonicalize_filename_mode (const char *name, canonicalize_mode_t can_mode) free (buf); } - else if (can_mode != CAN_MISSING + else if (can_exist != CAN_MISSING && (!logical || readlink (rname, &discard, 1) < 0)) { saved_errno = errno; @@ -346,8 +348,8 @@ canonicalize_filename_mode (const char *name, canonicalize_mode_t can_mode) case ENOENT: /* RNAME does not exist. */ - if (can_mode == CAN_EXISTING - || (can_mode == CAN_ALL_BUT_LAST + if (can_exist == CAN_EXISTING + || (can_exist == CAN_ALL_BUT_LAST && end[strspn (end, SLASHES)])) goto error; break;