GNU bug report logs -
#53808
29.0.50; ansi colorization process could block indefinetly on stray ESC char
Previous Next
Reported by: Ioannis Kappas <ioannis.kappas <at> gmail.com>
Date: Sat, 5 Feb 2022 20:53:02 UTC
Severity: normal
Found in version 29.0.50
Fixed in version 29.1
Done: Lars Ingebrigtsen <larsi <at> gnus.org>
Bug is archived. No further changes may be made.
Full log
Message #8 received at 53808 <at> debbugs.gnu.org (full text, mbox):
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
This bug report was last modified 3 years and 106 days ago.
Previous Next
GNU bug tracking system
Copyright (C) 1999 Darren O. Benham,
1997,2003 nCipher Corporation Ltd,
1994-97 Ian Jackson.