]> git.eshelyaron.com Git - emacs.git/commitdiff
Fix file-name-case-insensitive-p in ffap (bug#56443)
authorMattias Engdegård <mattiase@acm.org>
Fri, 8 Jul 2022 13:09:16 +0000 (15:09 +0200)
committerMattias Engdegård <mattiase@acm.org>
Fri, 8 Jul 2022 13:13:21 +0000 (15:13 +0200)
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
test/src/fileio-tests.el

index d07e62a12125277cc54bc6db78474dd5dfebfa6e..9697f6c8cf18c3df9e8a90e7ead51fb72171988d 100644 (file)
@@ -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;
     }
index c137ce06f1a3c981e194fcf6b4bb917bab974b37..08582c8a86267d742c8798426eb77e1b81b830c1 100644 (file)
@@ -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