]> git.eshelyaron.com Git - emacs.git/commitdiff
Ensure that expand-file-name returns an absolute file name
authorKen Brown <kbrown@cornell.edu>
Mon, 8 Jul 2019 22:37:33 +0000 (18:37 -0400)
committerKen Brown <kbrown@cornell.edu>
Mon, 8 Jul 2019 22:37:33 +0000 (18:37 -0400)
* src/fileio.c (Fexpand_file_name): Don't directly use the current
buffer's default-directory if it is relative.  Instead replace it
by its expansion relative to invocation-directory.  (Bug#36502)
* test/src/fileio-tests.el
(fileio-tests--relative-default-directory): New test.

src/fileio.c
test/src/fileio-tests.el

index 505e4ec33bf655daa83c59888a8fd5fff6901c7a..8f23a305a52772ca15c730b1620143ac23562439 100644 (file)
@@ -804,7 +804,22 @@ the root directory.  */)
 
   /* Use the buffer's default-directory if DEFAULT_DIRECTORY is omitted.  */
   if (NILP (default_directory))
-    default_directory = BVAR (current_buffer, 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.  */
+      if (STRINGP (dir))
+       {
+         if (!NILP (Ffile_name_absolute_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);
+       }
+    }
   if (! STRINGP (default_directory))
     {
 #ifdef DOS_NT
index bd827e5498ffa3c79356f038d10e8434471eaf58..8788c830c9420522aa472cd686dc418461e59e59 100644 (file)
@@ -126,3 +126,8 @@ Also check that an encoding error can appear in a symlink."
               (should (equal c1 (char-before)))
               (should (equal c1 (char-after))))))
       (if f (delete-file f)))))
+
+(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")))))