[PATCH 3/9] posix: Allow glob to match dangling symlinks [BZ #866]

Zack Weinberg zackw@panix.com
Mon Sep 11 17:56:00 GMT 2017


On Mon, Sep 11, 2017 at 1:38 PM, Paul Eggert <eggert@cs.ucla.edu> wrote:
> On 09/11/2017 10:25 AM, Zack Weinberg wrote:
>>
>>
>> It should be enough to make a dummy call, e.g.
>>
>>   pglob->gl_lstat(".", &statbuf);
>>
>
> Unfortunately calling lstat is quite expensive on some (non-POSIX)
> platforms, even on the working directory. So we can't do the above in the
> Gnulib version.

I have trouble believing this will be a measurable performance hit,
considering how much other expensive work glob has to do.

> Besides, under the proposed patch glob is going to use
> gl_lstat instead of gl_stat in almost all cases, so the dummy call won't add
> much extra checking.

The point is not to add _extra_ checking; the point is to ensure that
gl_lstat (and gl_stat) are valid on all calls, _even if_ they wouldn't
otherwise have been used.  I'm trying to turn "may fail at runtime
under rare circumstances" into "will definitely fail at runtime on the
first use", which is the best we can do in C.

> I suppose we could valid gl_stat instead, as gl_stat usage will become rare
> (used only if GLOB_MARK is also specified, just before returning results).
> But we don't have any code in the wild that is giving us invalid gl_stat
> pointers, so it wouldn't be that helpful to try to validate gl_stat either.

So here's an alternative, less thorough but perhaps also less costly
approach: when GLOB_ALTDIRFUNCS is set, call both gl_stat and gl_lstat
on the first name that's going to be returned, even if we have no
other reason to do this.  Optionally, memoize the function pointer and
don't bother making the extra call again if we recognize that it's
known to work.

(Maybe also it would be a good idea to check up front for any NULL
callbacks in the ALTDIRFUNCS case.)

zw



More information about the Libc-alpha mailing list