From 3a75362f0375f66ebe204c87313899ed81010731 Mon Sep 17 00:00:00 2001 From: Paul Eggert Date: Thu, 17 Dec 2020 01:13:40 -0800 Subject: [PATCH 3/4] canonicalize: omit second readlink when not needed * lib/canonicalize.c (canonicalize_filename_mode_stk): Omit second readlink when (can_exist != CAN_MISSING && startlen != 0 && !logical). Simplify. --- ChangeLog | 5 +++++ lib/canonicalize.c | 32 ++++++++------------------------ 2 files changed, 13 insertions(+), 24 deletions(-) diff --git a/ChangeLog b/ChangeLog index 58c7c16d7..fccedabe3 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,10 @@ 2020-12-17 Paul Eggert + canonicalize: omit second readlink when not needed + * lib/canonicalize.c (canonicalize_filename_mode_stk): + Omit second readlink when (can_exist != CAN_MISSING + && startlen != 0 && !logical). Simplify. + canonicalize: remove arbitrary 8192-byte limit Remove canonicalize.c’s arbitrary 8192-byte limit on platforms like GNU Hurd that do not define the PATH_MAX macro, and similarly diff --git a/lib/canonicalize.c b/lib/canonicalize.c index 04fae10a4..bcfd52be2 100644 --- a/lib/canonicalize.c +++ b/lib/canonicalize.c @@ -360,31 +360,15 @@ canonicalize_filename_mode_stk (const char *name, canonicalize_mode_t can_mode, dest++; } } - else if (can_exist != CAN_MISSING - && (startlen == 0 - ? stat (rname, &st) < 0 - : !logical && readlink (rname, &discard, 1) < 0)) - { - switch (errno) - { - case EINVAL: - case EOVERFLOW: /* Possible with stat. */ - /* RNAME exists and is not a symbolic link. */ - break; - - case ENOENT: - /* RNAME does not exist. */ - if (can_exist == CAN_EXISTING + else if (! (can_exist == CAN_MISSING + || (startlen == 0 + ? stat (rname, &st) == 0 || errno == EOVERFLOW + : ((logical && 0 <= readlink (rname, &discard, 1)) + || errno == EINVAL)) || (can_exist == CAN_ALL_BUT_LAST - && end[strspn (end, SLASHES)])) - goto error; - break; - - default: - /* Some other problem with RNAME. */ - goto error; - } - } + && errno == ENOENT + && !end[strspn (end, SLASHES)]))) + goto error; } } if (dest > rname + prefix_len + 1 && ISSLASH (dest[-1])) -- 2.27.0