Subject: 25.1; Error in ansi-color-filter-apply when control sequences are dropped

From: Ivan Andrus <[email-masked]>
Date: Sat, 13 Aug 2016 23:43:20 -0600

In `ansi-color-filter-apply' the block "eliminate unrecognized escape
sequences" occurs after the "find the next escape sequence" block.  This
is problematic because the variable start is set in the "next sequence"
block and used in the "save context, add the remainder of the string to
the result" block.  However, dropping control sequences with
`ansi-color-drop-regexp' can cause the string to shorten and start to
point past the end of the string.

I merely swapped the order of the first two blocks mentioned and was
able to get past the error.  But there are likely other places that need
to be fixed as well.

I have commit access, and can make the change (as well as tracking down
other places where it might be necessary), but I would like some
feedback on whether this is the right way solve it.  Presumably there
was a reason escape sequences were dropped after start was set up.

-Ivan

diff --git a/lisp/ansi-color.el b/lisp/ansi-color.el
index 788a7bd..7cea2d9 100644
--- a/lisp/ansi-color.el
+++ b/lisp/ansi-color.el
@@ -258,14 +258,14 @@ ansi-color-filter-apply
     (if (cadr ansi-color-context)
         (setq string (concat (cadr ansi-color-context) string)
               ansi-color-context nil))
-    ;; find the next escape sequence
-    (while (setq end (string-match ansi-color-regexp string start))
-      (setq result (concat result (substring string start end))
-            start (match-end 0)))
     ;; eliminate unrecognized escape sequences
     (while (string-match ansi-color-drop-regexp string)
       (setq string
             (replace-match "" nil nil string)))
+    ;; find the next escape sequence
+    (while (setq end (string-match ansi-color-regexp string start))
+      (setq result (concat result (substring string start end))
+            start (match-end 0)))
     ;; save context, add the remainder of the string to the result
     (let (fragment)
       (if (string-match "\033" string start)

In GNU Emacs 25.1.2 (x86_64-apple-darwin15.5.0, NS appkit-1404.47 Version 10.11.6 (Build 15G31))
 of 2016-08-04 built on iandrus-osx
Repository revision: 784291b06c4e4fe52f4c1aa15bc99874abc517c4
Windowing system distributor 'Apple', version 10.3.1404
Configured using:
 'configure --with-ns --with-modules
 PKG_CONFIG_PATH=/opt/X11/lib/pkgconfig'

Configured features:
JPEG RSVG IMAGEMAGICK DBUS NOTIFY ACL GNUTLS LIBXML2 ZLIB
TOOLKIT_SCROLL_BARS NS MODULES

Important settings:
  value of $LANG: en_US.UTF-8
  locale-coding-system: utf-8-unix