From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 27053 invoked by alias); 3 Jun 2018 16:30:52 -0000 Mailing-List: contact zsh-workers-help@zsh.org; run by ezmlm Precedence: bulk X-No-Archive: yes List-Id: Zsh Workers List List-Post: List-Help: List-Unsubscribe: X-Seq: 42923 Received: (qmail 8358 invoked by uid 1010); 3 Jun 2018 16:30:52 -0000 X-Qmail-Scanner-Diagnostics: from rcpt-mqugw.biglobe.ne.jp by f.primenet.com.au (envelope-from , uid 7791) with qmail-scanner-2.11 (clamdscan: 0.99.2/21882. spamassassin: 3.4.1. Clear:RC:0(133.208.100.2):SA:0(-2.6/5.0):. Processed in 1.312457 secs); 03 Jun 2018 16:30:52 -0000 X-Spam-Checker-Version: SpamAssassin 3.4.1 (2015-04-28) on f.primenet.com.au X-Spam-Level: X-Spam-Status: No, score=-2.6 required=5.0 tests=BAYES_00,RCVD_IN_DNSWL_LOW, SPF_PASS autolearn=ham autolearn_force=no version=3.4.1 X-Envelope-From: takimoto-j@kba.biglobe.ne.jp X-Qmail-Scanner-Mime-Attachments: | X-Qmail-Scanner-Zip-Files: | X-Biglobe-Sender: From: "Jun T." Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: quoted-printable Mime-Version: 1.0 (Mac OS X Mail 10.3 \(3273\)) Subject: Re: [Bug] Strange Globing Behaviour when used with sudo Date: Mon, 4 Jun 2018 01:30:14 +0900 References: <20180530202349.GA10754@osmium.lan> <20180531094403.05b62bee@camnpupstephen.cam.scsc.local> <20180531084938eucas1p19a854d9e9ea17428cd6549f56a283356~zroN76sl33019130191eucas1p1K@eucas1p1.samsung.com> <3977A049-90E6-4EDD-9E4C-8D2FF38593A3@kba.biglobe.ne.jp> <20180531152941eucas1p2c45927fa47f727224e2da98b4f6a7604~zxFgS-BM50307003070eucas1p2G@eucas1p2.samsung.com> <20180531181546.osffv3ysjzaxjfid@Daniels-MacBook-Air.local> <01B9BB58-DDAE-4056-AD9A-2367DC3AC20D@kba.biglobe.ne.jp> <20180602181719.vzcnij3fvx7eueuk@Daniels-MacBook-Air.local> To: zsh-workers@zsh.org In-Reply-To: <20180602181719.vzcnij3fvx7eueuk@Daniels-MacBook-Air.local> Message-Id: <7D035B65-A2BA-49F3-807D-A387FF4FE6DB@kba.biglobe.ne.jp> X-Mailer: Apple Mail (2.3273) X-Biglobe-Spnum: 64598 Do we need to fix (or workaround) this problem, given that very basic commands like "ls" are also broken on macOS? > 2018/06/03 03:17, Daniel Tameling wrote: >=20 > $ sudo zsh -c 'echo */../ > file/../ Thanks. If we really need to workaround this bug, probably the simplest way=20 would be not to add a file to pathbuf as in the patch below. I couldn't put all the patch within a single "#ifdef __APPLE__" because the return value of addpath() is changed to int. > Btw: > I noticed bash manages to trigger the ls error message when zsh > outputs "no matches found"=20 > $ bash -c 'ls */..' > ls: cannot access '*/..': No such file or directory Try 'setopt no_nomatch'. See the option NOMATCH in zshoptions(1). diff --git a/Src/glob.c b/Src/glob.c index 66a95329f..a36d76ac1 100644 --- a/Src/glob.c +++ b/Src/glob.c @@ -259,11 +259,28 @@ struct complist { /* Add a component to pathbuf: This keeps track of how * * far we are into a file name, since each path component * * must be matched separately. */ +#ifdef __APPLE__ +#define BROKEN_STAT +/* + * Workaround for broken stat()/lstat()/access() on macOS. + * If called by root, these syscalls mistakenly returns success for = paths + * like "foo/bar/.", "foo/bar/..", "foo/bar/../" etc. even if bar is a = file. + * This causes statfullpath() to also succeed for these paths. + * To workaround this problem, we do not add s to pathbuf if it is not = a + * directory and return -1 instead. + * + * On systems other than macOS, always add s to pathbuf and return 0. + */ +#endif =20 /**/ -static void +static int addpath(char *s, int l) { +#ifdef BROKEN_STAT + struct stat stbuf; + int oppos =3D pathpos; +#endif DPUTS(!pathbuf, "BUG: pathbuf not initialised"); while (pathpos + l + 1 >=3D pathbufsz) pathbuf =3D zrealloc(pathbuf, pathbufsz *=3D 2); @@ -271,6 +288,14 @@ addpath(char *s, int l) pathbuf[pathpos++] =3D *s++; pathbuf[pathpos++] =3D '/'; pathbuf[pathpos] =3D '\0'; +#ifdef BROKEN_STAT + if (stat(unmeta(pathbuf), &stbuf) || !S_ISDIR(stbuf.st_mode)) { + pathbuf[pathpos =3D oppos] =3D '\0'; + return -1; + } + else +#endif + return 0; } =20 /* stat the filename s appended to pathbuf. l should be true for = lstat, * @@ -531,9 +556,12 @@ scanner(Complist q, int shortcircuit) sr.st_dev !=3D sc.st_dev); } } - if (add) { - addpath(str, l); - if (!closure || !statfullpath("", NULL, 1)) { + if (add && !addpath(str,l)) { + if (!closure +#ifndef BROKEN_STAT + || !statfullpath("", NULL, 1) +#endif + ) { scanner((q->closure) ? q : q->next, = shortcircuit); if (shortcircuit && shortcircuit =3D=3D matchct) return; @@ -650,15 +678,17 @@ scanner(Complist q, int shortcircuit) =20 for (fn =3D subdirs; fn < subdirs+subdirlen; ) { int l =3D strlen(fn); - addpath(fn, l); + int fn_is_dir =3D !addpath(fn, l); fn +=3D l + 1; memcpy((char *)&errsfound, fn, sizeof(int)); fn +=3D sizeof(int); - /* scan next level */ - scanner((q->closure) ? q : q->next, shortcircuit);=20 - if (shortcircuit && shortcircuit =3D=3D matchct) - return; - pathbuf[pathpos =3D oppos] =3D '\0'; + if (fn_is_dir) { + /* scan next level */ + scanner((q->closure) ? q : q->next, shortcircuit);=20= + if (shortcircuit && shortcircuit =3D=3D matchct) + return; + pathbuf[pathpos =3D oppos] =3D '\0'; + } } hrealloc(subdirs, subdirlen, 0); }