From 9438a1d3bd591a5eec9f7d80308cc2674fdd3fdf Mon Sep 17 00:00:00 2001 From: Eli Zaretskii Date: Wed, 9 Oct 2024 16:21:08 +0300 Subject: [PATCH] Avoid segfaults in Rmail-MIME Rmail-MIME decodes text of email, including removal of CR characters, but that can segfault if the text of some MIME part is empty. * src/coding.c (decode_coding_raw_text): * lisp/mail/rmailmm.el (rmail-mime-insert-decoded-text): Don't attempt to decode empty text region. (cherry picked from commit f520008744b1eb71accded4108888d1f2055402e) --- lisp/mail/rmailmm.el | 12 +++++++----- src/coding.c | 4 +++- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/lisp/mail/rmailmm.el b/lisp/mail/rmailmm.el index a5ce5b65cd7..d86e48e6281 100644 --- a/lisp/mail/rmailmm.el +++ b/lisp/mail/rmailmm.el @@ -579,11 +579,13 @@ HEADER is a header component of a MIME-entity object (see (ignore-errors (base64-decode-region pos (point)))) ((string= transfer-encoding "quoted-printable") (quoted-printable-decode-region pos (point)))))) - (decode-coding-region - pos (point) - ;; Use -dos decoding, to remove ^M characters left from base64 or - ;; rogue qp-encoded text. - (coding-system-change-eol-conversion coding-system 1)) + ;; If the text is empty, we don't have anything to decode. + (and (/= pos (point)) + (decode-coding-region + pos (point) + ;; Use -dos decoding, to remove ^M characters left from base64 + ;; or rogue qp-encoded text. + (coding-system-change-eol-conversion coding-system 1))) (if (and (or (not rmail-mime-coding-system) (consp rmail-mime-coding-system)) (not (eq (coding-system-base coding-system) 'us-ascii))) diff --git a/src/coding.c b/src/coding.c index 547fcbb8120..cd5a12972e6 100644 --- a/src/coding.c +++ b/src/coding.c @@ -5270,7 +5270,9 @@ decode_coding_raw_text (struct coding_system *coding) coding->chars_at_source = 1; coding->consumed_char = coding->src_chars; coding->consumed = coding->src_bytes; - if (eol_dos && coding->source[coding->src_bytes - 1] == '\r') + if (eol_dos + && coding->src_bytes > 0 /* empty source text? */ + && coding->source[coding->src_bytes - 1] == '\r') { coding->consumed_char--; coding->consumed--; -- 2.39.5