From 25f8aa5cad87ab716fb480d50ee40c38609c78ce Mon Sep 17 00:00:00 2001 From: Daniel Shahaf Date: Sat, 21 Mar 2020 19:09:04 +0000 Subject: [PATCH 6/8] Don't use xsymlinks() in 'whence -s'. --- Src/utils.c | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/Src/utils.c b/Src/utils.c index 6ad028c6b..567df9222 100644 --- a/Src/utils.c +++ b/Src/utils.c @@ -910,7 +910,14 @@ slashsplit(char *s) return r; } -/* expands symlinks and .. or . expressions */ +/* expands symlinks and .. or . expressions + * + * Puts the result in the global "xbuf" + * + * If "full" is true, resolve one level of symlinks only. + * + * WARNING: This will segfault on symlink loops (thread: workers/45282) + */ /**/ static int @@ -1041,10 +1048,10 @@ void print_if_link(char *s, int all) { if (*s == '/') { - *xbuf = '\0'; if (all) { char *start = s + 1; char xbuflink[PATH_MAX+1]; + *xbuf = '\0'; for (;;) { if (xsymlinks(start, 0) > 0) { printf(" -> "); @@ -1059,8 +1066,11 @@ print_if_link(char *s, int all) } } } else { - if (xsymlinks(s + 1, 1) > 0) - printf(" -> "), zputs(*xbuf ? xbuf : "/", stdout); + if (chrealpath(&s, 'P', 0)) { + printf(" -> "); + zputs(*s ? s : "/", stdout); + zsfree(s); + } } } }