unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
From: Ken Brown <kbrown@cornell.edu>
To: Eli Zaretskii <eliz@gnu.org>, "rms@gnu.org" <rms@gnu.org>
Cc: "dan@dpsutton.com" <dan@dpsutton.com>,
	"npostavs@gmail.com" <npostavs@gmail.com>,
	"36502@debbugs.gnu.org" <36502@debbugs.gnu.org>,
	"monnier@iro.umontreal.ca" <monnier@iro.umontreal.ca>,
	"schwab@suse.de" <schwab@suse.de>
Subject: bug#36502: Fwd: bug#36502: 27.0.50; infinite loop in file-name-case-insensitive-p
Date: Mon, 15 Jul 2019 13:39:24 +0000	[thread overview]
Message-ID: <09ed9fa5-efd8-93df-e4f1-dbd73cb1b823@cornell.edu> (raw)
In-Reply-To: <07659a69-b89e-51da-8bb3-adc32e1f39ae@cornell.edu>

[-- Attachment #1: Type: text/plain, Size: 356 bytes --]

On 7/12/2019 4:18 PM, Ken Brown wrote:
> On 7/12/2019 2:41 AM, Eli Zaretskii wrote:
>> It is only absolute if what follows ~ is a slash or a name of an
>> existing user.  I think we should fix the inconsistency in that
>> direction.
> 
> Patch attached.

Is the patch OK, Eli?  Here it is again, fleshed out with a commit message and test.

Ken

[-- Attachment #2: 0001-Fix-expand-file-name-for-names-starting-with.patch --]
[-- Type: text/plain, Size: 4952 bytes --]

From bdc13b7b64b2f1314b920ab57e6986d4cf866d14 Mon Sep 17 00:00:00 2001
From: Ken Brown <kbrown@cornell.edu>
Date: Mon, 15 Jul 2019 09:32:49 -0400
Subject: [PATCH] Fix expand-file-name for names starting with '~'

* src/fileio.c: (Fexpand_file_name): If the current buffer's
default-directory starts with "~user" where "user" is not a valid
user name, don't give the '~' a special meaning.  Just treat the
value of default-directory as a relative name.  (Bug#36502)
* test/src/fileio-tests.el
(fileio-tests--relative-default-directory): Add a test.
---
 src/fileio.c             | 68 ++++++++++++++++++++++++++--------------
 test/src/fileio-tests.el |  6 +++-
 2 files changed, 49 insertions(+), 25 deletions(-)

diff --git a/src/fileio.c b/src/fileio.c
index 7f83267956..4c7625cad4 100644
--- a/src/fileio.c
+++ b/src/fileio.c
@@ -737,6 +737,13 @@ DEFUN ("make-temp-name", Fmake_temp_name, Smake_temp_name, 1, 1, 0,
 				   empty_unibyte_string, Qnil);
 }
 
+/* NAME must be a string.  */
+static bool
+file_name_absolute_no_tilde_p (Lisp_Object name)
+{
+  return IS_ABSOLUTE_FILE_NAME (SSDATA (name));
+}
+
 DEFUN ("expand-file-name", Fexpand_file_name, Sexpand_file_name, 1, 2, 0,
        doc: /* Convert filename NAME to absolute, and canonicalize it.
 Second arg DEFAULT-DIRECTORY is directory to start with if NAME is relative
@@ -807,41 +814,54 @@ DEFUN ("expand-file-name", Fexpand_file_name, Sexpand_file_name, 1, 2, 0,
       error ("Invalid handler in `file-name-handler-alist'");
     }
 
+  /* As a last resort, we may have to use the root as
+     default_directory below.  */
+  Lisp_Object root;
+#ifdef DOS_NT
+      /* "/" is not considered a root directory on DOS_NT, so using it
+	 as default_directory causes an infinite recursion in, e.g.,
+	 the following:
+
+            (let (default-directory)
+	      (expand-file-name "a"))
+
+	 To avoid this, we use the root of the current drive.  */
+      root = build_string (emacs_root_dir ());
+#else
+      root = build_string ("/");
+#endif
 
   /* Use the buffer's default-directory if DEFAULT_DIRECTORY is omitted.  */
   if (NILP (default_directory))
     {
       Lisp_Object dir = BVAR (current_buffer, directory);
-      /* The buffer's default-directory should be absolute.  If it
-	 isn't, try to expand it relative to invocation-directory.
-	 But we have to be careful to avoid an infinite loop, because
-	 the code in emacs.c that sets Vinvocation_directory might
-	 call Fexpand_file_name.  */
+      /* The buffer's default-directory should be absolute or should
+	 start with `~'.  If it isn't absolute, we replace it by its
+	 expansion relative to a known absolute name ABSDIR, which is
+	 the invocation-directory if the latter is absolute, or the
+	 root otherwise.
+
+	 In case default-directory starts with `~' or `~user', where
+	 USER is a valid user name, this correctly expands it (and
+	 ABSDIR plays no role).  If USER is not a valid user name, the
+	 leading `~' loses its special meaning and is retained as part
+	 of the expanded name.  */
       if (STRINGP (dir))
 	{
-	  if (!NILP (Ffile_name_absolute_p (dir)))
+	  if (file_name_absolute_no_tilde_p (dir))
 	    default_directory = dir;
-	  else if (STRINGP (Vinvocation_directory)
-		   && !NILP (Ffile_name_absolute_p (Vinvocation_directory)))
-	    default_directory = Fexpand_file_name (dir, Vinvocation_directory);
+	  else
+	    {
+	      Lisp_Object absdir
+		= STRINGP (Vinvocation_directory)
+		&& file_name_absolute_no_tilde_p (Vinvocation_directory)
+		? Vinvocation_directory : root;
+	      default_directory = Fexpand_file_name (dir, absdir);
+	    }
 	}
     }
   if (! STRINGP (default_directory))
-    {
-#ifdef DOS_NT
-      /* "/" is not considered a root directory on DOS_NT, so using "/"
-	 here causes an infinite recursion in, e.g., the following:
-
-            (let (default-directory)
-	      (expand-file-name "a"))
-
-	 To avoid this, we set default_directory to the root of the
-	 current drive.  */
-      default_directory = build_string (emacs_root_dir ());
-#else
-      default_directory = build_string ("/");
-#endif
-    }
+    default_directory = root;
 
   handler = Ffind_file_name_handler (default_directory, Qexpand_file_name);
   if (!NILP (handler))
diff --git a/test/src/fileio-tests.el b/test/src/fileio-tests.el
index 0e0230a145..813ee5f798 100644
--- a/test/src/fileio-tests.el
+++ b/test/src/fileio-tests.el
@@ -131,4 +131,8 @@ fileio-tests--insert-file-interrupt
 (ert-deftest fileio-tests--relative-default-directory ()
   "Test expand-file-name when default-directory is relative."
   (let ((default-directory "some/relative/name"))
-    (should (file-name-absolute-p (expand-file-name "foo")))))
+    (should (file-name-absolute-p (expand-file-name "foo"))))
+  (let* ((default-directory "~foo")
+         (name (expand-file-name "bar")))
+    (should (and (file-name-absolute-p name)
+                 (not (eq (aref name 0) ?~))))))
-- 
2.21.0


  reply	other threads:[~2019-07-15 13:39 UTC|newest]

Thread overview: 34+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-07-04 16:48 bug#36502: 27.0.50; infinite loop in file-name-case-insensitive-p Daniel Sutton
2019-07-04 18:08 ` Ken Brown
     [not found]   ` <CAMfzp7bhcmeY7QP4-ALfmBE4OojJthcYEVLR79zj-FrGx5s+WA@mail.gmail.com>
2019-07-05  1:32     ` Ken Brown
     [not found]       ` <CAMfzp7brsFLdpi04pDAL+O_yVuF7=EERzinVBKoQyTaLUtgwDA@mail.gmail.com>
     [not found]         ` <CAMfzp7Y=wA8_V=Tvm1iOtyXM-kqKZyx41Q4phJfnwmygHhJWLA@mail.gmail.com>
2019-07-05  3:05           ` bug#36502: Fwd: " Daniel Sutton
2019-07-06 12:49             ` Ken Brown
2019-07-06 13:27             ` Noam Postavsky
2019-07-06 15:38               ` Ken Brown
2019-07-06 16:14                 ` Eli Zaretskii
2019-07-07 14:09                   ` Ken Brown
2019-07-07 14:37                     ` Eli Zaretskii
2019-07-07 19:30                       ` Ken Brown
2019-07-08 12:25                         ` Eli Zaretskii
2019-07-08 13:36                           ` Ken Brown
2019-07-08 13:59                             ` Eli Zaretskii
2019-07-08 15:17                               ` Ken Brown
2019-07-08 16:44                                 ` Ken Brown
2019-07-08 17:23                                   ` Eli Zaretskii
2019-07-10 21:57                                     ` Ken Brown
2019-07-11 23:36                                       ` Richard Stallman
2019-07-12  6:41                                         ` Eli Zaretskii
2019-07-12 20:18                                           ` Ken Brown
2019-07-15 13:39                                             ` Ken Brown [this message]
2019-07-19  7:00                                               ` Eli Zaretskii
2019-07-20  9:19                                               ` Eli Zaretskii
2019-07-20 14:27                                                 ` Ken Brown
2019-07-20 15:52                                                   ` Eli Zaretskii
2019-07-21  2:32                                                   ` Paul Eggert
2019-07-21 14:21                                                     ` Eli Zaretskii
2019-07-21 14:30                                                       ` Ken Brown
2019-07-22  2:16                                                       ` Ken Brown
2019-07-24 21:36                                                         ` Paul Eggert
2019-07-24 22:47                                                           ` Ken Brown
2019-07-26 11:04                                                           ` Andy Moreton
2019-07-08 14:37                         ` Andreas Schwab

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/emacs/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=09ed9fa5-efd8-93df-e4f1-dbd73cb1b823@cornell.edu \
    --to=kbrown@cornell.edu \
    --cc=36502@debbugs.gnu.org \
    --cc=dan@dpsutton.com \
    --cc=eliz@gnu.org \
    --cc=monnier@iro.umontreal.ca \
    --cc=npostavs@gmail.com \
    --cc=rms@gnu.org \
    --cc=schwab@suse.de \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).