diff options
| author | Philipp Stephani | 2020-12-23 12:00:46 +0100 |
|---|---|---|
| committer | Philipp Stephani | 2020-12-23 12:00:46 +0100 |
| commit | 3cbd4169d6dd370b4fa8180fc2adfbf426f57837 (patch) | |
| tree | 11b24ad133c4f24f9e8d8798f4fb482944de15ab /src/coding.c | |
| parent | 3edc4fd53ff9e1355da2371400aac4d5897ca190 (diff) | |
| download | emacs-3cbd4169d6dd370b4fa8180fc2adfbf426f57837.tar.gz emacs-3cbd4169d6dd370b4fa8180fc2adfbf426f57837.zip | |
Reject filenames containing NUL bytes.
Such filenames are dangerous, as Emacs would silently only use the
part up to the first NUL byte. Reject them explicitly instead.
* src/coding.c (encode_file_name_1): New helper function.
(encode_file_name): Check that encoded filename doesn't contain a
NUL byte.
(syms_of_coding): Define 'filenamep' symbol.
* test/src/fileio-tests.el (fileio-tests/null-character): New unit
test.
* etc/NEWS: Document change.
Diffstat (limited to 'src/coding.c')
| -rw-r--r-- | src/coding.c | 18 |
1 files changed, 16 insertions, 2 deletions
diff --git a/src/coding.c b/src/coding.c index 1afa4aa4749..8c2443889d4 100644 --- a/src/coding.c +++ b/src/coding.c | |||
| @@ -10354,8 +10354,8 @@ decode_file_name (Lisp_Object fname) | |||
| 10354 | #endif | 10354 | #endif |
| 10355 | } | 10355 | } |
| 10356 | 10356 | ||
| 10357 | Lisp_Object | 10357 | static Lisp_Object |
| 10358 | encode_file_name (Lisp_Object fname) | 10358 | encode_file_name_1 (Lisp_Object fname) |
| 10359 | { | 10359 | { |
| 10360 | /* This is especially important during bootstrap and dumping, when | 10360 | /* This is especially important during bootstrap and dumping, when |
| 10361 | file-name encoding is not yet known, and therefore any non-ASCII | 10361 | file-name encoding is not yet known, and therefore any non-ASCII |
| @@ -10380,6 +10380,19 @@ encode_file_name (Lisp_Object fname) | |||
| 10380 | #endif | 10380 | #endif |
| 10381 | } | 10381 | } |
| 10382 | 10382 | ||
| 10383 | Lisp_Object | ||
| 10384 | encode_file_name (Lisp_Object fname) | ||
| 10385 | { | ||
| 10386 | Lisp_Object encoded = encode_file_name_1 (fname); | ||
| 10387 | /* No system accepts NUL bytes in filenames. Allowing them can | ||
| 10388 | cause subtle bugs because the system would silently use a | ||
| 10389 | different filename than expected. Perform this check after | ||
| 10390 | encoding to not miss NUL bytes introduced through encoding. */ | ||
| 10391 | CHECK_TYPE (memchr (SSDATA (encoded), '\0', SBYTES (encoded)) == NULL, | ||
| 10392 | Qfilenamep, fname); | ||
| 10393 | return encoded; | ||
| 10394 | } | ||
| 10395 | |||
| 10383 | DEFUN ("decode-coding-string", Fdecode_coding_string, Sdecode_coding_string, | 10396 | DEFUN ("decode-coding-string", Fdecode_coding_string, Sdecode_coding_string, |
| 10384 | 2, 4, 0, | 10397 | 2, 4, 0, |
| 10385 | doc: /* Decode STRING which is encoded in CODING-SYSTEM, and return the result. | 10398 | doc: /* Decode STRING which is encoded in CODING-SYSTEM, and return the result. |
| @@ -11780,6 +11793,7 @@ syms_of_coding (void) | |||
| 11780 | DEFSYM (Qignored, "ignored"); | 11793 | DEFSYM (Qignored, "ignored"); |
| 11781 | 11794 | ||
| 11782 | DEFSYM (Qutf_8_string_p, "utf-8-string-p"); | 11795 | DEFSYM (Qutf_8_string_p, "utf-8-string-p"); |
| 11796 | DEFSYM (Qfilenamep, "filenamep"); | ||
| 11783 | 11797 | ||
| 11784 | defsubr (&Scoding_system_p); | 11798 | defsubr (&Scoding_system_p); |
| 11785 | defsubr (&Sread_coding_system); | 11799 | defsubr (&Sread_coding_system); |