]> git.eshelyaron.com Git - emacs.git/commitdiff
* lisp/mail/rfc2231.el (rfc2231-decode-encoded-string): Fix match data error
authorStefan Monnier <monnier@iro.umontreal.ca>
Mon, 19 Oct 2020 17:03:41 +0000 (13:03 -0400)
committerStefan Monnier <monnier@iro.umontreal.ca>
Mon, 19 Oct 2020 17:03:41 +0000 (13:03 -0400)
Get (match-string 3 string) earlier, in case `mm-charset-to-coding-system`
clobbers the match data.
Also, check that `string-match` succeeded before using its match data.

lisp/mail/rfc2231.el

index add099745b6440ade3306f77d92a53b20122760f..17da60e0bee30b16247e8a4e2ab3803fc0d65716 100644 (file)
@@ -215,23 +215,25 @@ These look like:
  \"\\='en-us\\='This%20is%20%2A%2A%2Afun%2A%2A%2A\",
  \"\\='\\='This%20is%20%2A%2A%2Afun%2A%2A%2A\", or
  \"This is ***fun***\"."
-  (string-match "\\`\\(?:\\([^']+\\)?'\\([^']+\\)?'\\)?\\(.+\\)" string)
-  (let ((coding-system (mm-charset-to-coding-system
-                       (match-string 1 string) nil t))
-       ;;(language (match-string 2 string))
-       (value (match-string 3 string)))
-    (mm-with-unibyte-buffer
-      (insert value)
-      (goto-char (point-min))
-      (while (re-search-forward "%\\([[:xdigit:]][[:xdigit:]]\\)" nil t)
-       (insert
-        (prog1
-            (string-to-number (match-string 1) 16)
-          (delete-region (match-beginning 0) (match-end 0)))))
-      ;; Decode using the charset, if any.
-      (if (memq coding-system '(nil ascii))
-         (buffer-string)
-       (decode-coding-string (buffer-string) coding-system)))))
+  (if (not (string-match "\\`\\(?:\\([^']+\\)?'\\([^']+\\)?'\\)?\\(.+\\)\\'"
+                         string))
+      (error "Unrecognized RFC2231 format: %S" string)
+    (let ((value (match-string 3 string))
+         ;;(language (match-string 2 string))
+         (coding-system (mm-charset-to-coding-system
+                         (match-string 1 string) nil t)))
+      (mm-with-unibyte-buffer
+        (insert value)
+        (goto-char (point-min))
+        (while (re-search-forward "%\\([[:xdigit:]][[:xdigit:]]\\)" nil t)
+         (insert
+          (prog1
+              (string-to-number (match-string 1) 16)
+            (delete-region (match-beginning 0) (match-end 0)))))
+       ;; Decode using the charset, if any.
+       (if (memq coding-system '(nil ascii))
+           (buffer-string)
+         (decode-coding-string (buffer-string) coding-system))))))
 
 (defun rfc2231-encode-string (param value)
   "Return a PARAM=VALUE string encoded according to RFC2231.