diff options
| author | Bob Rogers | 2022-02-13 09:32:13 +0100 |
|---|---|---|
| committer | Lars Ingebrigtsen | 2022-02-13 09:32:13 +0100 |
| commit | 51d44fd705a2779beeb3fe1d59af88caadbc247a (patch) | |
| tree | dcd9bc8dc1a092352f84489aa10dfe91f945394c | |
| parent | ae13948ff52c91b4a8188be6a2cc8d65b440731c (diff) | |
| download | emacs-51d44fd705a2779beeb3fe1d59af88caadbc247a.tar.gz emacs-51d44fd705a2779beeb3fe1d59af88caadbc247a.zip | |
Fix ietf-drums-remove-whitespace unmatched " and (
* lisp/mail/ietf-drums.el:
+ (ietf-drums-skip-comment): New helper function.
+ (ietf-drums-remove-comments): Use ietf-drums-skip-comment.
+ (ietf-drums-remove-whitespace): Handle unterminated quotes and
comments, as ietf-drums-remove-comments already does.
* test/lisp/mail/ietf-drums-tests.el:
+ Test unterminated quote and comment for
ietf-drums-remove-whitespace and ietf-drums-remove-comments (bug#53965).
| -rw-r--r-- | lisp/mail/ietf-drums.el | 30 | ||||
| -rw-r--r-- | test/lisp/mail/ietf-drums-tests.el | 16 |
2 files changed, 36 insertions, 10 deletions
diff --git a/lisp/mail/ietf-drums.el b/lisp/mail/ietf-drums.el index db77aba172f..85aa27235fc 100644 --- a/lisp/mail/ietf-drums.el +++ b/lisp/mail/ietf-drums.el | |||
| @@ -65,6 +65,21 @@ backslash and doublequote.") | |||
| 65 | (modify-syntax-entry ?\' "_" table) | 65 | (modify-syntax-entry ?\' "_" table) |
| 66 | table)) | 66 | table)) |
| 67 | 67 | ||
| 68 | (defvar ietf-drums-comment-syntax-table | ||
| 69 | (let ((table (copy-syntax-table ietf-drums-syntax-table))) | ||
| 70 | (modify-syntax-entry ?\" "w" table) | ||
| 71 | table) | ||
| 72 | "In comments, DQUOTE is normal and does not start a string.") | ||
| 73 | |||
| 74 | (defun ietf-drums--skip-comment () | ||
| 75 | ;; From just before the start of a comment, go to the end. Returns | ||
| 76 | ;; point. If the comment is unterminated, go to point-max. | ||
| 77 | (condition-case () | ||
| 78 | (with-syntax-table ietf-drums-comment-syntax-table | ||
| 79 | (forward-sexp 1)) | ||
| 80 | (scan-error (goto-char (point-max)))) | ||
| 81 | (point)) | ||
| 82 | |||
| 68 | (defun ietf-drums-token-to-list (token) | 83 | (defun ietf-drums-token-to-list (token) |
| 69 | "Translate TOKEN into a list of characters." | 84 | "Translate TOKEN into a list of characters." |
| 70 | (let ((i 0) | 85 | (let ((i 0) |
| @@ -109,14 +124,7 @@ backslash and doublequote.") | |||
| 109 | (forward-sexp 1) | 124 | (forward-sexp 1) |
| 110 | (error (goto-char (point-max))))) | 125 | (error (goto-char (point-max))))) |
| 111 | ((eq c ?\() | 126 | ((eq c ?\() |
| 112 | (delete-region | 127 | (delete-region (point) (ietf-drums--skip-comment))) |
| 113 | (point) | ||
| 114 | (condition-case nil | ||
| 115 | (with-syntax-table (copy-syntax-table ietf-drums-syntax-table) | ||
| 116 | (modify-syntax-entry ?\" "w") | ||
| 117 | (forward-sexp 1) | ||
| 118 | (point)) | ||
| 119 | (error (point-max))))) | ||
| 120 | (t | 128 | (t |
| 121 | (forward-char 1)))) | 129 | (forward-char 1)))) |
| 122 | (buffer-string)))) | 130 | (buffer-string)))) |
| @@ -130,9 +138,11 @@ backslash and doublequote.") | |||
| 130 | (setq c (char-after)) | 138 | (setq c (char-after)) |
| 131 | (cond | 139 | (cond |
| 132 | ((eq c ?\") | 140 | ((eq c ?\") |
| 133 | (forward-sexp 1)) | 141 | (condition-case () |
| 142 | (forward-sexp 1) | ||
| 143 | (scan-error (goto-char (point-max))))) | ||
| 134 | ((eq c ?\() | 144 | ((eq c ?\() |
| 135 | (forward-sexp 1)) | 145 | (ietf-drums--skip-comment)) |
| 136 | ((memq c '(?\ ?\t ?\n ?\r)) | 146 | ((memq c '(?\ ?\t ?\n ?\r)) |
| 137 | (delete-char 1)) | 147 | (delete-char 1)) |
| 138 | (t | 148 | (t |
diff --git a/test/lisp/mail/ietf-drums-tests.el b/test/lisp/mail/ietf-drums-tests.el index 4cc38b8763a..b13937bf736 100644 --- a/test/lisp/mail/ietf-drums-tests.el +++ b/test/lisp/mail/ietf-drums-tests.el | |||
| @@ -40,6 +40,16 @@ | |||
| 40 | (should (equal (ietf-drums-remove-comments | 40 | (should (equal (ietf-drums-remove-comments |
| 41 | "random (first) (second (and)) (third) not fourth") | 41 | "random (first) (second (and)) (third) not fourth") |
| 42 | "random not fourth")) | 42 | "random not fourth")) |
| 43 | ;; Test some unterminated comments. | ||
| 44 | (should (equal (ietf-drums-remove-comments "test an (unterminated comment") | ||
| 45 | "test an ")) | ||
| 46 | (should (equal (ietf-drums-remove-comments "test an \"unterminated quote") | ||
| 47 | ;; returns the string unchanged (and doesn't barf). | ||
| 48 | "test an \"unterminated quote")) | ||
| 49 | (should (equal (ietf-drums-remove-comments | ||
| 50 | ;; note that double-quote is not special. | ||
| 51 | "test (unterminated comments with \"quoted (\" )stuff") | ||
| 52 | "test ")) | ||
| 43 | 53 | ||
| 44 | ;; ietf-drums-remove-whitespace | 54 | ;; ietf-drums-remove-whitespace |
| 45 | (should (equal (ietf-drums-remove-whitespace "random string") | 55 | (should (equal (ietf-drums-remove-whitespace "random string") |
| @@ -53,6 +63,12 @@ | |||
| 53 | (should (equal (ietf-drums-remove-whitespace | 63 | (should (equal (ietf-drums-remove-whitespace |
| 54 | "random (first) (second (and)) (third) not fourth") | 64 | "random (first) (second (and)) (third) not fourth") |
| 55 | "random(first)(second (and))(third)notfourth")) | 65 | "random(first)(second (and))(third)notfourth")) |
| 66 | ;; Test some unterminated comments and quotes. | ||
| 67 | (should (equal (ietf-drums-remove-whitespace | ||
| 68 | "random (first) (second (and)) (third unterminated") | ||
| 69 | "random(first)(second (and))(third unterminated")) | ||
| 70 | (should (equal (ietf-drums-remove-whitespace "random \"non terminated string") | ||
| 71 | "random\"non terminated string")) | ||
| 56 | 72 | ||
| 57 | ;; ietf-drums-strip | 73 | ;; ietf-drums-strip |
| 58 | (should (equal (ietf-drums-strip "random string") "randomstring")) | 74 | (should (equal (ietf-drums-strip "random string") "randomstring")) |