[bug] coreutils: potentially dangerous: $(realpath //) != /

Eric Blake eblake@redhat.com
Mon Mar 12 20:47:00 GMT 2018


On 03/12/2018 03:28 PM, Mikhail Usenko via cygwin wrote:
> coreutils-8.26-2
> cygwin-2.10.0-1
> 
> Test case 1:
> -------------------
> $ realpath //
> //

Correct.

> -------------------
> Expected output: /

Wrong.

On cygwin, '/' and '//' are two different directories, as allowed by 
POSIX. Converting // into / is a violation of the POSIX requirements on 
pathname resolution on systems where // is distinct from /.

> 
> Test case 2:
> -------------------
> $ MYDIR=//
> $ test "$(realpath -e "$MYDIR" )" != /  &&  echo rm -rf "$MYDIR"/*

Wrong.  If you want to guarantee that an arbitrary variable concatenated 
with / and then a glob expands into the expected directory, then YOU 
have to prefilter the arbitrary variable to make sure it does not 
consist solely of slashes.  Or, if you don't want to remove anything 
from the special // directory, you can guarantee that your rm attempt 
will operate on / by supplying more than one slash yourself, as in rm 
-rf "$MYDIR"///* (if MYDIR is empty, that is equivalent to '/*'; if 
MYDIR is non-empty but not //, then you are guaranteed that the * 
expands relative to MYDIR, and only when MYDIR is exactly // do you fail 
to glob what was expected).

At any rate, realpath is not broken, but only your expectations.

-- 
Eric Blake, Principal Software Engineer
Red Hat, Inc.           +1-919-301-3266
Virtualization:  qemu.org | libvirt.org

--
Problem reports:       http://cygwin.com/problems.html
FAQ:                   http://cygwin.com/faq/
Documentation:         http://cygwin.com/docs.html
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple



More information about the Cygwin mailing list