Subject: bug#21454: 25.0.50; `parse-colon-path' fails with paths containing consecutive directory separators

From: Tino Calancha <[email-masked]>
Date: Thu, 10 Sep 2015 20:15:42 +0900 (JST)

Implementation use `substitute-in-file-name' which substitute environment variables but 
also drop all in front of consecutive directory separators (up to the last 
of those dir. separators). The documentation of `parse-colon-path' dont 
mention about that behaviour.

In unix systems there is no difference between:

I)  /foo//bar/baz
II) /foo/bar/baz

`substitute-in-file-name' would translate I) into:
/bar/baz

In case path to parse contains an environment variable:
FOO=/foo/bar
BAZ=/foo/bar/

A) path="$FOO/bin" ; works (FOO is a directory file name)
B) path="$BAZ/bin" ; fails (FOO is not a directory file name)

Observed same behaviour on this fuction in emacs versions: 19, 20, 21, 22, 
23, 24 and 25.
Attached patch calls `substitute-env-in-file-name' instead of 
`substitute-in-file-name' and it returns same values for 
inputs I) and II) or A) and B).

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
In *scratch* buffer after started session with:
emacs -Q

(let* ((foo "/foo/bar//baz/:/qux/quux/corge//")) ; '//'
   (setenv "FOO" foo)
   (parse-colon-path (getenv "FOO")))
("/baz/" "/")

(let ((foo "/foo/bar/baz/qux/:/quux/corge/:/grault/graply/waldo")) ; `ok'
   (setenv "FOO" foo)
   (parse-colon-path "$FOO"))
("/foo/bar/baz/qux/:/quux/corge/:/grault/graply/waldo/")

(let ((foo "/foo/bar/baz/qux/:/quux/corge/:/grault/graply/waldo/")) ; end with '/' and `parse-colon-path' add another
   (setenv "FOO" foo)
   (parse-colon-path "$FOO"))
("/")

(let ((foo "/foo/bar/baz/qux/:/quux/corge/:/grault/graply/waldo/")) ; `getenv' makes it work somehow
   (setenv "FOO" foo)
   (parse-colon-path (getenv "FOO")))
("/foo/bar/baz/qux/" "/quux/corge/" "/grault/graply/waldo/")

(let ((foo "/foo/bar/baz//qux/:/quux/corge/:/grault/garply/waldo/")) ; `getenv' dont help with '//'
   (setenv "FOO" foo)
   (parse-colon-path (getenv "FOO")))
("/qux/" "/quux/corge/" "/grault/garply/waldo/")

(let ((foo "/foo/bar/baz//qux/:/quux/corge/:/grault/garply/waldo")) ; '//'
   (setenv "FOO" foo)
   (parse-colon-path "$FOO"))
("/qux/:/quux/corge/:/grault/garply/waldo/")

(let* ((foo "/foo/bar")                    ; `ok' foo not end with '/'
        (bar "$FOO/baz/qux/:/quux/corge/"))
   (setenv "FOO" foo)
   (setenv "BAR" bar)
   (parse-colon-path (getenv "BAR")))
("/foo/bar/baz/qux/" "/quux/corge/")

(let* ((foo "/foo/bar/")                   ; foo ends with '/'
 	   (bar "$FOO/baz/qux/:/quux/corge/"))
   (setenv "FOO" foo)
   (setenv "BAR" bar)
   (parse-colon-path (getenv "BAR")))
("/baz/qux/" "/quux/corge/")

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

In GNU Emacs 25.0.50.1 (x86_64-unknown-linux-gnu, GTK+ Version 2.24.23)
  of 2015-09-10
Repository revision: f962c5d6472804f788cdd54631bcc46aab5f59ce
Windowing system distributor 'Scientific Linux', version 11.0.11500000
System Description:	Scientific Linux release 6.7 (Carbon)

Configured using:
  'configure --without-makeinfo --with-gif=no'

Configured features:
XPM JPEG TIFF PNG IMAGEMAGICK SOUND DBUS GCONF NOTIFY LIBSELINUX LIBXML2
FREETYPE XFT ZLIB TOOLKIT_SCROLL_BARS GTK2 X11

