From 6791165b2a0e707f719efec08aad62cdf6ed8ad3 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Mattias=20Engdeg=C3=A5rd?= Date: Fri, 8 Jul 2022 15:09:16 +0200 Subject: [PATCH] Fix file-name-case-insensitive-p in ffap (bug#56443) Don't crash if the file name argument to file-name-case-insensitive-p, after expansion, doesn't have a parent directory. This occurs when calling ffap on something that looks like an email address. * src/fileio.c (Ffile_name_case_insensitive_p): Return nil if no file or parent directory could be found. * test/src/fileio-tests.el (fileio-tests--identity-expand-handler) (fileio--file-name-case-insensitive-p): New test. --- src/fileio.c | 6 +++--- test/src/fileio-tests.el | 16 ++++++++++++++++ 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/src/fileio.c b/src/fileio.c index d07e62a1212..9697f6c8cf1 100644 --- a/src/fileio.c +++ b/src/fileio.c @@ -2601,9 +2601,9 @@ is case-insensitive. */) if (err <= 0) return err < 0 ? Qt : Qnil; Lisp_Object parent = file_name_directory (filename); - /* Avoid infinite loop if the root has trouble - (impossible?). */ - if (!NILP (Fstring_equal (parent, filename))) + /* Avoid infinite loop if the root has trouble (if that's even possible). + Without a parent, we just don't know and return nil as well. */ + if (!STRINGP (parent) || !NILP (Fstring_equal (parent, filename))) return Qnil; filename = parent; } diff --git a/test/src/fileio-tests.el b/test/src/fileio-tests.el index c137ce06f1a..08582c8a862 100644 --- a/test/src/fileio-tests.el +++ b/test/src/fileio-tests.el @@ -201,4 +201,20 @@ Also check that an encoding error can appear in a symlink." (insert-file-contents "/dev/urandom" nil nil 10) (should (= (buffer-size) 10)))) +(defun fileio-tests--identity-expand-handler (_ file &rest _) + file) +(put 'fileio-tests--identity-expand-handler 'operations '(expand-file-name)) + +(ert-deftest fileio--file-name-case-insensitive-p () + ;; Check that we at least don't crash if given nonexisting files + ;; without a directory (bug#56443). + + ;; Use an identity file-name handler, as if called by `ffap'. + (let* ((file-name-handler-alist + '(("^mailto:" . fileio-tests--identity-expand-handler))) + (file "mailto:snowball@hell.com")) + ;; Check that `expand-file-name' is identity for this name. + (should (equal (expand-file-name file nil) file)) + (file-name-case-insensitive-p file))) + ;;; fileio-tests.el ends here -- 2.39.5