GNU bug report logs - #7711
Patch for bug #7687

Previous Next

Package: emacs;

Reported by: Flo <sensorflo <at> gmail.com>

Date: Wed, 22 Dec 2010 18:55:02 UTC

Severity: normal

Tags: patch

Merged with 7687

Found in version 23.1.50

Done: Stefan Monnier <monnier <at> IRO.UMontreal.CA>

Bug is archived. No further changes may be made.

To add a comment to this bug, you must first unarchive it, by sending
a message to control AT debbugs.gnu.org, with unarchive 7711 in the body.
You can then email your comments to 7711 AT debbugs.gnu.org in the normal way.

Toggle the display of automated, internal messages from the tracker.

View this report as an mbox folder, status mbox, maintainer mbox


Report forwarded to owner <at> debbugs.gnu.org, bug-gnu-emacs <at> gnu.org:
bug#7711; Package emacs. (Wed, 22 Dec 2010 18:55:02 GMT) Full text and rfc822 format available.

Acknowledgement sent to Flo <sensorflo <at> gmail.com>:
New bug report received and forwarded. Copy sent to bug-gnu-emacs <at> gnu.org. (Wed, 22 Dec 2010 18:55:02 GMT) Full text and rfc822 format available.

Message #5 received at submit <at> debbugs.gnu.org (full text, mbox):

From: Flo <sensorflo <at> gmail.com>
To: bug-gnu-emacs <at> gnu.org
Subject: Patch for bug #7687
Date: Wed, 22 Dec 2010 20:00:51 +0100
What problem is solved by this patch
===========================================

GNU bug report #7687
23.1.50; shell-quote-argument does not correctly quote newlines

A copy of that bug report is at the end of this Email


Why does the patch solve the probem
===================================

Before shell-quote-argument quoted everything except POSIX
filename characters with a leading backslash. The result of
shell-quote-argument was that a new line in the input was
'quoted' with a backslash. A backslashed followed by a
newline however means line continuation to POSIX shells. It
is discarded by the shell. So the result after the shell was
the empty string. The new version of shell-quote-argument
treats newlines specially. They are quoted with single
quotes.


Same information again put differently:

old version:

input to shell-quote-argument: a newline

output of shell-quote-argument = input to the shell:
backslash followed by a newline

result of shell's quoting mechanism: the emtpy string (due
to line continuation)


new version:

input to shell-quote-argument: a newline

output of shell-quote-argument = input to the shell: newline
in single quotes

result of shell's quoting mechanism: a newline



Change Log
===========
subr.el does not yet have a change log, so I put it here

shell-quote-argument now correctly quotes newlines for POSIX
shells


Effect on other systems
========================

shell-quote-argument already distinguishes between different
system types using system-type. I only changed the part that
is concerned about POSIX shells.



diff: old version (23.1.50) first, new version second
=====================================================

$ gunzip /usr/share/emacs/23.1.50/lisp/subr.el.gz -c | diff -c -
/home/emp8118035/.emacs.d/patches/subr.el
*** STDIN Wed Dec 22 19:27:58 2010
--- /home/emp8118035/.emacs.d/patches/subr.el Mon Dec 20 17:28:30 2010
***************
*** 2391,2403 ****
  	(concat "\"" result (substring argument start) "\""))
      (if (equal argument "")
          "''"
!       ;; Quote everything except POSIX filename characters.
!       ;; This should be safe enough even for really weird shells.
!       (let ((result "") (start 0) end)
          (while (string-match "[^-0-9a-zA-Z_./]" argument start)
            (setq end (match-beginning 0)
                  result (concat result (substring argument start end)
!                                "\\" (substring argument end (1+ end)))
                  start (1+ end)))
          (concat result (substring argument start))))))

