1
0
Fork 0
mirror of git://git.code.sf.net/p/cdesktopenv/code synced 2025-03-09 15:50:02 +00:00

Skip '.' and '..' when globbing patterns like .*

There are convincing arguments why including '.' and '..' in the
result of pathname expansion is actively harmful. See:
https://www.austingroupbugs.net/view.php?id=1228
https://github.com/ksh93/ksh/issues/58#issuecomment-653716846

pdksh, mksh and zsh already skip these special traversal names
in all cases. This commit makes ksh act like these shells.

Since passing '.' and especially '..' as arguments to commands like
'chmod -R' and 'cp -r' may cause harm, this change seems likely to
fix more legacy scripts than it breaks. I'm unaware of anyone ever
having come up with a concrete use case for the old behaviour.

This change also fixes the bug that '.' and '..' failed to be
ignored as documented if FIGNORE is set.

src/lib/libast/misc/glob.c: glob_dir():
- Explicitly skip any matching '.' and '..' in all cases.

src/cmd/ksh93/tests/glob.sh:
- Add test_glob() tests for '*' and '.*'.

src/cmd/ksh93/sh.1: File Name Generation:
- Update to match new behaviour.

Resolves: https://github.com/ksh93/ksh/issues/58
This commit is contained in:
Martijn Dekker 2020-08-10 00:02:23 +01:00
parent be5ea8bbb2
commit 5312a59d5a
5 changed files with 25 additions and 7 deletions

View file

@ -528,11 +528,13 @@ skip:
*restore2 = gp->gl_delim;
while ((name = (*gp->gl_dirnext)(gp, dirf)) && !*gp->gl_intr)
{
if (name[0] == '.' && (!name[1] || name[1] == '.' && !name[2]))
continue; /* do not ever match '.' or '..' */
if (notdir = (gp->gl_status & GLOB_NOTDIR))
gp->gl_status &= ~GLOB_NOTDIR;
if (ire && !regexec(ire, name, 0, NiL, 0))
continue;
if (matchdir && (name[0] != '.' || name[1] && (name[1] != '.' || name[2])) && !notdir)
if (matchdir && !notdir)
addmatch(gp, prefix, name, matchdir, NiL, anymeta);
if (!regexec(pre, name, 0, NiL, 0))
{