diff --git a/ChangeLog b/ChangeLog index 8f8617ac..b247a16e 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,16 @@ 2007-12-04 James Youngman + Fix Savannah bug #20970, handling of trailing slashes with -name. + * find/pred.c (pred_name_common): Strip trailing slashes from the + pathname before applying fnmatch() to it. This fixes Savannah bug + #20970. + * find/testsuite/find.posix/nameslash.exp: Test case for bug #20970. + * find/testsuite/find.posix/nameslash.xo: Expected output file for + same. + * find/testsuite/Makefile.am (EXTRA_DIST_EXP): Added nameslash.exp. + (EXTRA_DIST_XO): Added nameslash.xo. + * NEWS: mention this fix. + Fix Savannah bug #15384, find misbehaves when parent directory is not readable. * find/find.c: Add enumeration member diff --git a/NEWS b/NEWS index 05df130b..87ee40d1 100644 --- a/NEWS +++ b/NEWS @@ -7,6 +7,10 @@ GNU findutils NEWS - User visible changes. -*- outline -*- (allout)* ** Bug Fixes +#20970: Trailing slash on directory arguments breaks -name. "find +foo/ -name foo" now correctly matches foo and printf foo/. See POSIX +interp http://www.opengroup.org/austin/interps/uploads/40/14959/AI-186.txt + #20751: Avoid memory corruption in find -ls that has been present since 4.2.28. diff --git a/find/pred.c b/find/pred.c index afcf3e56..cbd5e3c3 100644 --- a/find/pred.c +++ b/find/pred.c @@ -967,31 +967,32 @@ pred_ilname (char *pathname, struct stat *stat_buf, struct predicate *pred_ptr) /* Common code between -name, -iname. PATHNAME is being visited, STR is name to compare basename against, and FLAGS are passed to - fnmatch. */ + fnmatch. Recall that 'find / -name /' is one of the few times where a '/' + in the -name must actually find something. */ static boolean pred_name_common (const char *pathname, const char *str, int flags) { - /* Prefer last_component over base_name, to avoid malloc when - possible. */ - char *base = last_component (pathname); + char *p; + boolean b; + /* We used to use last_component() here, but that would not allow us + * to modify the input string, which is const. We could optimise by + * duplicating the string only if we need to modify it, and I'll do + * that if there is a measurable performance difference on a machine + * built after 1990... + */ + char *base = base_name (pathname); + /* remove trailing slashes, but leave "/" or "//foo" unchanged. */ + strip_trailing_slashes(base); - /* base is empty only if pathname is a file system root. But recall - that 'find / -name /' is one of the few times where a '/' in the - -name must actually find something. */ - if (!*base) - { - boolean b; - base = base_name (pathname); - b = fnmatch (str, base, flags) == 0; - free (base); - return b; - } /* FNM_PERIOD is not used here because POSIX requires that it not be. * See http://standards.ieee.org/reading/ieee/interp/1003-2-92_int/pasc-1003.2-126.html */ - return fnmatch (str, base, flags) == 0; + b = fnmatch (str, base, flags) == 0; + free (base); + return b; } + boolean pred_iname (char *pathname, struct stat *stat_buf, struct predicate *pred_ptr) { diff --git a/find/testsuite/Makefile.am b/find/testsuite/Makefile.am index 0c9c4dd9..987c47a2 100644 --- a/find/testsuite/Makefile.am +++ b/find/testsuite/Makefile.am @@ -63,6 +63,7 @@ find.posix/sv-bug-12181.xo \ find.posix/depth1.xo \ find.posix/sizes.xo \ find.posix/name.xo \ +find.posix/nameslash.xo \ find.posix/parent.xo \ find.posix/perm-vanilla.xo \ find.posix/prune.xo \ @@ -133,6 +134,7 @@ find.posix/sv-bug-11175.exp \ find.posix/sv-bug-12181.exp \ find.posix/depth1.exp \ find.posix/name.exp \ +find.posix/nameslash.exp \ find.posix/sizes.exp \ find.posix/parent.exp \ find.posix/perm-vanilla.exp \ diff --git a/find/testsuite/find.posix/nameslash.exp b/find/testsuite/find.posix/nameslash.exp new file mode 100644 index 00000000..1aaf2206 --- /dev/null +++ b/find/testsuite/find.posix/nameslash.exp @@ -0,0 +1,7 @@ +# tests for -name and trailing slashes. +# See http://www.opengroup.org/austin/interps/uploads/40/14959/AI-186.txt +# This is a test for Savannah bug #20970. +exec rm -rf tmp +exec mkdir tmp tmp/foo tmp/bar +find_start p {tmp/foo/// tmp/bar/// -name foo -o -name 'bar?*'} +exec rm -rf tmp diff --git a/find/testsuite/find.posix/nameslash.xo b/find/testsuite/find.posix/nameslash.xo new file mode 100644 index 00000000..e94ef73f --- /dev/null +++ b/find/testsuite/find.posix/nameslash.xo @@ -0,0 +1 @@ +tmp/foo///