emacs-diffs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Emacs-diffs] master ebf53ed: Fix make-temp-file bug with ""/"."/".." pr


From: Paul Eggert
Subject: [Emacs-diffs] master ebf53ed: Fix make-temp-file bug with ""/"."/".." prefix
Date: Sat, 12 Aug 2017 23:05:28 -0400 (EDT)

branch: master
commit ebf53ed4f6469d24c3a76835eab014d82aed551c
Author: Paul Eggert <address@hidden>
Commit: Paul Eggert <address@hidden>

    Fix make-temp-file bug with ""/"."/".." prefix
    
    The bug with "." and ".." has been present for a while; I
    introduced the bug with "" earlier today in my patch for Bug#28023.
    * lisp/files.el (make-temp-file): Do not use expand-file-name if
    PREFIX is empty or "." or "..", as it does the wrong thing.
    Compute absolute-prefix here ...
    (files--make-magic-temp-file): ... instead of here ...
    * src/fileio.c (Fmake_temp_file_internal): ... or here.
    
    * lisp/files.el (make-temp-file): If the prefix is empty, append
    "/" to the absolute prefix so that the new files are children
    rather than siblings of temporary-file-directory.  This fixes a
    bug introduced in the previous change.
    * test/lisp/files-tests.el (files-test-make-temp-file-empty-prefix):
    New test, for the bug.
---
 lisp/files.el            | 19 ++++++++-----------
 src/fileio.c             |  9 ++++-----
 test/lisp/files-tests.el | 14 ++++++++++++++
 3 files changed, 26 insertions(+), 16 deletions(-)

diff --git a/lisp/files.el b/lisp/files.el
index 19573cd..b05d453 100644
--- a/lisp/files.el
+++ b/lisp/files.el
@@ -1407,14 +1407,17 @@ You can then use `write-region' to write new data into 
the file.
 If DIR-FLAG is non-nil, create a new empty directory instead of a file.
 
 If SUFFIX is non-nil, add that at the end of the file name."
-  (let ((absolute-prefix (expand-file-name prefix temporary-file-directory)))
+  (let ((absolute-prefix
+        (if (or (zerop (length prefix)) (member prefix '("." "..")))
+            (concat (file-name-as-directory temporary-file-directory) prefix)
+          (expand-file-name prefix temporary-file-directory))))
     (if (find-file-name-handler absolute-prefix 'write-region)
-        (files--make-magic-temp-file prefix dir-flag suffix)
+        (files--make-magic-temp-file absolute-prefix dir-flag suffix)
       (make-temp-file-internal absolute-prefix
                               (if dir-flag t) (or suffix "")))))
 
-(defun files--make-magic-temp-file (prefix &optional dir-flag suffix)
-  "Implement (make-temp-file PREFIX DIR-FLAG SUFFIX).
+(defun files--make-magic-temp-file (absolute-prefix &optional dir-flag suffix)
+  "Implement (make-temp-file ABSOLUTE-PREFIX DIR-FLAG SUFFIX).
 This implementation works on magic file names."
   ;; Create temp files with strict access rights.  It's easy to
   ;; loosen them later, whereas it's impossible to close the
@@ -1423,13 +1426,7 @@ This implementation works on magic file names."
     (let (file)
       (while (condition-case ()
                 (progn
-                  (setq file
-                        (make-temp-name
-                         (if (zerop (length prefix))
-                             (file-name-as-directory
-                              temporary-file-directory)
-                           (expand-file-name prefix
-                                             temporary-file-directory))))
+                  (setq file (make-temp-name absolute-prefix))
                   (if suffix
                       (setq file (concat file suffix)))
                   (if dir-flag
diff --git a/src/fileio.c b/src/fileio.c
index b7e3b71..69079c6 100644
--- a/src/fileio.c
+++ b/src/fileio.c
@@ -662,17 +662,16 @@ DEFUN ("make-temp-file-internal", 
Fmake_temp_file_internal,
 Return the name of the generated file.  If DIR-FLAG is zero, do not
 create the file, just its name.  Otherwise, if DIR-FLAG is non-nil,
 create an empty directory.  The file name should end in SUFFIX.
+Do not expand PREFIX; a non-absolute PREFIX is relative to the Emacs
+working directory.
 
 Signal an error if the file could not be created.
 
 This function does not grok magic file names.  */)
   (Lisp_Object prefix, Lisp_Object dir_flag, Lisp_Object suffix)
 {
-  bool make_temp_name = EQ (dir_flag, make_number (0));
+  CHECK_STRING (prefix);
   CHECK_STRING (suffix);
-  if (!make_temp_name)
-    prefix = Fexpand_file_name (prefix, Vtemporary_file_directory);
-
   Lisp_Object encoded_prefix = ENCODE_FILE (prefix);
   Lisp_Object encoded_suffix = ENCODE_FILE (suffix);
   ptrdiff_t prefix_len = SBYTES (encoded_prefix);
@@ -686,7 +685,7 @@ This function does not grok magic file names.  */)
   memset (data + prefix_len, 'X', nX);
   memcpy (data + prefix_len + nX, SSDATA (encoded_suffix), suffix_len);
   int kind = (NILP (dir_flag) ? GT_FILE
-             : make_temp_name ? GT_NOCREATE
+             : EQ (dir_flag, make_number (0)) ? GT_NOCREATE
              : GT_DIR);
   int fd = gen_tempname (data, suffix_len, O_BINARY | O_CLOEXEC, kind);
   if (fd < 0 || (NILP (dir_flag) && emacs_close (fd) != 0))
diff --git a/test/lisp/files-tests.el b/test/lisp/files-tests.el
index 7bfdca5..4a17e0d 100644
--- a/test/lisp/files-tests.el
+++ b/test/lisp/files-tests.el
@@ -166,6 +166,20 @@ form.")
            (should (eq buffer-file-coding-system 'iso-2022-7bit-unix))))
       (delete-file tempfile))))
 
+(ert-deftest files-test-make-temp-file-empty-prefix ()
+  "Test make-temp-file with an empty prefix."
+  (let ((tempfile (make-temp-file ""))
+        (tempdir (make-temp-file "" t))
+        (tempfile-. (make-temp-file "."))
+        (tempdir-. (make-temp-file "." t))
+        (tempfile-.. (make-temp-file ".."))
+        (tempdir-.. (make-temp-file ".." t)))
+    (dolist (file (list tempfile tempfile-. tempfile-..))
+      (should file)
+      (delete-file file))
+    (dolist (dir (list tempdir tempdir-. tempdir-..))
+      (should dir)
+      (delete-directory dir))))
 
 ;; Stop the above "Local Var..." confusing Emacs.
 



reply via email to

[Prev in Thread] Current Thread [Next in Thread]