Web lists-archives.com

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

On 03/12/2018 03:28 PM, Mikhail Usenko via cygwin wrote:

Test case 1:
$ realpath //


Expected output: /


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