diff options
| author | Rich Felker <dalias@aerifal.cx> | 2017-10-13 23:08:21 -0400 | 
|---|---|---|
| committer | Rich Felker <dalias@aerifal.cx> | 2017-10-13 23:08:21 -0400 | 
| commit | 5b5eb527c5ed5ca2786bf82892a04ca3bdf33d31 (patch) | |
| tree | cd31c6d2edee353dc6c8f72507faf091e2566cd6 /src | |
| parent | c1e27367a9b26b9baac0f37a12349fc36567c8b6 (diff) | |
| download | musl-5b5eb527c5ed5ca2786bf82892a04ca3bdf33d31.tar.gz | |
fix incorrect base name offset from nftw when pathname ends in slash(es)
the rightmost '/' character is not necessarily the delimiter before
the basename; it could be a spurious trailing character on the
directory name.
this change does not introduce any normalization of pathnames or
stripping of trailing slashes, contrary to at least glibc and perhaps
other implementations; it jusst prevents their presence from breaking
things. whether further changes should be made is an open question
that may depend on conformance and/or application compatibility
considerations.
based loosely on patch by Joakim Sindholt.
Diffstat (limited to 'src')
| -rw-r--r-- | src/misc/nftw.c | 12 | 
1 files changed, 9 insertions, 3 deletions
diff --git a/src/misc/nftw.c b/src/misc/nftw.c index efb2b895..eb9014bc 100644 --- a/src/misc/nftw.c +++ b/src/misc/nftw.c @@ -28,7 +28,6 @@ static int do_nftw(char *path, int (*fn)(const char *, const struct stat *, int,  	int type;  	int r;  	struct FTW lev; -	char *name;  	if ((flags & FTW_PHYS) ? lstat(path, &st) : stat(path, &st) < 0) {  		if (!(flags & FTW_PHYS) && errno==ENOENT && !lstat(path, &st)) @@ -53,10 +52,17 @@ static int do_nftw(char *path, int (*fn)(const char *, const struct stat *, int,  	new.dev = st.st_dev;  	new.ino = st.st_ino;  	new.level = h ? h->level+1 : 0; -	new.base = l+1; +	new.base = j+1;  	lev.level = new.level; -	lev.base = h ? h->base : (name=strrchr(path, '/')) ? name-path : 0; +	if (h) { +		lev.base = h->base; +	} else { +		size_t k; +		for (k=j; k && path[k]=='/'; k--); +		for (; k && path[k-1]!='/'; k--); +		lev.base = k; +	}  	if (!(flags & FTW_DEPTH) && (r=fn(path, &st, type, &lev)))  		return r;  | 