--- 2391,2407 ----
  	(concat "\"" result (substring argument start) "\""))
      (if (equal argument "")
          "''"
!       ;; Quote everything except POSIX filename characters. This
should be safe
!       ;; enough even for really weird shells. Quoting is done with backslash,
!       ;; except for newlines, which are quoted with single quotes; a
!       ;; backslash-newline sequence is interpreted as line-continuation by the
!       ;; shell and thus effectivly being ignored.
!       (let ((result "") (start 0) end ch)
          (while (string-match "[^-0-9a-zA-Z_./]" argument start)
            (setq end (match-beginning 0)
+                 ch (substring argument end (1+ end))
                  result (concat result (substring argument start end)
!                                (if (string= ch "\n") "'\n'" (concat "\\" ch)))
                  start (1+ end)))
          (concat result (substring argument start))))))




Copy of the bug report
===========================================

GNU bug report logs - #7687
23.1.50; shell-quote-argument does not correctly quote newlines


Please write in English if possible, because the Emacs maintainers
usually do not have translators to read other languages for them.

Your bug report will be posted to the emacs-pretest-bug <at> gnu.org
mailing list.

Please describe exactly what actions triggered the bug
and the precise symptoms of the bug:

When I run the following script:

----------------------------------
#!/bin/bash
emacs -Q --batch eval='(shell-command (concat "echo "
(shell-quote-argument "line1\nline2") " >tmpfile"))'
cat tmpfile
----------------------------------

I expect the following content within 'tmpfile', i.e. exactly the
string I passed to shell-quote-argument.

-------------
line1
line2
-------------

Instead I get this:

-------------
line1line2
-------------


I have not made any modification to Emacs.

I made a patch and will send it as described in the Emacs manual in
chapter 'Sending Patches for GNU Emacs'.


If Emacs crashed, and you have the Emacs process in the gdb debugger,
please include the output from the following gdb commands:
    `bt full' and `xbacktrace'.
If you would like to further debug the crash, please read the file
/usr/share/emacs/23.1.50/etc/DEBUG for instructions.


In GNU Emacs 23.1.50.1 (i486-pc-linux-gnu, GTK+ Version 2.18.0)
 of 2009-09-27 on palmer, modified by Debian
 (emacs-snapshot package, version 1:20090909-1)
Windowing system distributor `The X.Org Foundation', version 11.0.10706000
configured using `configure  '--build' 'i486-linux-gnu' '--host'
'i486-linux-gnu' '--prefix=/usr' '--sharedstatedir=/var/lib'
'--libexecdir=/usr/lib' '--localstatedir=/var'
'--infodir=/usr/share/info' '--mandir=/usr/share/man' '--with-pop=yes'
'--enable-locallisppath=/etc/emacs-snapshot:/etc/emacs:/usr/local/share/emacs/23.1.50/site-lisp:/usr/local/share/emacs/site-lisp:/usr/share/emacs/23.1.50/site-lisp:/usr/share/emacs/site-lisp'
'--with-x=yes' '--with-x-toolkit=gtk' 'build_alias=i486-linux-gnu'
'host_alias=i486-linux-gnu' 'CFLAGS=-DDEBIAN
-DSITELOAD_PURESIZE_EXTRA=5000 -g -O2' 'LDFLAGS=-g -Wl,--as-needed'
'CPPFLAGS=''

Important settings:
  value of $LC_ALL: nil
  value of $LC_COLLATE: nil
  value of $LC_CTYPE: nil
  value of $LC_MESSAGES: nil
  value of $LC_MONETARY: nil
  value of $LC_NUMERIC: nil
  value of $LC_TIME: nil
  value of $LANG: en_US.utf8
  value of $XMODIFIERS: nil
  locale-coding-system: utf-8-unix
  default enable-multibyte-characters: t

Major mode: Info

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

Recent input:
M-x b u g - r e p o <tab> C-g C-h i C-h r C-s b u g
C-s <return> <return> 2 C-v SPC M-x r e p o r t - e
m a c s - b u <tab> <return>

Recent messages:
uncompressing emacs-6.gz...done
uncompressing emacs-7.gz...done
uncompressing emacs-8.gz...done
Mark saved where search started
uncompressing emacs.gz...done
uncompressing emacs-6.gz...done
uncompressing emacs.gz...done
uncompressing emacs-6.gz...done
uncompressing emacs.gz...done
uncompressing emacs-6.gz...done

