From 0528a7c8725c7c28a6f2815802fcc089c2fe306f Mon Sep 17 00:00:00 2001 From: Ken Brown Date: Mon, 8 Jul 2019 18:37:33 -0400 Subject: [PATCH] Ensure that expand-file-name returns an absolute file name * 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 | 17 ++++++++++++++++- test/src/fileio-tests.el | 5 +++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/src/fileio.c b/src/fileio.c index 505e4ec33bf..8f23a305a52 100644 --- a/src/fileio.c +++ b/src/fileio.c @@ -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 diff --git a/test/src/fileio-tests.el b/test/src/fileio-tests.el index bd827e5498f..8788c830c94 100644 --- a/test/src/fileio-tests.el +++ b/test/src/fileio-tests.el @@ -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"))))) -- 2.39.5