From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: miha--- via "Bug reports for GNU Emacs, the Swiss army knife of text editors" Newsgroups: gmane.emacs.bugs Subject: bug#53808: 29.0.50; ansi colorization process could block indefinetly on stray ESC char Date: Sun, 06 Feb 2022 21:36:59 +0100 Message-ID: <87ee4fwx3o.fsf@miha-pc> References: <87fsoxasfs.fsf@gnus.org> Reply-To: Mime-Version: 1.0 Content-Type: multipart/signed; boundary="==-=-="; micalg=pgp-sha256; protocol="application/pgp-signature" Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="34945"; mail-complaints-to="usenet@ciao.gmane.io" Cc: 53808@debbugs.gnu.org To: Ioannis Kappas , Lars Ingebrigtsen Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Sun Feb 06 21:31:19 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 1nGoBq-0008sa-Bw for geb-bug-gnu-emacs@m.gmane-mx.org; Sun, 06 Feb 2022 21:31:19 +0100 Original-Received: from localhost ([::1]:60952 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1nGoBp-00086T-7O for geb-bug-gnu-emacs@m.gmane-mx.org; Sun, 06 Feb 2022 15:31:17 -0500 Original-Received: from eggs.gnu.org ([209.51.188.92]:40168) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nGoBd-0007w5-4D for bug-gnu-emacs@gnu.org; Sun, 06 Feb 2022 15:31:05 -0500 Original-Received: from debbugs.gnu.org ([209.51.188.43]:45994) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1nGoBa-0004if-J9 for bug-gnu-emacs@gnu.org; Sun, 06 Feb 2022 15:31:03 -0500 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1nGoBa-0004R6-DM for bug-gnu-emacs@gnu.org; Sun, 06 Feb 2022 15:31:02 -0500 X-Loop: help-debbugs@gnu.org Resent-From: Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Sun, 06 Feb 2022 20:31: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.164417941416985 (code B ref 53808); Sun, 06 Feb 2022 20:31:02 +0000 Original-Received: (at 53808) by debbugs.gnu.org; 6 Feb 2022 20:30:14 +0000 Original-Received: from localhost ([127.0.0.1]:39890 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1nGoAn-0004Pq-Ms for submit@debbugs.gnu.org; Sun, 06 Feb 2022 15:30:14 -0500 Original-Received: from kamnitnik.top ([209.250.245.214]:58830) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1nGoAm-0004Pf-05 for 53808@debbugs.gnu.org; Sun, 06 Feb 2022 15:30:12 -0500 Original-Received: from localhost (BSN-77-156-43.static.siol.net [193.77.156.43]) by kamnitnik.top (Postfix) with ESMTPSA id 0D5EE9C707; Sun, 6 Feb 2022 20:30:09 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kamnitnik.top; s=mail; t=1644179410; bh=W9kt+mnjpZxretapX+fLhj37ulMrsbQ8oBqk84KXRwY=; h=From:To:Cc:Subject:In-Reply-To:References:Date:From; b=OIyCePuPI9qNN1um6Hw3jgPqRYPhfZPHF4X23b28L0fNCs4Y0OwIjY/G4w8LUnV+b eEbqYZieiuQuFZT+kj2r67JPTdA3VSmHF9ZtsIxJOh4pTZMhWgjjAbENXTO24Owhm3 7t5J3ygFlSTXX8MTDbnFzvaMKz/oUWbEBTrMKLIW9/NSI/04d2pX6PtMQkSmD2oj+y RHLGiDOxsaByY06weGvD+26QIxqMjNnVekekjz9AsFwPqhtSLxHvGkZ3bv3Q0HXpvp bod0wUecpNHe29Q24pb3ba1gmGTqr1zxxGTpjR6YQLse+oEXpFoVUEtqQD2WGCpZHy p3HAJ4h1Mq7Nw== In-Reply-To: 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:226173 Archived-At: --==-=-= Content-Type: multipart/mixed; boundary="=-=-=" --=-=-= Content-Type: text/plain Ioannis Kappas writes: > It is handled correctly as expected if the concatenated sequence is an > SGR, it is output as such, i.e. all > test/lisp/ansi-color-tests.el:ansi-color-incomplete-sequences-test > pass still pass. > > Here is the list of unit tests showing of what I consider correct > handling of non SGR sequences I have came up with thus far > > (ert-deftest ansi-color-context-non-sgr () > > [...] > > (with-temp-buffer > (let ((pretext (ansi-color-apply "\e[33;")) > (text (ansi-color-apply "1mHello World\e[0m"))) > (should (string= "Hello World" text)) > (should (equal (get-char-property 2 'font-lock-face text) > '(ansi-color-bold (:foreground "yellow3")))) > )) > ) Thanks. I took the liberty of working on your patch, adding support for ansi-color-apply-on-region, ansi-color-filter-region, ansi-color-filter-apply. I also added some tests as you suggested and made a minor simplification. --=-=-= Content-Type: text/x-patch Content-Disposition: inline; filename=0001-ansi-color-don-t-get-stuck-on-e.patch Content-Transfer-Encoding: quoted-printable From=20162045f83154d3df7b482871b05076a92efd02f9 Mon Sep 17 00:00:00 2001 From: Ioannis Kappas Date: Sun, 6 Feb 2022 21:25:56 +0100 Subject: [PATCH] ansi-color: don't get stuck on \e * lisp/ansi-color.el (ansi-color--control-seq-fragment-regexp): New constant. (ansi-color-filter-apply): (ansi-color-apply): (ansi-color-filter-region): (ansi-color-apply-on-region): Don't get stuck on \e if it is determined that it cannot start a valid ANSI escape sequence (Bug#53808). * test/lisp/ansi-color-tests.el (ansi-color-incomplete-sequences-test): Test for \e that doesn't start a valid ANSI escape sequence. =2D-- lisp/ansi-color.el | 26 ++++++++++++++++++++------ test/lisp/ansi-color-tests.el | 20 +++++++++++++++++++- 2 files changed, 39 insertions(+), 7 deletions(-) diff --git a/lisp/ansi-color.el b/lisp/ansi-color.el index 3973d9db08..e5d2e2c4ac 100644 =2D-- a/lisp/ansi-color.el +++ b/lisp/ansi-color.el @@ -347,6 +347,10 @@ ansi-color-control-seq-regexp "\e\\[[\x30-\x3F]*[\x20-\x2F]*[\x40-\x7E]" "Regexp matching an ANSI control sequence.") =20 +(defconst ansi-color--control-seq-fragment-regexp + "\e\\[[\x30-\x3F]*[\x20-\x2F]*\\|\e" + "Regexp matching a partial ANSI control sequence.") + (defconst ansi-color-parameter-regexp "\\([0-9]*\\)[m;]" "Regexp that matches SGR control sequence parameters.") =20 @@ -492,7 +496,9 @@ ansi-color-filter-apply ;; save context, add the remainder of the string to the result (let ((fragment "")) (push (substring string start =2D (if (string-match "\033" string start) + (if (string-match + (concat "\\(?:" ansi-color--control-seq-fragme= nt-regexp "\\)\\'") + string start) (let ((pos (match-beginning 0))) (setq fragment (substring string pos)) pos) @@ -549,7 +555,9 @@ 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 =2D (if (string-match "\033" string start) + (if (string-match + (concat "\\(?:" ansi-color--control-seq-fragment-regexp "\\)\\'") + string start) (let ((pos (match-beginning 0))) (setcar (cdr context) (substring string pos)) (push (substring string start pos) result)) @@ -685,7 +693,11 @@ ansi-color-filter-region (while (re-search-forward ansi-color-control-seq-regexp end-marker t) (delete-region (match-beginning 0) (match-end 0))) ;; save context, add the remainder of the string to the result =2D (if (re-search-forward "\033" end-marker t) + (set-marker start (point)) + (while (re-search-forward ansi-color--control-seq-fragment-regexp + end-marker t)) + (if (and (/=3D (point) start) + (=3D (point) end-marker)) (set-marker start (match-beginning 0)) (set-marker start nil))))) =20 @@ -742,10 +754,12 @@ ansi-color-apply-on-region ;; Otherwise, strip. (delete-region esc-beg esc-end)))) ;; search for the possible start of a new escape sequence =2D (if (re-search-forward "\033" end-marker t) + (while (re-search-forward ansi-color--control-seq-fragment-regexp + end-marker t)) + (if (and (/=3D (point) start-marker) + (=3D (point) end-marker)) (progn =2D (while (re-search-forward "\033" end-marker t)) =2D (backward-char) + (goto-char (match-beginning 0)) (funcall ansi-color-apply-face-function start-marker (point) (ansi-color--face-vec-face face-vec)) diff --git a/test/lisp/ansi-color-tests.el b/test/lisp/ansi-color-tests.el index 71b706c763..2ff7fc6aaf 100644 =2D-- a/test/lisp/ansi-color-tests.el +++ b/test/lisp/ansi-color-tests.el @@ -171,7 +171,25 @@ ansi-color-incomplete-sequences-test (insert str) (ansi-color-apply-on-region opoint (point)))) (should (ansi-color-tests-equal-props =2D propertized-str (buffer-string)))))) + propertized-str (buffer-string)))) + + ;; \e not followed by '[' and invalid ANSI escape seqences + (dolist (fun (list ansi-filt ansi-app)) + (with-temp-buffer + (should (equal (funcall fun "\e") "")) + (should (equal (funcall fun "\e[33m test \e[0m") + (with-temp-buffer + (concat "\e" (funcall fun "\e[33m test \e[0m"))))= )) + (with-temp-buffer + (should (equal (funcall fun "\e[") "")) + (should (equal (funcall fun "\e[33m Z \e[0m") + (with-temp-buffer + (concat "\e[" (funcall fun "\e[33m Z \e[0m")))))) + (with-temp-buffer + (should (equal (funcall fun "\e a \e\e[\e[") "\e a \e\e[")) + (should (equal (funcall fun "\e[33m Z \e[0m") + (with-temp-buffer + (concat "\e[" (funcall fun "\e[33m Z \e[0m"))))))= ))) =20 (provide 'ansi-color-tests) =20 =2D-=20 2.34.1 --=-=-= Content-Type: text/plain Thanks again and best regards. --=-=-=-- --==-=-= Content-Type: application/pgp-signature; name="signature.asc" -----BEGIN PGP SIGNATURE----- iQJHBAEBCAAxFiEEmxVnesoT5rQXvVXnswkaGpIVmT8FAmIAMWsTHG1paGFAa2Ft bml0bmlrLnRvcAAKCRCzCRoakhWZP+WFD/44huc4fAmaZv2HQ8YkAxGXf5ln8wDG l5rxjPYODbHBEkMpV8rAKrCM7EvCQKvZqbrAVwmj+p619akWuQbQoZSs3v3lRJDW AAuluYpckiLV6FVQb93Vl8qWfdjajua/nTdP8jxKLv/odclzYNOmiHJ5ITQxq0cE p6/5XNU4x6ONwb2A2MxZ6WN+O8GmH2k/j6cBzf1yTw1mfYKHJ83t6mjcQv8PBTyC vbs++//T6vFuz+8wn/NujhAJ6gQPpqX/KMbxhD2/hw1loh1fkzW19ky5Y2fp+w7C Rf+FNm1ZMAERq9sb02BY3/mReRmFSeLTBEybxHWonBrYOFoIf3VvJMBBPQx142Cj k7HKZLDalTLEV8CNK51dXdS0KIER4KwXBMXMS9WmhPBYrGpsIP9vgO6OuJ7oBGyI mt7IARgLYI1/UEiVkvnddWAPVF8b/ZffMLI//NXyIP3uE3g/Ep2CUoFuFo5mnhRP nhBXmOi0bo9y1h2Hs+hHrXj20IrUvOo3IoJRkzGbJckPvJFiSWEPJckOklW4fTfI JkPNc0dMaRbKkwHlwZIlz0e1ZX7mBnMC92kklw35D9KomvL3IHQ1ntWQFGsMJKBI TmYMRmrQGq6ZDTRT5KRZIO/rMkm2jlvUwPGeZ8K2bXqVRN5kM+cjX7eYF7H/kKkH C1mS6UHjhTdH7w== =cfzn -----END PGP SIGNATURE----- --==-=-=--