Subject: 27.1; comint-redirect-results-list-from-process sometimes fails to skip over command if echoed

From: Richard Sharman <[email-masked]>
Date: Sun, 17 Jan 2021 18:51:28 -0800

When function comint-redirect-results-list-from-process is examining the
output buffer of the command it attempts to skip past the command if it
was echoed.

However, this test uses looking-at and so will fail if the command
contains characters with special meansing to regexps. It can also fail
if the command contains a trailing newline character but the
buffer may have a control-M character.

The latter problem can be fixed by calling comint-carriage-motion before
the test is done; the former by looking-at (regexp-quote command)
instead of just at command.

Note that bug 42662 (27.1; `comint-redirect-results-list-from-process’ 
treats COMMAND as a regexp) is another symptom of the former problem.

Example code to reproduce the problem:

(defun example ()
  (interactive)
  ;; create shell if not already created
  (save-window-excursion
    (shell))
  (let ((p (get-process "shell"))
	command
	reply)
    (process-send-string p "irb\n")
    (setq command "puts File.atime(\"example.el\") if File.exist?(\"example.el\")\n")
    (setq reply (comint-redirect-results-list-from-process p command "^.*$" 0))
    (process-send-string p "quit\n")	;; cleanup!
    (message (car reply))	;; this should be the a time of the file
    reply
    ))

File example.el creates a shell process then runs “irb”, the ruby
interactive shell, then sends it a command with question-marks in it.
The first line returned should be the atime of the file.

Patch:

--- comint.el.orig	2021-01-17 14:22:17.000000000 -0800
+++ comint.el	2021-01-17 16:49:47.000000000 -0800
@@ -3833,9 +3833,11 @@
 		  (accept-process-output process)))
       ;; Collect the output
       (set-buffer output-buffer)
+      (unless comint-inhibit-carriage-motion
+	(comint-carriage-motion (point-min)(point-max)))
       (goto-char (point-min))
       ;; Skip past the command, if it was echoed
-      (and (looking-at command)
+      (and (looking-at (regexp-quote command))
 	   (forward-line))
       (while (and (not (eobp))
 		  (re-search-forward regexp nil t))

Example output in *scratch* buffer before the patch:

(example)
("irb
" "puts File.atime(\"example.el\") if File.exist?(\"example.el\")
" "
" "")

and after patch applied:

(example)
("2021-01-17 18:33:45 -0800" "nil" "")

In GNU Emacs 27.1 (build 2, x86_64-apple-darwin17.7.0, NS appkit-1561.61 Version 10.13.6 (Build 17G14033))
of 2020-12-13 built on henry.local
Windowing system distributor 'Apple', version 10.3.1561
System Description:  Mac OS X 10.13.6

Recent messages:
2021-01-17 18:33:45 -0800
Result: "2021-01-17 18:33:45 -0800"
("2021-01-17 18:33:45 -0800" "nil" "")

Result: ("2021-01-17 18:33:45 -0800" "nil" "")

Result: ("2021-01-17 18:33:45 -0800" "nil" "")

Quit
Type C-x 4 C-o RET to restore the other window.  
Quit
Configured features:
NOTIFY KQUEUE ACL GNUTLS LIBXML2 ZLIB TOOLKIT_SCROLL_BARS NS MODULES
THREADS JSON PDUMPER GMP

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

Major mode: Shell

Minor modes in effect:
  shell-dirtrack-mode: t
  tooltip-mode: t
  global-eldoc-mode: t
  electric-indent-mode: t
  mouse-wheel-mode: t
  tool-bar-mode: t
  menu-bar-mode: t
  file-name-shadow-mode: t
  global-font-lock-mode: t
  font-lock-mode: t
  blink-cursor-mode: t
  auto-composition-mode: t
  auto-encryption-mode: t
  auto-compression-mode: t
  line-number-mode: t
  transient-mark-mode: t

Load-path shadows:
None found.

Features:
(shadow sort mail-extr emacsbug sendmail org-timer org-colview org-clock
org-attach org-id org-archive org-agenda ol-eww ol-rmail ol-mhe ol-irc
ol-info ol-gnus nnir gnus-sum url url-proxy url-privacy url-expand
url-methods url-history mailcap shr url-cookie url-domsuf url-util
url-parse auth-source cl-seq eieio eieio-core cl-macs eieio-loaddefs
json map url-vars svg xml dom browse-url gnus-group gnus-undo gnus-start
gnus-cloud nnimap nnmail mail-source utf7 netrc nnoo parse-time iso8601
gnus-spec gnus-int gnus-range message rmc puny rfc822 mml mml-sec
password-cache epa epg epg-config mm-decode mm-bodies mm-encode
mail-parse rfc2231 mailabbrev gmm-utils mailheader gnus-win gnus
nnheader gnus-util rmail rmail-loaddefs rfc2047 rfc2045 ietf-drums
text-property-search seq byte-opt bytecomp byte-compile cconv mail-utils
mm-util mail-prsvr wid-edit ol-docview doc-view jka-compr image-mode
exif dired dired-loaddefs ol-bibtex bibtex ol-bbdb ol-w3m reporter org
ob ob-tangle ob-ref ob-lob ob-table ob-exp org-macro org-footnote
org-src ob-comint org-pcomplete org-list org-faces org-entities noutline
outline easy-mmode org-version ob-emacs-lisp ob-core ob-eval org-table
ol org-keys org-compat advice org-macs org-loaddefs format-spec cal-menu
calendar cal-loaddefs apropos edebug gv derived time-date subr-x
help-fns radix-tree cl-print debug backtrace help-mode easymenu
find-func cl-loaddefs cl-lib shell pcomplete comint ansi-color ring
tooltip eldoc electric uniquify ediff-hook vc-hooks lisp-float-type
mwheel term/ns-win ns-win ucs-normalize mule-util term/common-win
tool-bar dnd fontset image regexp-opt fringe tabulated-list replace
newcomment text-mode elisp-mode lisp-mode prog-mode register page
tab-bar menu-bar rfn-eshadow isearch timer select scroll-bar mouse
jit-lock font-lock syntax facemenu font-core term/tty-colors frame
minibuffer cl-generic cham georgian utf-8-lang misc-lang vietnamese
tibetan thai tai-viet lao korean japanese eucjp-ms cp51932 hebrew greek
romanian slovak czech european ethiopic indian cyrillic chinese
composite charscript charprop case-table epa-hook jka-cmpr-hook help
simple abbrev obarray cl-preloaded nadvice loaddefs button faces
cus-face macroexp files text-properties overlay sha1 md5 base64 format
env code-pages mule custom widget hashtable-print-readable backquote
threads kqueue cocoa ns multi-tty make-network-process emacs)

Memory information:
((conses 16 179913 11829)
(symbols 48 19738 1)
(strings 32 63869 1510)
(string-bytes 1 2209108)
(vectors 16 29528)
(vector-slots 8 338974 19272)
(floats 8 213 69)
(intervals 56 717 0)
(buffers 1000 19))