unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
From: Ioannis Kappas <ioannis.kappas@gmail.com>
To: 53808@debbugs.gnu.org
Subject: bug#53808: 29.0.50; ansi colorization process could block indefinetly on stray ESC char
Date: Sat, 5 Feb 2022 21:00:21 +0000	[thread overview]
Message-ID: <CAMRHuGBLL0Fd1N_vnb+KQHicxp47Ad-ijy=gqmHOfHt8V+c1Uw@mail.gmail.com> (raw)
In-Reply-To: <CAMRHuGAcY60X4cTMXRQCcH2v=KU2mi=7oJi0Daz4S6MVRWKHOA@mail.gmail.com>

The issue appears to be caused by the ansi color context logic, trying
to handle potential SGR sequences split between string fragments. The
SGR sequence is accumulated into the context until is complete and
only then output with the rest of the input string.

But currently, identifying the beginning of an SGR sequence is
performed naively based on the first character (ESC aka ^], \033 or
\e) and until a valid C1 sequence is matched in the accumulated
context string, rather than checking whether the SGR sequence is valid
or completed:

(defconst ansi-color-control-seq-regexp
  ;; See ECMA 48, section 5.4 "Control Sequences".
  "\e\\[[\x30-\x3F]*[\x20-\x2F]*[\x40-\x7E]"
  "Regexp matching an ANSI control sequence.")

(defun ansi-color-apply (string)
  "Translates SGR control sequences into text properties..."
  (let* ((context
          (ansi-color--ensure-context 'ansi-color-context nil))
         (face-vec (car context))
         (start 0)
         end result)
    ;; If context was saved and is a string, prepend it.
    (setq string (concat (cadr context) string))
    (setcar (cdr context) "")
    ;; Find the next escape sequence.
    (while (setq end (string-match ansi-color-control-seq-regexp string start))
      (let ((esc-end (match-end 0)))
        ;; ...
        (push (substring string start end) result)
        (setq start (match-end 0))
        ;; ...
        ))
    ;; ...

    ;; save context, add the remainder of the string to the result
    (if (string-match "\033" string start)
        (let ((pos (match-beginning 0)))
          (setcar (cdr context) (substring string pos))
          (push (substring string start pos) result))
      (push (substring string start) result))
    (apply 'concat (nreverse result))))


A solution (open to discussion) could be to identify a partial SGR
fragment based on its actual specification rather than only starting
with the ESC char:

modified   lisp/ansi-color.el
@@ -501,6 +501,19 @@ ansi-color-filter-apply
       (setcar (cdr context) fragment))
     (apply #'concat (nreverse result))))

+(defconst ansi-color--sgr-partial-regex
+  "\e\\(?:\\[\\|$\\)\\(?:(?:[0-9]+;?\\)*"
+  "A regexp for locating the beginning of a partial SGR
+  sequence.")
+
+(defun ansi-color--sgr-fragment-pos (string start)
+  "Check if STRING ends with a partial SGR sequence and return
+its position or nil otherwise. Start looking in STRING at position START."
+  (save-match-data
+    (when (and (string-match ansi-color--sgr-partial-regex string start)
+               (= (match-end 0) (1- (length string))))
+      (match-beginning 0))))
+
 (defun ansi-color-apply (string)
   "Translates SGR control sequences into text properties.
 Delete all other control sequences without processing them.
@@ -549,8 +562,8 @@ ansi-color-apply
       (put-text-property start (length string)
                          'font-lock-face face string))
     ;; save context, add the remainder of the string to the result
-    (if (string-match "\033" string start)
-        (let ((pos (match-beginning 0)))
+    (if-let ((pos (ansi-color--sgr-fragment-pos string start)))
+        (progn
           (setcar (cdr context) (substring string pos))
           (push (substring string start pos) result))
       (push (substring string start) result))

Let me know your thoughts, there is also `ansi-color-filter-apply' and
`ansi-color-filter-region' that would need similar treatment. I also
have a unit test in development.

Thanks





  reply	other threads:[~2022-02-05 21:00 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-02-05 20:52 bug#53808: 29.0.50; ansi colorization process could block indefinetly on stray ESC char Ioannis Kappas
2022-02-05 21:00 ` Ioannis Kappas [this message]
2022-02-05 21:47   ` Ioannis Kappas
2022-02-05 21:56   ` Lars Ingebrigtsen
2022-02-05 22:05     ` Ioannis Kappas
2022-02-06 20:36       ` miha--- via Bug reports for GNU Emacs, the Swiss army knife of text editors
2022-02-06 22:55         ` Lars Ingebrigtsen
2022-02-07  7:51         ` Ioannis Kappas
2022-02-07 11:42           ` miha--- via Bug reports for GNU Emacs, the Swiss army knife of text editors

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/emacs/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='CAMRHuGBLL0Fd1N_vnb+KQHicxp47Ad-ijy=gqmHOfHt8V+c1Uw@mail.gmail.com' \
    --to=ioannis.kappas@gmail.com \
    --cc=53808@debbugs.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).