From 93b2a1303d625f6937923e6cc94626e0cf3b5ba8 Mon Sep 17 00:00:00 2001 From: Eshel Yaron Date: Sat, 27 Apr 2024 13:24:42 +0200 Subject: [PATCH] ; * lisp/dired-aux.el (dired-diff): Fix TOCTOU, simplify. --- lisp/dired-aux.el | 29 +++++++++-------------------- 1 file changed, 9 insertions(+), 20 deletions(-) diff --git a/lisp/dired-aux.el b/lisp/dired-aux.el index 2b716b0b5b6..98a8f358199 100644 --- a/lisp/dired-aux.el +++ b/lisp/dired-aux.el @@ -185,8 +185,8 @@ substituted, and will be passed through normally to the shell. proceed)) ;;;###autoload -(defun dired-diff (file &optional switches) - "Compare file at point with FILE using `diff'. +(defun dired-diff (file &optional switches current) + "Compare CURRENT file with FILE using `diff'. If called interactively, prompt for FILE. If the mark is active in Transient Mark mode, use the file at the mark as the default for FILE. (That's the mark set by \\[set-mark-command], @@ -253,30 +253,19 @@ the string of command switches used as the third argument of `diff'." ;; `M-n' candidates. (file-backup-file-names current)) (dired-dwim-target-defaults (list current) target-dir)))) + (when default (push (expand-file-name default) defaults)) (list - (minibuffer-with-setup-hook - (lambda () - (setq-local minibuffer-default-add-function nil) - (setq minibuffer-default defaults)) - (read-file-name (format-prompt "Diff %s with" default current) - target-dir default t)) + (read-file-name (format-prompt "Diff %s with" defaults current) + target-dir defaults t) (if current-prefix-arg (read-string "Options for diff: " (if (stringp diff-switches) diff-switches - (mapconcat #'identity diff-switches " ")))))) + (mapconcat #'identity diff-switches " ")))) + current)) dired-mode) - (let ((current (dired-get-filename t))) - (when (or (equal (expand-file-name file) - (expand-file-name current)) - (and (file-directory-p file) - (equal (expand-file-name current file) - (expand-file-name current)))) - (error "Attempt to compare the file to itself")) - (if (and (backup-file-name-p current) - (equal file (file-name-sans-versions current))) - (diff current file switches) - (diff file current switches)))) + (setq current (or current (dired-get-filename t))) + (diff file current switches)) ;;;###autoload (defun dired-backup-diff (&optional switches) -- 2.39.5