Load-path shadows:
/usr/share/emacs/23.1.50/site-lisp/debian-startup hides
/usr/share/emacs/site-lisp/debian-startup
/usr/share/emacs/site-lisp/rst hides /usr/share/emacs/23.1.50/lisp/textmodes/rst




Merged 7687 7711. Request was from Stefan Monnier <monnier <at> iro.umontreal.ca> to control <at> debbugs.gnu.org. (Thu, 23 Dec 2010 15:08:02 GMT) Full text and rfc822 format available.

Information forwarded to owner <at> debbugs.gnu.org, bug-gnu-emacs <at> gnu.org:
bug#7711; Package emacs. (Fri, 21 Jan 2011 18:00:03 GMT) Full text and rfc822 format available.

Message #10 received at 7711 <at> debbugs.gnu.org (full text, mbox):

From: Stefan Monnier <monnier <at> IRO.UMontreal.CA>
To: Flo <sensorflo <at> gmail.com>
Cc: 7711 <at> debbugs.gnu.org
Subject: Re: bug#7711: Patch for bug #7687
Date: Fri, 21 Jan 2011 13:07:30 -0500
forcemerge 7687 7711
close 7711
thanks

> the empty string. The new version of shell-quote-argument
> treats newlines specially. They are quoted with single
> quotes.

Thanks.  I've installed a slightly different patch (took the opportunity
to simplify the code by using replace-regexp-in-string) that implements
your idea.


        Stefan


=== modified file 'lisp/ChangeLog'
--- lisp/ChangeLog	2011-01-21 05:04:45 +0000
+++ lisp/ChangeLog	2011-01-21 18:04:16 +0000
@@ -1,3 +1,8 @@
+2011-01-21  Stefan Monnier  <monnier <at> iro.umontreal.ca>
+
+	* subr.el (shell-quote-argument): Properly quote \n (bug#7687).
+	Suggested by Flo <sensorflo <at> gmail.com>.
+
 2011-01-21  Glenn Morris  <rgm <at> gnu.org>
 
 	* progmodes/compile.el (compilation-error-regexp-alist):

=== modified file 'lisp/subr.el'
--- lisp/subr.el	2011-01-15 02:32:19 +0000
+++ lisp/subr.el	2011-01-21 18:04:45 +0000
@@ -2426,13 +2426,8 @@
         "''"
       ;; Quote everything except POSIX filename characters.
       ;; This should be safe enough even for really weird shells.
-      (let ((result "") (start 0) end)
-        (while (string-match "[^-0-9a-zA-Z_./]" argument start)
-          (setq end (match-beginning 0)
-                result (concat result (substring argument start end)
-                               "\\" (substring argument end (1+ end)))
-                start (1+ end)))
-        (concat result (substring argument start))))))
+      (replace-regexp-in-string "\n" "'\n'"
+       (replace-regexp-in-string "[^-0-9a-zA-Z_./\n]" "\\\\\\&" argument)))))
 
 (defun string-or-null-p (object)
   "Return t if OBJECT is a string or nil.





Forcibly Merged 7687 7711. Request was from Stefan Monnier <monnier <at> IRO.UMontreal.CA> to control <at> debbugs.gnu.org. (Fri, 21 Jan 2011 18:00:04 GMT) Full text and rfc822 format available.

bug closed, send any further explanations to Flo <sensorflo <at> gmail.com> Request was from Stefan Monnier <monnier <at> IRO.UMontreal.CA> to control <at> debbugs.gnu.org. (Fri, 21 Jan 2011 18:00:04 GMT) Full text and rfc822 format available.

bug archived. Request was from Debbugs Internal Request <help-debbugs <at> gnu.org> to internal_control <at> debbugs.gnu.org. (Sat, 19 Feb 2011 12:24:04 GMT) Full text and rfc822 format available.

This bug report was last modified 14 years and 122 days ago.

Previous Next


GNU bug tracking system
Copyright (C) 1999 Darren O. Benham, 1997,2003 nCipher Corporation Ltd, 1994-97 Ian Jackson.