From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Ioannis Kappas Newsgroups: gmane.emacs.bugs 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 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset="UTF-8" Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="36270"; mail-complaints-to="usenet@ciao.gmane.io" To: 53808@debbugs.gnu.org Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Sat Feb 05 22:01:23 2022 Return-path: Envelope-to: geb-bug-gnu-emacs@m.gmane-mx.org Original-Received: from lists.gnu.org ([209.51.188.17]) by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1nGSBP-0009Kj-7w for geb-bug-gnu-emacs@m.gmane-mx.org; Sat, 05 Feb 2022 22:01:23 +0100 Original-Received: from localhost ([::1]:40972 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1nGSBN-0001W4-Ri for geb-bug-gnu-emacs@m.gmane-mx.org; Sat, 05 Feb 2022 16:01:21 -0500 Original-Received: from eggs.gnu.org ([209.51.188.92]:48846) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nGSB4-0001UF-Tt for bug-gnu-emacs@gnu.org; Sat, 05 Feb 2022 16:01:03 -0500 Original-Received: from debbugs.gnu.org ([209.51.188.43]:41785) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1nGSB4-0007wL-I4 for bug-gnu-emacs@gnu.org; Sat, 05 Feb 2022 16:01:02 -0500 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1nGSB4-0007ae-Bt for bug-gnu-emacs@gnu.org; Sat, 05 Feb 2022 16:01:02 -0500 X-Loop: help-debbugs@gnu.org In-Reply-To: Resent-From: Ioannis Kappas Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Sat, 05 Feb 2022 21:01:02 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 53808 X-GNU-PR-Package: emacs Original-Received: via spool by 53808-submit@debbugs.gnu.org id=B53808.164409483829140 (code B ref 53808); Sat, 05 Feb 2022 21:01:02 +0000 Original-Received: (at 53808) by debbugs.gnu.org; 5 Feb 2022 21:00:38 +0000 Original-Received: from localhost ([127.0.0.1]:35682 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1nGSAf-0007Zv-Tr for submit@debbugs.gnu.org; Sat, 05 Feb 2022 16:00:38 -0500 Original-Received: from mail-ot1-f50.google.com ([209.85.210.50]:43575) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1nGSAe-0007Zj-Ok for 53808@debbugs.gnu.org; Sat, 05 Feb 2022 16:00:37 -0500 Original-Received: by mail-ot1-f50.google.com with SMTP id j38-20020a9d1926000000b0059fa6de6c71so8029637ota.10 for <53808@debbugs.gnu.org>; Sat, 05 Feb 2022 13:00:36 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=mime-version:from:date:message-id:subject:to; bh=KhwxJrX9Gm4ysQGzCeDfTDDzXlYDTS18dTrjqT7VNoM=; b=PKbyHGdEZC1milxuDD8Wr0HBkveIPlkWdBYmLSMdSQeq4/PUGiXV91c7JDjtkpqQ8N /5i1TaYQtkN8/DfX1G7ged8M7sBGPNXDZBew1eNBnH7p4VO7C4tsxoT39zDD6/huaVdy rYINB7RCPTwAWts3TflwyiDCimPDac3gcDj511oRfeCOlNyBjNinJGjmidCy2HP9ZfXS RT75KFEIvPv73AynsOUT0zrQ1xkB7D7agZSyulloyComKX62myF2vNI6Ca78XNCMSZ2b qVw/JBOAjQZKEo77IiLs884m4nwu314P+Svu6BfLbPH74VnSZjc3fVwUNuj/oluGAgGU yOPw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:mime-version:from:date:message-id:subject:to; bh=KhwxJrX9Gm4ysQGzCeDfTDDzXlYDTS18dTrjqT7VNoM=; b=HUzJoZDWtJ0Kcy1YDSARkoB0WC6cMirc2zIuSKMzkdKXtcQH0wepcRxNoMOmnis/oH kxyVP4Bp8yghVIuy15iXa6HB/n6HUNfXWhQDIZ4JMysCRPHRaFwnFiBDvTmKcRyOjxUJ dxUXgVVNWkJ2BnDehWdWdlddugPHvCpbgkZt2HJuWpGO6I1F8wbpI09MvY5UbV692h5N OIDWcTxcrYBBlEqlhjPYPzqlPuG5quLinpLsZ/2HnNf79d6mA4aU9Xu0hE75ux8UbdQY qoBcIV6WE0nJSDWw+hrEqJ9HszMtjhk/UW4uxX1ET1Zo9uEKLqqHaQfaS8bicnAweYtU gp1w== X-Gm-Message-State: AOAM532SER8Zf1y75iAsTWUnWgXjdtJNtuKOikPIvm7OJIkDGoYxKZA1 45VuJG+RLH44SQ8fJFuoTWS42yMbjjmLM2Fscg73xNp5BTo= X-Google-Smtp-Source: ABdhPJyOL9Vqu932VyKdoQs8Ws+kbn4YyQ6ofx15J/qhuJL3pvb7Hf3DFfrGG0AfE/Lt/tK2sDUd/oPccIiYl3lPkc4= X-Received: by 2002:a05:6830:25c6:: with SMTP id d6mr1842813otu.91.1644094830999; Sat, 05 Feb 2022 13:00:30 -0800 (PST) X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-BeenThere: bug-gnu-emacs@gnu.org List-Id: "Bug reports for GNU Emacs, the Swiss army knife of text editors" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Original-Sender: "bug-gnu-emacs" Xref: news.gmane.io gmane.emacs.bugs:226092 Archived-At: 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