aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorFederico Tedin2021-09-15 00:15:16 +0200
committerEli Zaretskii2021-09-18 09:36:26 +0300
commit4e21c5f451a18f96172e63dbe8a3ceef780758bb (patch)
treeeb8e8408ac1db13a0892eb20818189e3f3f086fd
parent62e870691d2192e7848e047734556dec21797a7b (diff)
downloademacs-4e21c5f451a18f96172e63dbe8a3ceef780758bb.tar.gz
emacs-4e21c5f451a18f96172e63dbe8a3ceef780758bb.zip
Check for null bytes in filenames in 'expand-file-name' (bug#49723)
* src/fileio.c (expand-file-name): Check for null bytes for both NAME and DEFAULT-DIRECTORY arguments. Also check for null bytes in buffer-local default-directory, assuming it is used. * src/coding.c (encode_file_name): Use CHECK_STRING_NULL_BYTES. * src/lisp.h (CHECK_STRING_NULL_BYTES): Add function for checking for null bytes in Lisp strings. * test/src/fileio-tests.el (fileio-test--expand-file-name-null-bytes): Add test for new changes to expand-file-name. * etc/NEWS: Announce changes.
-rw-r--r--etc/NEWS7
-rw-r--r--src/coding.c3
-rw-r--r--src/fileio.c6
-rw-r--r--src/lisp.h7
-rw-r--r--test/src/fileio-tests.el9
5 files changed, 29 insertions, 3 deletions
diff --git a/etc/NEWS b/etc/NEWS
index 515f8bac562..b93e87642b8 100644
--- a/etc/NEWS
+++ b/etc/NEWS
@@ -294,6 +294,13 @@ personalize the uniquified buffer name.
294--- 294---
295** 'remove-hook' is now an interactive command. 295** 'remove-hook' is now an interactive command.
296 296
297** 'expand-file-name' now checks for null bytes in filenames.
298The function will now check for null bytes in both NAME and
299DEFAULT-DIRECTORY arguments, as well as in the 'default-directory'
300buffer-local variable, assuming its value is used. If null bytes are
301found, 'expand-file-name' will signal an error.
302
303---
297** Frames 304** Frames
298 305
299+++ 306+++
diff --git a/src/coding.c b/src/coding.c
index d027c7d5399..7030a53869a 100644
--- a/src/coding.c
+++ b/src/coding.c
@@ -10430,8 +10430,7 @@ encode_file_name (Lisp_Object fname)
10430 cause subtle bugs because the system would silently use a 10430 cause subtle bugs because the system would silently use a
10431 different filename than expected. Perform this check after 10431 different filename than expected. Perform this check after
10432 encoding to not miss NUL bytes introduced through encoding. */ 10432 encoding to not miss NUL bytes introduced through encoding. */
10433 CHECK_TYPE (memchr (SSDATA (encoded), '\0', SBYTES (encoded)) == NULL, 10433 CHECK_STRING_NULL_BYTES (encoded);
10434 Qfilenamep, fname);
10435 return encoded; 10434 return encoded;
10436} 10435}
10437 10436
diff --git a/src/fileio.c b/src/fileio.c
index 0db8ed793b3..3c13d3fe416 100644
--- a/src/fileio.c
+++ b/src/fileio.c
@@ -945,6 +945,7 @@ the root directory. */)
945 USE_SAFE_ALLOCA; 945 USE_SAFE_ALLOCA;
946 946
947 CHECK_STRING (name); 947 CHECK_STRING (name);
948 CHECK_STRING_NULL_BYTES (name);
948 949
949 /* If the file name has special constructs in it, 950 /* If the file name has special constructs in it,
950 call the corresponding file name handler. */ 951 call the corresponding file name handler. */
@@ -993,7 +994,10 @@ the root directory. */)
993 if (STRINGP (dir)) 994 if (STRINGP (dir))
994 { 995 {
995 if (file_name_absolute_no_tilde_p (dir)) 996 if (file_name_absolute_no_tilde_p (dir))
996 default_directory = dir; 997 {
998 CHECK_STRING_NULL_BYTES (dir);
999 default_directory = dir;
1000 }
997 else 1001 else
998 { 1002 {
999 Lisp_Object absdir 1003 Lisp_Object absdir
diff --git a/src/lisp.h b/src/lisp.h
index 7bfc69b647b..9716b34baee 100644
--- a/src/lisp.h
+++ b/src/lisp.h
@@ -1615,6 +1615,13 @@ STRING_SET_CHARS (Lisp_Object string, ptrdiff_t newsize)
1615 XSTRING (string)->u.s.size = newsize; 1615 XSTRING (string)->u.s.size = newsize;
1616} 1616}
1617 1617
1618INLINE void
1619CHECK_STRING_NULL_BYTES (Lisp_Object string)
1620{
1621 CHECK_TYPE (memchr (SSDATA (string), '\0', SBYTES (string)) == NULL,
1622 Qfilenamep, string);
1623}
1624
1618/* A regular vector is just a header plus an array of Lisp_Objects. */ 1625/* A regular vector is just a header plus an array of Lisp_Objects. */
1619 1626
1620struct Lisp_Vector 1627struct Lisp_Vector
diff --git a/test/src/fileio-tests.el b/test/src/fileio-tests.el
index f4d123b4261..438ebebb769 100644
--- a/test/src/fileio-tests.el
+++ b/test/src/fileio-tests.el
@@ -136,6 +136,15 @@ Also check that an encoding error can appear in a symlink."
136 (should (and (file-name-absolute-p name) 136 (should (and (file-name-absolute-p name)
137 (not (eq (aref name 0) ?~)))))) 137 (not (eq (aref name 0) ?~))))))
138 138
139(ert-deftest fileio-test--expand-file-name-null-bytes ()
140 "Test that expand-file-name checks for null bytes in filenames."
141 (should-error (expand-file-name (concat "file" (char-to-string ?\0) ".txt"))
142 :type 'wrong-type-argument)
143 (should-error (expand-file-name "file.txt" (concat "dir" (char-to-string ?\0)))
144 :type 'wrong-type-argument)
145 (let ((default-directory (concat "dir" (char-to-string ?\0))))
146 (should-error (expand-file-name "file.txt") :type 'wrong-type-argument)))
147
139(ert-deftest fileio-tests--file-name-absolute-p () 148(ert-deftest fileio-tests--file-name-absolute-p ()
140 "Test file-name-absolute-p." 149 "Test file-name-absolute-p."
141 (dolist (suffix '("" "/" "//" "/foo" "/foo/" "/foo//" "/foo/bar")) 150 (dolist (suffix '("" "/" "//" "/foo" "/foo/" "/foo//" "/foo/bar"))