* 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. --- ChangeLog | 6 ++++++ lib/canonicalize.c | 24 +++++++++++++----------- 2 files changed, 19 insertions(+), 11 deletions(-)
diff --git a/ChangeLog b/ChangeLog index 625bae737..d5ee34f1c 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,11 @@ 2020-12-02 Paul Eggert <egg...@cs.ucla.edu> + 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 e363cfef3..d12fc6b8f 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, <FILENAME, dev, ino>. 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; -- 2.27.0