Important settings:
   value of $LANG: en_US.utf8
   value of $XMODIFIERS: @im=none
   locale-coding-system: utf-8-unix

Major mode: Lisp Interaction

Minor modes in effect:
   tooltip-mode: t
   global-eldoc-mode: t
   electric-indent-mode: t
   mouse-wheel-mode: t
   tool-bar-mode: t
   menu-bar-mode: t
   file-name-shadow-mode: t
   global-font-lock-mode: t
   font-lock-mode: t
   blink-cursor-mode: t
   auto-composition-mode: t
   auto-encryption-mode: t
   auto-compression-mode: t
   line-number-mode: t
   transient-mark-mode: t

Recent messages:
For information about GNU Emacs and the GNU system, type C-h C-a.
<backtab> is undefined
Quit
completing-read-default: Command attempted to use minibuffer while in minibuffer
user-error: Beginning of history; no preceding item
Quit [2 times]

Load-path shadows:
None found.

Features:
(shadow sort gnus-util mail-extr emacsbug message dired format-spec
rfc822 mml mml-sec mm-decode mm-bodies mm-encode mail-parse rfc2231
mailabbrev gmm-utils mailheader sendmail rfc2047 rfc2045 ietf-drums
mm-util help-fns help-mode easymenu cl-loaddefs pcase cl-lib mail-prsvr
mail-utils time-date mule-util tooltip eldoc electric uniquify
ediff-hook vc-hooks lisp-float-type mwheel x-win term/common-win x-dnd
tool-bar dnd fontset image regexp-opt fringe tabulated-list newcomment
elisp-mode lisp-mode prog-mode register page menu-bar rfn-eshadow timer
select scroll-bar mouse jit-lock font-lock syntax facemenu font-core
frame cl-generic cham georgian utf-8-lang misc-lang vietnamese tibetan
thai tai-viet lao korean japanese eucjp-ms cp51932 hebrew greek romanian
slovak czech european ethiopic indian cyrillic chinese charscript
case-table epa-hook jka-cmpr-hook help simple abbrev minibuffer
cl-preloaded nadvice loaddefs button faces cus-face macroexp files
text-properties overlay sha1 md5 base64 format env code-pages mule
custom widget hashtable-print-readable backquote dbusbind inotify
dynamic-setting system-font-setting font-render-setting move-toolbar gtk
x-toolkit x multi-tty make-network-process emacs)

Memory information:
((conses 16 80421 12421)
  (symbols 48 18940 0)
  (miscs 40 39 110)
  (strings 32 12495 3997)
  (string-bytes 1 361986)
  (vectors 16 11158)
  (vector-slots 8 408984 7901)
  (floats 8 131 123)
  (intervals 56 192 0)
  (buffers 976 12)
  (heap 1024 26696 621))

--- files.el	2015-09-10 19:31:06.569650962 +0900
+++ files_patched.el	2015-09-10 20:01:00.303650748 +0900
@@ -667,7 +667,8 @@
   "Value of the CDPATH environment variable, as a list.
 Not actually set up until the first time you use it.")
 
-(defun parse-colon-path (search-path)
+(require 'env)
+(defun myparse-colon-path2 (search-path)
   "Explode a search path into a list of directory names.
 Directories are separated by `path-separator' (which is colon in
 GNU and Unix systems).  Substitute environment variables into the
@@ -675,10 +676,12 @@
 a leading or trailing separator, or two adjacent separators), return
 nil (meaning `default-directory') as the associated list element."
   (when (stringp search-path)
-    (mapcar (lambda (f)
-	      (if (equal "" f) nil
-		(substitute-in-file-name (file-name-as-directory f))))
-	    (split-string search-path path-separator))))
+    (let* ((path-list        (mapcar 'substitute-env-vars (split-string search-path path-separator)))
+           (search-path-sub  (mapconcat 'identity path-list path-separator)))
+      (mapcar (lambda (f)
+                (if (equal "" f) nil
+                  (expand-file-name (file-name-as-directory f))))
+              (split-string search-path-sub path-separator)))))
 
 (defun cd-absolute (dir)
   "Change current directory to given absolute file name DIR."