From MAILER-DAEMON Thu Aug 02 16:02:22 2012
Received: from list by lists.gnu.org with archive (Exim 4.71)
	id 1Sx1bC-0006zY-SV
	for mharc-bug-diffutils@gnu.org; Thu, 02 Aug 2012 16:02:22 -0400
Received: from eggs.gnu.org ([208.118.235.92]:60654)
	by lists.gnu.org with esmtp (Exim 4.71)
	(envelope-from <andreas.gruenbacher@gmail.com>) id 1Sx1bA-0006ut-JZ
	for bug-diffutils@gnu.org; Thu, 02 Aug 2012 16:02:21 -0400
Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71)
	(envelope-from <andreas.gruenbacher@gmail.com>) id 1Sx1b9-0001z4-OX
	for bug-diffutils@gnu.org; Thu, 02 Aug 2012 16:02:20 -0400
Received: from mail-lpp01m010-f41.google.com ([209.85.215.41]:60958)
	by eggs.gnu.org with esmtp (Exim 4.71)
	(envelope-from <andreas.gruenbacher@gmail.com>)
	id 1Sx1b3-0001yE-Hc; Thu, 02 Aug 2012 16:02:13 -0400
Received: by lahi5 with SMTP id i5so6041288lah.0
	for <multiple recipients>; Thu, 02 Aug 2012 13:02:12 -0700 (PDT)
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113;
	h=mime-version:in-reply-to:references:date:message-id:subject:from:to
	:cc:content-type:content-transfer-encoding;
	bh=1iT2MFEKzsNzumstfjewjepzsT6uXXSp8tovTl41nII=;
	b=EDvGb/a5fLRMf0xuAyIDBsrXh75hafkS28qQT8sLBNGX16T8W8oDamX0lqHLClANrr
	U4GytiucOHTV2eo7JeYzDxNtnVQKXU/JmkDknOns/iAWLvzbJ42V/tqMCTugjJEN/ZV0
	Zjih+A7geuU9L3Bu2ReTPQPPxsurNM7TOVNmryX92ZLUcqQsCcl2YdMuNJj3PgGoXY1Y
	q9g8F8E+5q6k0LCT66/ZDgsxo34td6t5X6jZt7MaG6g6ZHUDFb1B1ISojnazhnwqdybN
	+iiNb9wdkpm4AiLNbgwOt+BKYetmv8YEJD4tmsbfxeraCY7Xamc+G89+QLGInORq2gDU
	ti8A==
MIME-Version: 1.0
Received: by 10.112.43.67 with SMTP id u3mr10107642lbl.16.1343937732181; Thu,
	02 Aug 2012 13:02:12 -0700 (PDT)
Received: by 10.112.149.38 with HTTP; Thu, 2 Aug 2012 13:02:12 -0700 (PDT)
In-Reply-To: <7v8vdyfqa9.fsf@alter.siamese.dyndns.org>
References: <CAHpGcM+DkL_CCisCjF9m0j3NRJUNAdfL05T0cfLjLfVORQHrpw@mail.gmail.com>
	<7v8vdyfqa9.fsf@alter.siamese.dyndns.org>
Date: Thu, 2 Aug 2012 22:02:12 +0200
Message-ID: <CAHpGcML5PVAgd9x75=5k1-ev+Px4V+26KJhhyZk-O54p0WxfxQ@mail.gmail.com>
From: =?ISO-8859-1?Q?Andreas_Gr=FCnbacher?= <andreas.gruenbacher@gmail.com>
To: Junio C Hamano <gitster@pobox.com>
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable
X-detected-operating-system: by eggs.gnu.org: Genre and OS details not
	recognized.
X-Received-From: 209.85.215.41
Cc: bug-diffutils@gnu.org, Jim Meyering <meyering@redhat.com>,
	bug-patch@gnu.org, git@vger.kernel.org
Subject: Re: [bug-diffutils] GNU patch close to next stable release (2)
X-BeenThere: bug-diffutils@gnu.org
X-Mailman-Version: 2.1.14
Precedence: list
List-Id: "All diffutils discussion." <bug-diffutils.gnu.org>
List-Unsubscribe: <https://lists.gnu.org/mailman/options/bug-diffutils>,
	<mailto:bug-diffutils-request@gnu.org?subject=unsubscribe>
List-Archive: <http://lists.gnu.org/archive/html/bug-diffutils>
List-Post: <mailto:bug-diffutils@gnu.org>
List-Help: <mailto:bug-diffutils-request@gnu.org?subject=help>
List-Subscribe: <https://lists.gnu.org/mailman/listinfo/bug-diffutils>,
	<mailto:bug-diffutils-request@gnu.org?subject=subscribe>
X-List-Received-Date: Thu, 02 Aug 2012 20:02:21 -0000

Junio,

2012/8/1 Junio C Hamano <gitster@pobox.com>:
> Andreas Gr=FCnbacher <agruen@gnu.org> writes:
>
>> * Support for double-quoted filenames in the "diff --git" format: when a
>>   filename starts with a double quote, it is interpreted as a C string
>>   literal.  The escape sequences \\, \", \a, \b, \f, \n, \r, \t, \v, and=
 \ooo
>>   (a three-digit octal number between 0 and 255) are recognized.
>
> Is this also available in non-git diffs?

I have only enabled this for git-style diffs for now. GNU diff doesn't do
filename quoting so far and if it eventually implements a slightly differen=
t
form of quoting (which I wouldn't recommend), we might be in trouble.

> IIRC, this extension to allow funny characters in pathnames was
> proposed by and discussed with Paul Eggert (listed as one of GNU
> diffutils maintainers at www.gnu.org/software/diffutils/) and git happene=
d
> to adopt it earlier than "GNU diff" itself did.

Paul and Jim, any thoughts about adding filename quoting to GNU diff anytim=
e
soon, or about the format?

Thanks,
Andreas


From MAILER-DAEMON Thu Aug 02 17:08:45 2012
Received: from list by lists.gnu.org with archive (Exim 4.71)
	id 1Sx2dR-00086A-Gt
	for mharc-bug-diffutils@gnu.org; Thu, 02 Aug 2012 17:08:45 -0400
Received: from eggs.gnu.org ([208.118.235.92]:49923)
	by lists.gnu.org with esmtp (Exim 4.71)
	(envelope-from <eggert@cs.ucla.edu>) id 1Sx2dP-00085w-Cs
	for bug-diffutils@gnu.org; Thu, 02 Aug 2012 17:08:44 -0400
Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71)
	(envelope-from <eggert@cs.ucla.edu>) id 1Sx2dO-00057C-JD
	for bug-diffutils@gnu.org; Thu, 02 Aug 2012 17:08:43 -0400
Received: from smtp.cs.ucla.edu ([131.179.128.62]:53828)
	by eggs.gnu.org with esmtp (Exim 4.71)
	(envelope-from <eggert@cs.ucla.edu>)
	id 1Sx2dO-00056j-Dq; Thu, 02 Aug 2012 17:08:42 -0400
Received: from localhost (localhost.localdomain [127.0.0.1])
	by smtp.cs.ucla.edu (Postfix) with ESMTP id B2287A60009;
	Thu,  2 Aug 2012 14:08:40 -0700 (PDT)
X-Virus-Scanned: amavisd-new at smtp.cs.ucla.edu
Received: from smtp.cs.ucla.edu ([127.0.0.1])
	by localhost (smtp.cs.ucla.edu [127.0.0.1]) (amavisd-new, port 10024)
	with ESMTP id JnzNeATYyr9X; Thu,  2 Aug 2012 14:08:40 -0700 (PDT)
Received: from [192.168.1.4] (pool-108-23-119-2.lsanca.fios.verizon.net
	[108.23.119.2])
	by smtp.cs.ucla.edu (Postfix) with ESMTPSA id 54EEBA60007;
	Thu,  2 Aug 2012 14:08:40 -0700 (PDT)
Message-ID: <501AEC59.7010907@cs.ucla.edu>
Date: Thu, 02 Aug 2012 14:08:41 -0700
From: Paul Eggert <eggert@cs.ucla.edu>
Organization: UCLA Computer Science Department
User-Agent: Mozilla/5.0 (X11; Linux i686;
	rv:14.0) Gecko/20120714 Thunderbird/14.0
MIME-Version: 1.0
To: =?UTF-8?B?QW5kcmVhcyBHcsO8bmJhY2hlcg==?=
 <andreas.gruenbacher@gmail.com>
References: <CAHpGcM+DkL_CCisCjF9m0j3NRJUNAdfL05T0cfLjLfVORQHrpw@mail.gmail.com>
	<7v8vdyfqa9.fsf@alter.siamese.dyndns.org>
	<CAHpGcML5PVAgd9x75=5k1-ev+Px4V+26KJhhyZk-O54p0WxfxQ@mail.gmail.com>
In-Reply-To: <CAHpGcML5PVAgd9x75=5k1-ev+Px4V+26KJhhyZk-O54p0WxfxQ@mail.gmail.com>
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: quoted-printable
X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.6 (newer, 3)
X-Received-From: 131.179.128.62
Cc: bug-diffutils@gnu.org, Jim Meyering <meyering@redhat.com>,
	bug-patch@gnu.org, git@vger.kernel.org, Junio C Hamano <gitster@pobox.com>
Subject: Re: [bug-diffutils] GNU patch close to next stable release (2)
X-BeenThere: bug-diffutils@gnu.org
X-Mailman-Version: 2.1.14
Precedence: list
List-Id: "All diffutils discussion." <bug-diffutils.gnu.org>
List-Unsubscribe: <https://lists.gnu.org/mailman/options/bug-diffutils>,
	<mailto:bug-diffutils-request@gnu.org?subject=unsubscribe>
List-Archive: <http://lists.gnu.org/archive/html/bug-diffutils>
List-Post: <mailto:bug-diffutils@gnu.org>
List-Help: <mailto:bug-diffutils-request@gnu.org?subject=help>
List-Subscribe: <https://lists.gnu.org/mailman/listinfo/bug-diffutils>,
	<mailto:bug-diffutils-request@gnu.org?subject=subscribe>
X-List-Received-Date: Thu, 02 Aug 2012 21:08:44 -0000

On 08/02/2012 01:02 PM, Andreas Gr=C3=BCnbacher wrote:
> Paul and Jim, any thoughts about adding filename quoting to GNU diff an=
ytime
> soon, or about the format?

The git format looks OK.  I don't have time right now to add it to GNU di=
ff, tho.


From MAILER-DAEMON Thu Aug 02 17:13:53 2012
Received: from list by lists.gnu.org with archive (Exim 4.71)
	id 1Sx2iP-0001CL-Jh
	for mharc-bug-diffutils@gnu.org; Thu, 02 Aug 2012 17:13:53 -0400
Received: from eggs.gnu.org ([208.118.235.92]:50542)
	by lists.gnu.org with esmtp (Exim 4.71)
	(envelope-from <jim@meyering.net>) id 1Sx2iN-0001C4-Su
	for bug-diffutils@gnu.org; Thu, 02 Aug 2012 17:13:52 -0400
Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71)
	(envelope-from <jim@meyering.net>) id 1Sx2iN-0007xf-2d
	for bug-diffutils@gnu.org; Thu, 02 Aug 2012 17:13:51 -0400
Received: from mx.meyering.net ([88.168.87.75]:47617)
	by eggs.gnu.org with esmtp (Exim 4.71)
	(envelope-from <jim@meyering.net>)
	id 1Sx2iM-0007xN-Pa; Thu, 02 Aug 2012 17:13:50 -0400
Received: from rho.meyering.net (rho.meyering.net [127.0.0.1])
	by rho.meyering.net (Acme Bit-Twister) with ESMTP id 439A5600AE;
	Thu,  2 Aug 2012 23:13:49 +0200 (CEST)
From: Jim Meyering <jim@meyering.net>
To: Paul Eggert <eggert@cs.ucla.edu>
In-Reply-To: <501AEC59.7010907@cs.ucla.edu> (Paul Eggert's message of "Thu, 02
	Aug 2012 14:08:41 -0700")
References: <CAHpGcM+DkL_CCisCjF9m0j3NRJUNAdfL05T0cfLjLfVORQHrpw@mail.gmail.com>
	<7v8vdyfqa9.fsf@alter.siamese.dyndns.org>
	<CAHpGcML5PVAgd9x75=5k1-ev+Px4V+26KJhhyZk-O54p0WxfxQ@mail.gmail.com>
	<501AEC59.7010907@cs.ucla.edu>
Date: Thu, 02 Aug 2012 23:13:49 +0200
Message-ID: <877gtht2sy.fsf@rho.meyering.net>
Lines: 9
MIME-Version: 1.0
Content-Type: text/plain; charset=iso-8859-1
Content-Transfer-Encoding: quoted-printable
X-detected-operating-system: by eggs.gnu.org: Genre and OS details not
	recognized.
X-Received-From: 88.168.87.75
Cc: bug-diffutils@gnu.org,
	Andreas =?iso-8859-1?Q?Gr=FCnbacher?= <andreas.gruenbacher@gmail.com>,
	Junio C Hamano <gitster@pobox.com>, bug-patch@gnu.org, git@vger.kernel.org
Subject: Re: [bug-diffutils] GNU patch close to next stable release (2)
X-BeenThere: bug-diffutils@gnu.org
X-Mailman-Version: 2.1.14
Precedence: list
List-Id: "All diffutils discussion." <bug-diffutils.gnu.org>
List-Unsubscribe: <https://lists.gnu.org/mailman/options/bug-diffutils>,
	<mailto:bug-diffutils-request@gnu.org?subject=unsubscribe>
List-Archive: <http://lists.gnu.org/archive/html/bug-diffutils>
List-Post: <mailto:bug-diffutils@gnu.org>
List-Help: <mailto:bug-diffutils-request@gnu.org?subject=help>
List-Subscribe: <https://lists.gnu.org/mailman/listinfo/bug-diffutils>,
	<mailto:bug-diffutils-request@gnu.org?subject=subscribe>
X-List-Received-Date: Thu, 02 Aug 2012 21:13:52 -0000

Paul Eggert wrote:
> On 08/02/2012 01:02 PM, Andreas Gr=FCnbacher wrote:
>> Paul and Jim, any thoughts about adding filename quoting to GNU diff any=
time
>> soon, or about the format?
>
> The git format looks OK.  I don't have time right now to add it to GNU di=
ff, tho.

I agree, and have no more free time for the task than Paul does.
Would someone care to contribute a complete patch?


From MAILER-DAEMON Mon Aug 06 21:03:07 2012
Received: from list by lists.gnu.org with archive (Exim 4.71)
	id 1SyYCR-0005Dk-5O
	for mharc-bug-diffutils@gnu.org; Mon, 06 Aug 2012 21:03:07 -0400
Received: from eggs.gnu.org ([208.118.235.92]:57761)
	by lists.gnu.org with esmtp (Exim 4.71)
	(envelope-from <andreas.gruenbacher@gmail.com>) id 1SyYCO-0005Da-Nq
	for bug-diffutils@gnu.org; Mon, 06 Aug 2012 21:03:05 -0400
Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71)
	(envelope-from <andreas.gruenbacher@gmail.com>) id 1SyYCN-0002ZX-CS
	for bug-diffutils@gnu.org; Mon, 06 Aug 2012 21:03:04 -0400
Received: from mail-wg0-f49.google.com ([74.125.82.49]:53353)
	by eggs.gnu.org with esmtp (Exim 4.71)
	(envelope-from <andreas.gruenbacher@gmail.com>) id 1SyYCN-0002Z6-1X
	for bug-diffutils@gnu.org; Mon, 06 Aug 2012 21:03:03 -0400
Received: by wgbez12 with SMTP id ez12so2395093wgb.30
	for <bug-diffutils@gnu.org>; Mon, 06 Aug 2012 18:03:01 -0700 (PDT)
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113;
	h=message-id:subject:from:to:cc:date:in-reply-to:references
	:content-type:x-mailer:content-transfer-encoding:mime-version;
	bh=NMBkh+fvmKLl+XYxlsxS6bXmuMUeu+bC48CWnWPmbUY=;
	b=iSDCpD+vSukWLmhucYSr+tNvJGs1fG1MQf0oUd/T9vxmabAxyKqKUqsNJAjZd6aJaa
	+YVvmJfoIVgYAgVVpSCJGkqVzvwBFzB7dtWeCxMdhzQrKnLrcR0d2mbvtfYdlrjt8Jhz
	aC3c1SBxdeBSs+NcxVOKz8ow4eVI8CL1lIwhZCP3Iktnzp/wv1tx9lundV5Qk0SJLk33
	rU5lVG9HbYPFLwvA+ZOpxgkz/RVRTJ1gzot59HkhSWOJlpN5Bd485LNYRIGyIyObvjsx
	wnynoMPpREol3y3IKKCjm1CEwSB/3r2OFIynNyWy+yMOtVlJPJB5bZ8Vj2jQyTfj8NT0
	7sWQ==
Received: by 10.180.76.135 with SMTP id k7mr22815756wiw.7.1344301381055;
	Mon, 06 Aug 2012 18:03:01 -0700 (PDT)
Received: from [192.168.91.241] (chello084114015216.14.vie.surfer.at.
	[84.114.15.216])
	by mx.google.com with ESMTPS id fr4sm27238274wib.8.2012.08.06.18.02.59
	(version=TLSv1/SSLv3 cipher=OTHER);
	Mon, 06 Aug 2012 18:03:00 -0700 (PDT)
Message-ID: <1344301378.26363.6.camel@schurl.lan>
From: Andreas Gruenbacher <andreas.gruenbacher@gmail.com>
To: Jim Meyering <jim@meyering.net>, Paul Eggert <eggert@cs.ucla.edu>
Date: Tue, 07 Aug 2012 03:02:58 +0200
In-Reply-To: <877gtht2sy.fsf@rho.meyering.net>
References: <CAHpGcM+DkL_CCisCjF9m0j3NRJUNAdfL05T0cfLjLfVORQHrpw@mail.gmail.com>
	<7v8vdyfqa9.fsf@alter.siamese.dyndns.org>
	<CAHpGcML5PVAgd9x75=5k1-ev+Px4V+26KJhhyZk-O54p0WxfxQ@mail.gmail.com>
	<501AEC59.7010907@cs.ucla.edu> <877gtht2sy.fsf@rho.meyering.net>
Content-Type: text/plain; charset="UTF-8"
X-Mailer: Evolution 3.2.3 (3.2.3-3.fc16) 
Content-Transfer-Encoding: 8bit
Mime-Version: 1.0
X-detected-operating-system: by eggs.gnu.org: Genre and OS details not
	recognized.
X-Received-From: 74.125.82.49
Cc: bug-diffutils@gnu.org
Subject: [bug-diffutils] [PATCH] Escape file names that contain special
	characters
X-BeenThere: bug-diffutils@gnu.org
X-Mailman-Version: 2.1.14
Precedence: list
List-Id: "All diffutils discussion." <bug-diffutils.gnu.org>
List-Unsubscribe: <https://lists.gnu.org/mailman/options/bug-diffutils>,
	<mailto:bug-diffutils-request@gnu.org?subject=unsubscribe>
List-Archive: <http://lists.gnu.org/archive/html/bug-diffutils>
List-Post: <mailto:bug-diffutils@gnu.org>
List-Help: <mailto:bug-diffutils-request@gnu.org?subject=help>
List-Subscribe: <https://lists.gnu.org/mailman/listinfo/bug-diffutils>,
	<mailto:bug-diffutils-request@gnu.org?subject=subscribe>
X-List-Received-Date: Tue, 07 Aug 2012 01:03:06 -0000

On Don, 2012-08-02 at 23:13 +0200, Jim Meyering wrote:
> Paul Eggert wrote:
> > On 08/02/2012 01:02 PM, Andreas Grünbacher wrote:
> >> Paul and Jim, any thoughts about adding filename quoting to GNU diff anytime
> >> soon, or about the format?
> >
> > The git format looks OK.  I don't have time right now to add it to GNU diff, tho.
> 
> I agree, and have no more free time for the task than Paul does.
> Would someone care to contribute a complete patch?

Here's an attempt.  Needs to be mentioned in NEWS still at least.  Can
one of you give this a close look?

Thanks,
Andreas


From: Andreas Gruenbacher <agruen@gnu.org>
Date: Tue, 7 Aug 2012 02:34:24 +0200
Subject: [PATCH] diff: Escape file names that contain special characters

If a file name contains space, '"', '\\', or control characters, it is enclosed
in double quotes, and all characters other than space are backslash escaped as
in C strings.
* src/util.c (c_escape_char): New function.
(c_escape): New function.
(begin_output): Escape file names when needed.
* src/context.c (print_context_header): New names parameter.
(print_context_label): New name parameter.
* src/diff.h (print_context_header): Change prototype.
---
 src/context.c |   13 ++++---
 src/diff.h    |    2 +-
 src/util.c    |  100 ++++++++++++++++++++++++++++++++++++++++++++++++++++++---
 3 files changed, 103 insertions(+), 12 deletions(-)

diff --git a/src/context.c b/src/context.c
index b73d5c3..53aa203 100644
--- a/src/context.c
+++ b/src/context.c
@@ -40,6 +40,7 @@ static lin find_function_last_match;
 static void
 print_context_label (char const *mark,
 		     struct file_data *inf,
+		     char const *name,
 		     char const *label)
 {
   if (label)
@@ -70,24 +71,24 @@ print_context_label (char const *mark,
 	      sprintf (buf, "%"PRIuMAX".%.9d", sec, nsec);
 	    }
 	}
-      fprintf (outfile, "%s %s\t%s\n", mark, inf->name, buf);
+      fprintf (outfile, "%s %s\t%s\n", mark, name, buf);
     }
 }
 
 /* Print a header for a context diff, with the file names and dates.  */
 
 void
-print_context_header (struct file_data inf[], bool unidiff)
+print_context_header (struct file_data inf[], char const *const *names, bool unidiff)
 {
   if (unidiff)
     {
-      print_context_label ("---", &inf[0], file_label[0]);
-      print_context_label ("+++", &inf[1], file_label[1]);
+      print_context_label ("---", &inf[0], names[0], file_label[0]);
+      print_context_label ("+++", &inf[1], names[1], file_label[1]);
     }
   else
     {
-      print_context_label ("***", &inf[0], file_label[0]);
-      print_context_label ("---", &inf[1], file_label[1]);
+      print_context_label ("***", &inf[0], names[0], file_label[0]);
+      print_context_label ("---", &inf[1], names[1], file_label[1]);
     }
 }
 
diff --git a/src/diff.h b/src/diff.h
index 4b6595c..212a8d0 100644
--- a/src/diff.h
+++ b/src/diff.h
@@ -332,7 +332,7 @@ XTERN FILE *outfile;
 extern int diff_2_files (struct comparison *);
 
 /* context.c */
-extern void print_context_header (struct file_data[], bool);
+extern void print_context_header (struct file_data[], char const * const *, bool);
 extern void print_context_script (struct change *, bool);
 
 /* dir.c */
diff --git a/src/util.c b/src/util.c
index 38b33ef..ca6ef2a 100644
--- a/src/util.c
+++ b/src/util.c
@@ -166,24 +166,109 @@ setup_output (char const *name0, char const *name1, bool recursive)
 static pid_t pr_pid;
 #endif
 
+static char c_escape_char (char c)
+{
+  switch (c) {
+    case '\a': return 'a';
+    case '\b': return 'b';
+    case '\t': return 't';
+    case '\n': return 'n';
+    case '\v': return 'v';
+    case '\f': return 'f';
+    case '\r': return 'r';
+    case '"': return '"';
+    case '\\': return '\\';
+    default:
+      return c < 32;
+  }
+}
+
+char *
+c_escape (char const *str)
+{
+  char const *s;
+  unsigned int plus = 0;
+  bool must_quote = false;
+
+  for (s = str; *s; s++)
+    {
+      char c = *s;
+
+      if (c == ' ')
+	{
+	  must_quote = true;
+	  continue;
+	}
+      switch (c_escape_char (*s))
+	{
+	  case 1:
+	    plus += 3;
+	    /* fall through */
+	  case 0:
+	    break;
+	  default:
+	    plus++;
+	    break;
+	}
+    }
+
+  if (must_quote || plus)
+    {
+      char *buffer, *b;
+
+      b = buffer = xmalloc (strlen (str) + plus + 3);
+      *b++ = '"';
+      for (s = str; *s; s++)
+	{
+	  char c = *s;
+	  char escape = c_escape_char (c);
+
+	  switch (escape)
+	    {
+	      case 0:
+		*b++ = c;
+		break;
+	      case 1:
+		*b++ = '\\';
+		*b++ = ((c >> 6) & 03) + '0';
+		*b++ = ((c >> 3) & 07) + '0';
+		*b++ = ((c >> 0) & 07) + '0';
+		break;
+	      default:
+		*b++ = '\\';
+		*b++ = escape;
+		break;
+	    }
+	}
+      *b++ = '"';
+      *b = 0;
+      return buffer;
+    }
+  else
+    return (char *) str;
+}
+
 void
 begin_output (void)
 {
+  char *names[2];
   char *name;
 
   if (outfile != 0)
     return;
 
+  names[0] = c_escape (current_name0);
+  names[1] = c_escape (current_name1);
+
   /* Construct the header of this piece of diff.  */
-  name = xmalloc (strlen (current_name0) + strlen (current_name1)
-		  + strlen (switch_string) + 7);
+  name = xmalloc (strlen (names[0]) + strlen (names[1]) + strlen (switch_string) + 7);
 
   /* POSIX 1003.1-2001 specifies this format.  But there are some bugs in
      the standard: it says that we must print only the last component
      of the pathnames, and it requires two spaces after "diff" if
      there are no options.  These requirements are silly and do not
      match historical practice.  */
-  sprintf (name, "diff%s %s %s", switch_string, current_name0, current_name1);
+  sprintf (name, "diff%s %s %s", switch_string, names[0], names[1]);
 
   if (paginate)
     {
@@ -258,16 +343,21 @@ begin_output (void)
   switch (output_style)
     {
     case OUTPUT_CONTEXT:
-      print_context_header (files, false);
+      print_context_header (files, (char const *const *)names, false);
       break;
 
     case OUTPUT_UNIFIED:
-      print_context_header (files, true);
+      print_context_header (files, (char const *const *)names, true);
       break;
 
     default:
       break;
     }
+
+  if (names[0] != current_name0)
+    free (names[0]);
+  if (names[1] != current_name1)
+    free (names[1]);
 }
 
 /* Call after the end of output of diffs for one file.
-- 
1.7.7.6



From MAILER-DAEMON Mon Aug 13 18:44:55 2012
Received: from list by lists.gnu.org with archive (Exim 4.71)
	id 1T13NX-0002Le-TR
	for mharc-bug-diffutils@gnu.org; Mon, 13 Aug 2012 18:44:55 -0400
Received: from eggs.gnu.org ([208.118.235.92]:49645)
	by lists.gnu.org with esmtp (Exim 4.71)
	(envelope-from <andreas.gruenbacher@gmail.com>) id 1T139w-0007cs-QZ
	for bug-diffutils@gnu.org; Mon, 13 Aug 2012 18:30:55 -0400
Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71)
	(envelope-from <andreas.gruenbacher@gmail.com>) id 1T139u-0001T2-Ii
	for bug-diffutils@gnu.org; Mon, 13 Aug 2012 18:30:52 -0400
Received: from mail-wg0-f49.google.com ([74.125.82.49]:55283)
	by eggs.gnu.org with esmtp (Exim 4.71)
	(envelope-from <andreas.gruenbacher@gmail.com>) id 1T139u-0001RI-7c
	for bug-diffutils@gnu.org; Mon, 13 Aug 2012 18:30:50 -0400
Received: by wgbez12 with SMTP id ez12so3107583wgb.30
	for <bug-diffutils@gnu.org>; Mon, 13 Aug 2012 15:30:49 -0700 (PDT)
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113;
	h=sender:message-id:subject:from:to:cc:date:content-type:x-mailer
	:content-transfer-encoding:mime-version;
	bh=BG7CgVKkETdXox3O0QhOCjuP26q8yb6e9lOhAgDCvtE=;
	b=WtVqNrtsSv87dMBJarbaK25S0Y+89uHYsNwo5LnYg76A1uCc92WkOthnBFUA38NNCH
	yqCgZIAsmPvHGvxHEaB4p3lyl1CKbJT3UUZwNsnUbeX1N7Vw5qd00J9NKKZ0oeYDuuv5
	viV5H5gD5h+Awe6NP5dpfjChj+SPYFE9C9eIxn07nXse8R3NS5l4OOXNudL5OLH5WEa1
	dngZ2LEHIU65h5TJeoj0VQdDL7HCeL64hegb14OjbAM5BXZgTSYLyxHfiXBcyu3336dg
	JpMU59DI8uywzCOvBIJWwbU/Rrs9k6MrCxDaNQhEDkr1CSWCib1dCHIb6etcVNR+rQdo
	FmLQ==
Received: by 10.216.232.204 with SMTP id n54mr7553513weq.182.1344897049116;
	Mon, 13 Aug 2012 15:30:49 -0700 (PDT)
Received: from [192.168.91.241] (chello084114015216.14.vie.surfer.at.
	[84.114.15.216])
	by mx.google.com with ESMTPS id w7sm18945538wiz.0.2012.08.13.15.30.47
	(version=TLSv1/SSLv3 cipher=OTHER);
	Mon, 13 Aug 2012 15:30:48 -0700 (PDT)
Sender: =?UTF-8?Q?Andreas_Gr=C3=BCnbacher?= <andreas.gruenbacher@gmail.com>
Message-ID: <1344897046.13604.7.camel@schurl.lan>
From: Andreas Gruenbacher <agruen@gnu.org>
To: bug-diffutils@gnu.org
Date: Tue, 14 Aug 2012 00:30:46 +0200
Content-Type: text/plain; charset="UTF-8"
X-Mailer: Evolution 3.2.3 (3.2.3-3.fc16)
Content-Transfer-Encoding: 7bit
Mime-Version: 1.0
X-detected-operating-system: by eggs.gnu.org: Genre and OS details not
	recognized.
X-Received-From: 74.125.82.49
X-Mailman-Approved-At: Mon, 13 Aug 2012 18:44:54 -0400
Subject: [bug-diffutils] [PATCH] diff: Encode file names with special
	characters
X-BeenThere: bug-diffutils@gnu.org
X-Mailman-Version: 2.1.14
Precedence: list
List-Id: "All diffutils discussion." <bug-diffutils.gnu.org>
List-Unsubscribe: <https://lists.gnu.org/mailman/options/bug-diffutils>,
	<mailto:bug-diffutils-request@gnu.org?subject=unsubscribe>
List-Archive: <http://lists.gnu.org/archive/html/bug-diffutils>
List-Post: <mailto:bug-diffutils@gnu.org>
List-Help: <mailto:bug-diffutils-request@gnu.org?subject=help>
List-Subscribe: <https://lists.gnu.org/mailman/listinfo/bug-diffutils>,
	<mailto:bug-diffutils-request@gnu.org?subject=subscribe>
X-List-Received-Date: Mon, 13 Aug 2012 22:30:55 -0000

Here is an updated version with a NEWS entry and a test case.

Note that the test case might not survive sending over email as it
contains a special character.  Maybe the test case can be turned into
plain text somehow and stay reasonably portable.

Andreas

--

* src/util.c (c_escape_char): New function.
(c_escape): New function.
(begin_output): Escape file names when needed.
* src/context.c (print_context_header): New names parameter.
(print_context_label): New name parameter.
* src/diff.h (print_context_header): Change prototype.
* tests/filename-quoting: New file.
* NEWS: Document this change.
---
 NEWS                   |    5 ++
 src/context.c          |   13 +++---
 src/diff.h             |    2 +-
 src/util.c             |  100 +++++++++++++++++++++++++++++++++++++++++++++--
 tests/Makefile.am      |    3 +-
 tests/filename-quoting |   58 ++++++++++++++++++++++++++++
 6 files changed, 168 insertions(+), 13 deletions(-)
 create mode 100755 tests/filename-quoting

diff --git a/NEWS b/NEWS
index 567fa3d..c04831a 100644
--- a/NEWS
+++ b/NEWS
@@ -7,6 +7,11 @@ GNU diffutils NEWS                                    -*- outline -*-
   --new-file (-N) and --unidirectional-new-file now allow comparisons to "-".
   A standard input that's closed acts like a nonexistent file.
 
+  File names which contain spaces, double quotes, backslashes or control
+  characters are now encoded as double quoted C string literals in the diff
+  output.  The escape sequences \\, \", \a, \b, \f, \n, \r, \t, \v, and \ooo (a
+  three-digit octal number between 0 and 255) are used.
+
 ** Packaging
 
   diffutils is now designed to build with Cygwin or MinGW rather than DJGPP.
diff --git a/src/context.c b/src/context.c
index b73d5c3..53aa203 100644
--- a/src/context.c
+++ b/src/context.c
@@ -40,6 +40,7 @@ static lin find_function_last_match;
 static void
 print_context_label (char const *mark,
 		     struct file_data *inf,
+		     char const *name,
 		     char const *label)
 {
   if (label)
@@ -70,24 +71,24 @@ print_context_label (char const *mark,
 	      sprintf (buf, "%"PRIuMAX".%.9d", sec, nsec);
 	    }
 	}
-      fprintf (outfile, "%s %s\t%s\n", mark, inf->name, buf);
+      fprintf (outfile, "%s %s\t%s\n", mark, name, buf);
     }
 }
 
 /* Print a header for a context diff, with the file names and dates.  */
 
 void
-print_context_header (struct file_data inf[], bool unidiff)
+print_context_header (struct file_data inf[], char const *const *names, bool unidiff)
 {
   if (unidiff)
     {
-      print_context_label ("---", &inf[0], file_label[0]);
-      print_context_label ("+++", &inf[1], file_label[1]);
+      print_context_label ("---", &inf[0], names[0], file_label[0]);
+      print_context_label ("+++", &inf[1], names[1], file_label[1]);
     }
   else
     {
-      print_context_label ("***", &inf[0], file_label[0]);
-      print_context_label ("---", &inf[1], file_label[1]);
+      print_context_label ("***", &inf[0], names[0], file_label[0]);
+      print_context_label ("---", &inf[1], names[1], file_label[1]);
     }
 }
 
diff --git a/src/diff.h b/src/diff.h
index 4b6595c..212a8d0 100644
--- a/src/diff.h
+++ b/src/diff.h
@@ -332,7 +332,7 @@ XTERN FILE *outfile;
 extern int diff_2_files (struct comparison *);
 
 /* context.c */
-extern void print_context_header (struct file_data[], bool);
+extern void print_context_header (struct file_data[], char const * const *, bool);
 extern void print_context_script (struct change *, bool);
 
 /* dir.c */
diff --git a/src/util.c b/src/util.c
index 38b33ef..ca6ef2a 100644
--- a/src/util.c
+++ b/src/util.c
@@ -166,24 +166,109 @@ setup_output (char const *name0, char const *name1, bool recursive)
 static pid_t pr_pid;
 #endif
 
+static char c_escape_char (char c)
+{
+  switch (c) {
+    case '\a': return 'a';
+    case '\b': return 'b';
+    case '\t': return 't';
+    case '\n': return 'n';
+    case '\v': return 'v';
+    case '\f': return 'f';
+    case '\r': return 'r';
+    case '"': return '"';
+    case '\\': return '\\';
+    default:
+      return c < 32;
+  }
+}
+
+char *
+c_escape (char const *str)
+{
+  char const *s;
+  unsigned int plus = 0;
+  bool must_quote = false;
+
+  for (s = str; *s; s++)
+    {
+      char c = *s;
+
+      if (c == ' ')
+	{
+	  must_quote = true;
+	  continue;
+	}
+      switch (c_escape_char (*s))
+	{
+	  case 1:
+	    plus += 3;
+	    /* fall through */
+	  case 0:
+	    break;
+	  default:
+	    plus++;
+	    break;
+	}
+    }
+
+  if (must_quote || plus)
+    {
+      char *buffer, *b;
+
+      b = buffer = xmalloc (strlen (str) + plus + 3);
+      *b++ = '"';
+      for (s = str; *s; s++)
+	{
+	  char c = *s;
+	  char escape = c_escape_char (c);
+
+	  switch (escape)
+	    {
+	      case 0:
+		*b++ = c;
+		break;
+	      case 1:
+		*b++ = '\\';
+		*b++ = ((c >> 6) & 03) + '0';
+		*b++ = ((c >> 3) & 07) + '0';
+		*b++ = ((c >> 0) & 07) + '0';
+		break;
+	      default:
+		*b++ = '\\';
+		*b++ = escape;
+		break;
+	    }
+	}
+      *b++ = '"';
+      *b = 0;
+      return buffer;
+    }
+  else
+    return (char *) str;
+}
+
 void
 begin_output (void)
 {
+  char *names[2];
   char *name;
 
   if (outfile != 0)
     return;
 
+  names[0] = c_escape (current_name0);
+  names[1] = c_escape (current_name1);
+
   /* Construct the header of this piece of diff.  */
-  name = xmalloc (strlen (current_name0) + strlen (current_name1)
-		  + strlen (switch_string) + 7);
+  name = xmalloc (strlen (names[0]) + strlen (names[1]) + strlen (switch_string) + 7);
 
   /* POSIX 1003.1-2001 specifies this format.  But there are some bugs in
      the standard: it says that we must print only the last component
      of the pathnames, and it requires two spaces after "diff" if
      there are no options.  These requirements are silly and do not
      match historical practice.  */
-  sprintf (name, "diff%s %s %s", switch_string, current_name0, current_name1);
+  sprintf (name, "diff%s %s %s", switch_string, names[0], names[1]);
 
   if (paginate)
     {
@@ -258,16 +343,21 @@ begin_output (void)
   switch (output_style)
     {
     case OUTPUT_CONTEXT:
-      print_context_header (files, false);
+      print_context_header (files, (char const *const *)names, false);
       break;
 
     case OUTPUT_UNIFIED:
-      print_context_header (files, true);
+      print_context_header (files, (char const *const *)names, true);
       break;
 
     default:
       break;
     }
+
+  if (names[0] != current_name0)
+    free (names[0]);
+  if (names[1] != current_name1)
+    free (names[1]);
 }
 
 /* Call after the end of output of diffs for one file.
diff --git a/tests/Makefile.am b/tests/Makefile.am
index afc9aad..c9b59e5 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -11,7 +11,8 @@ TESTS = \
   new-file \
   no-dereference \
   no-newline-at-eof \
-  stdin
+  stdin \
+  filename-quoting
 
 EXTRA_DIST = \
   $(TESTS) init.sh t-local.sh
diff --git a/tests/filename-quoting b/tests/filename-quoting
new file mode 100755
index 0000000..d06ff60
--- /dev/null
+++ b/tests/filename-quoting
@@ -0,0 +1,58 @@
+#!/bin/sh
+# filename quoting
+
+. "${srcdir=.}/init.sh"; path_prepend_ ../src
+
+fail=0
+
+cat <<EOF > exp- || fail=1
+diff -N -r "a/ " "b/ "
+0a1
+> space
+EOF
+
+cat <<EOF > exp--u || fail=1
+diff -N -r -u "a/ " "b/ "
+--- "a/ "
++++ "b/ "
+@@ -0,0 +1 @@
++space
+EOF
+
+cat <<EOF > exp--c || fail=1
+diff -N -r -c "a/ " "b/ "
+*** "a/ "
+--- "b/ "
+***************
+*** 0 ****
+--- 1 ----
++ space
+EOF
+
+mkdir a b
+echo space > "b/ " || fail=1
+for opt in '' -u -c; do
+  diff -N -r $opt a b > out 2> err; test $? = 1 || fail=1
+  # Remove date and time.
+  sed -e 's/^\([-+*][-+*][-+*] [^	]*\)	.*/\1/' out > k; mv k out
+  compare exp-$(echo $opt|tr ' ' _) out || fail=1
+done
+
+rm -f "b/ "
+
+cat <<EOF > exp || fail=1
+--- "a/\t"
++++ "b/\001"
+@@ -1 +1 @@
+-tab
++one
+EOF
+
+echo tab > "a/	" || fail=1
+echo one > "b/" || fail=1
+diff -u "a/	" "b/" > out 2> err; test $? = 1 || fail=1
+# Remove date and time.
+sed -e 's/^\([-+*][-+*][-+*] [^	]*\)	.*/\1/' out > k; mv k out
+compare exp out || fail=1
+
+Exit $fail
-- 
1.7.7.6



From MAILER-DAEMON Tue Aug 21 22:15:50 2012
Received: from list by lists.gnu.org with archive (Exim 4.71)
	id 1T40U2-0007xa-Fh
	for mharc-bug-diffutils@gnu.org; Tue, 21 Aug 2012 22:15:50 -0400
Received: from eggs.gnu.org ([208.118.235.92]:52175)
	by lists.gnu.org with esmtp (Exim 4.71)
	(envelope-from <debian@tim.thechases.com>) id 1T3svw-0004UI-KB
	for bug-diffutils@gnu.org; Tue, 21 Aug 2012 14:12:09 -0400
Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71)
	(envelope-from <debian@tim.thechases.com>) id 1T3svq-00064C-U7
	for bug-diffutils@gnu.org; Tue, 21 Aug 2012 14:12:08 -0400
Received: from boston.accountservergroup.com ([50.22.11.22]:42472)
	by eggs.gnu.org with esmtp (Exim 4.71)
	(envelope-from <debian@tim.thechases.com>) id 1T3svq-00063x-P0
	for bug-diffutils@gnu.org; Tue, 21 Aug 2012 14:12:02 -0400
Received: from ppp-70-251-69-251.dsl.rcsntx.swbell.net ([70.251.69.251]
	helo=[192.168.1.12])
	by boston.accountservergroup.com with esmtpsa (TLSv1:AES256-SHA:256)
	(Exim 4.69) (envelope-from <debian@tim.thechases.com>)
	id 1T3svo-0006KK-TA
	for bug-diffutils@gnu.org; Tue, 21 Aug 2012 13:12:00 -0500
Message-ID: <5033CFBC.7090103@tim.thechases.com>
Date: Tue, 21 Aug 2012 13:13:16 -0500
From: Tim Chase <debian@tim.thechases.com>
User-Agent: Mozilla/5.0 (X11; U; Linux x86_64; en-US;
	rv:1.9.2.24) Gecko/20111120 Icedove/3.1.16
MIME-Version: 1.0
To: bug-diffutils@gnu.org
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: 7bit
X-AntiAbuse: This header was added to track abuse,
	please include it with any abuse report
X-AntiAbuse: Primary Hostname - boston.accountservergroup.com
X-AntiAbuse: Original Domain - gnu.org
X-AntiAbuse: Originator/Caller UID/GID - [47 12] / [47 12]
X-AntiAbuse: Sender Address Domain - tim.thechases.com
X-Source: 
X-Source-Args: 
X-Source-Dir: 
X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.6 (newer, 3)
X-Received-From: 50.22.11.22
X-Mailman-Approved-At: Tue, 21 Aug 2012 22:15:48 -0400
Subject: [bug-diffutils] diff -p finds wrong function name
X-BeenThere: bug-diffutils@gnu.org
X-Mailman-Version: 2.1.14
Precedence: list
List-Id: "All diffutils discussion." <bug-diffutils.gnu.org>
List-Unsubscribe: <https://lists.gnu.org/mailman/options/bug-diffutils>,
	<mailto:bug-diffutils-request@gnu.org?subject=unsubscribe>
List-Archive: <http://lists.gnu.org/archive/html/bug-diffutils>
List-Post: <mailto:bug-diffutils@gnu.org>
List-Help: <mailto:bug-diffutils-request@gnu.org?subject=help>
List-Subscribe: <https://lists.gnu.org/mailman/listinfo/bug-diffutils>,
	<mailto:bug-diffutils-request@gnu.org?subject=subscribe>
X-List-Received-Date: Tue, 21 Aug 2012 18:12:09 -0000

According to the man/info pages for diff, "-p" should "Show which C
function each change is in."  However, if the function definition is
within the output context, it searches for the *previous* function.
 Reproducible with the following:

===============================
$ cat > one.c <<EOF
int call_me(int maybe)
{
  return maybe;
}

int main()
{
  return 0;
}
EOF
$ sed s/0/1/ one.c > two.c
$ diff -pu one.c two.c
--- one.c       2012-08-21 12:56:14.446205916 -0500
+++ two.c       2012-08-21 13:06:16.454205832 -0500
@@ -5,5 +5,5 @@ int call_me(int maybe)

 int main()
 {
-  return 0;
+  return 1;
 }
$ # how does adding "-p" change the output?
$ diff <(diff -u one.c two) <(diff -pu one.c two.c)
3c3
< @@ -5,5 +5,4 @@
---
> @@ -5,5 +5,4 @@ int call_me(int maybe)
$ diff --version | head -1 # in Debian Stable
diff (GNU diffutils) 3.0
===============================

The edit was made in main(), but diff finds/shows call_me() as the
containing function name.  FWIW, the same happens with context
rather than unified diffs.

-tkc







From MAILER-DAEMON Tue Aug 21 22:15:55 2012
Received: from list by lists.gnu.org with archive (Exim 4.71)
	id 1T40U7-0007xw-Jq
	for mharc-bug-diffutils@gnu.org; Tue, 21 Aug 2012 22:15:55 -0400
Received: from eggs.gnu.org ([208.118.235.92]:33848)
	by lists.gnu.org with esmtp (Exim 4.71)
	(envelope-from <debian@tim.thechases.com>) id 1T3uY9-0001Hb-Eu
	for bug-diffutils@gnu.org; Tue, 21 Aug 2012 15:55:47 -0400
Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71)
	(envelope-from <debian@tim.thechases.com>) id 1T3uY3-0006Pb-Nt
	for bug-diffutils@gnu.org; Tue, 21 Aug 2012 15:55:41 -0400
Received: from boston.accountservergroup.com ([50.22.11.22]:53880)
	by eggs.gnu.org with esmtp (Exim 4.71)
	(envelope-from <debian@tim.thechases.com>) id 1T3uY3-0006PT-Iv
	for bug-diffutils@gnu.org; Tue, 21 Aug 2012 15:55:35 -0400
Received: from ppp-70-251-69-251.dsl.rcsntx.swbell.net ([70.251.69.251]
	helo=[192.168.1.12])
	by boston.accountservergroup.com with esmtpsa (TLSv1:AES256-SHA:256)
	(Exim 4.69) (envelope-from <debian@tim.thechases.com>)
	id 1T3uY2-000El9-CO
	for bug-diffutils@gnu.org; Tue, 21 Aug 2012 14:55:34 -0500
Message-ID: <5033E801.2020502@tim.thechases.com>
Date: Tue, 21 Aug 2012 14:56:49 -0500
From: Tim Chase <debian@tim.thechases.com>
User-Agent: Mozilla/5.0 (X11; U; Linux x86_64; en-US;
	rv:1.9.2.24) Gecko/20111120 Icedove/3.1.16
MIME-Version: 1.0
To: bug-diffutils@gnu.org
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: 7bit
X-AntiAbuse: This header was added to track abuse,
	please include it with any abuse report
X-AntiAbuse: Primary Hostname - boston.accountservergroup.com
X-AntiAbuse: Original Domain - gnu.org
X-AntiAbuse: Originator/Caller UID/GID - [47 12] / [47 12]
X-AntiAbuse: Sender Address Domain - tim.thechases.com
X-Source: 
X-Source-Args: 
X-Source-Dir: 
X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.6 (newer, 3)
X-Received-From: 50.22.11.22
X-Mailman-Approved-At: Tue, 21 Aug 2012 22:15:54 -0400
Subject: [bug-diffutils] diff -p finds wrong function name
X-BeenThere: bug-diffutils@gnu.org
X-Mailman-Version: 2.1.14
Precedence: list
List-Id: "All diffutils discussion." <bug-diffutils.gnu.org>
List-Unsubscribe: <https://lists.gnu.org/mailman/options/bug-diffutils>,
	<mailto:bug-diffutils-request@gnu.org?subject=unsubscribe>
List-Archive: <http://lists.gnu.org/archive/html/bug-diffutils>
List-Post: <mailto:bug-diffutils@gnu.org>
List-Help: <mailto:bug-diffutils-request@gnu.org?subject=help>
List-Subscribe: <https://lists.gnu.org/mailman/listinfo/bug-diffutils>,
	<mailto:bug-diffutils-request@gnu.org?subject=subscribe>
X-List-Received-Date: Tue, 21 Aug 2012 19:55:47 -0000

[reposting as the first didn't seem to appear in the archives after
a couple hours]

According to the man/info pages for diff, "-p" should "Show which C
function each change is in."  However, if the function definition is
within the output context, it searches for the *previous* function.
Reproducible with the following:

===============================
$ cat > one.c <<EOF
int call_me(int maybe)
{
  return maybe;
}

int main()
{
  return 0;
}
EOF
$ sed s/0/1/ one.c > two.c
$ diff -pu one.c two.c
--- one.c       2012-08-21 12:56:14.446205916 -0500
+++ two.c       2012-08-21 13:06:16.454205832 -0500
@@ -5,5 +5,5 @@ int call_me(int maybe)

 int main()
 {
-  return 0;
+  return 1;
 }
$ # how does adding "-p" change the output?
$ diff <(diff -u one.c two) <(diff -pu one.c two.c)
3c3
< @@ -5,5 +5,4 @@
---
> @@ -5,5 +5,4 @@ int call_me(int maybe)
$ diff --version | head -1 # in Debian Stable
diff (GNU diffutils) 3.0
===============================

The edit was made in main(), but diff finds/shows call_me() as the
containing function name.  FWIW, the same happens with context
rather than unified diffs.

-tkc







From MAILER-DAEMON Tue Aug 28 05:08:10 2012
Received: from list by lists.gnu.org with archive (Exim 4.71)
	id 1T6HmM-00028q-IY
	for mharc-bug-diffutils@gnu.org; Tue, 28 Aug 2012 05:08:10 -0400
Received: from eggs.gnu.org ([208.118.235.92]:52144)
	by lists.gnu.org with esmtp (Exim 4.71)
	(envelope-from <jim@meyering.net>) id 1T6HmE-00022k-SJ
	for bug-diffutils@gnu.org; Tue, 28 Aug 2012 05:08:08 -0400
Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71)
	(envelope-from <jim@meyering.net>) id 1T6HmD-0004xd-RJ
	for bug-diffutils@gnu.org; Tue, 28 Aug 2012 05:08:02 -0400
Received: from mx.meyering.net ([88.168.87.75]:54179)
	by eggs.gnu.org with esmtp (Exim 4.71)
	(envelope-from <jim@meyering.net>) id 1T6HmD-0004xT-Kh
	for bug-diffutils@gnu.org; Tue, 28 Aug 2012 05:08:01 -0400
Received: from rho.meyering.net (rho.meyering.net [127.0.0.1])
	by rho.meyering.net (Acme Bit-Twister) with ESMTP id 04E9C604E2
	for <bug-diffutils@gnu.org>; Tue, 28 Aug 2012 11:08:00 +0200 (CEST)
From: Jim Meyering <jim@meyering.net>
To: bug-diffutils@gnu.org
Date: Tue, 28 Aug 2012 11:08:00 +0200
Message-ID: <87txvne64v.fsf@rho.meyering.net>
Lines: 38
MIME-Version: 1.0
Content-Type: text/plain
X-detected-operating-system: by eggs.gnu.org: Genre and OS details not
	recognized.
X-Received-From: 88.168.87.75
Subject: [bug-diffutils] [PATCH] diff: avoid possible longjmp-triggered
	misbehavior
X-BeenThere: bug-diffutils@gnu.org
X-Mailman-Version: 2.1.14
Precedence: list
List-Id: "All diffutils discussion." <bug-diffutils.gnu.org>
List-Unsubscribe: <https://lists.gnu.org/mailman/options/bug-diffutils>,
	<mailto:bug-diffutils-request@gnu.org?subject=unsubscribe>
List-Archive: <http://lists.gnu.org/archive/html/bug-diffutils>
List-Post: <mailto:bug-diffutils@gnu.org>
List-Help: <mailto:bug-diffutils-request@gnu.org?subject=help>
List-Subscribe: <https://lists.gnu.org/mailman/listinfo/bug-diffutils>,
	<mailto:bug-diffutils-request@gnu.org?subject=subscribe>
X-List-Received-Date: Tue, 28 Aug 2012 09:08:08 -0000

a very recent gcc 4.8.0 20120825 reported this problem:

    dir.c: In function 'find_dir_file_pathname':
    dir.c:324:54: error: argument 'file' might be clobbered by 'longjmp' or 'vfork' [-Werror=clobbered]
     find_dir_file_pathname (char const *dir, char const *file)

It seems legit, but perhaps it should be complaining about "match"
rather than "file".

Here's the fix:

>From 6926b608b1a73d35c3caac1253eb985d64ebe545 Mon Sep 17 00:00:00 2001
From: Jim Meyering <meyering@redhat.com>
Date: Tue, 28 Aug 2012 09:39:53 +0200
Subject: [PATCH] diff: avoid possible longjmp-triggered misbehavior

* src/dir.c (find_dir_file_pathname): gcc 4.8.0 20120825 reported
that a local variable's value might be clobbered.  Declare "match"
to point to volatile storage.
---
 src/dir.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/src/dir.c b/src/dir.c
index 57134c7..a2f6bba 100644
--- a/src/dir.c
+++ b/src/dir.c
@@ -324,7 +324,7 @@ char *
 find_dir_file_pathname (char const *dir, char const *file)
 {
   char *val;
-  char const *match = file;
+  char const *volatile match = file;
   struct dirdata dirdata;
   dirdata.names = NULL;
   dirdata.data = NULL;
--
1.7.12.116.g31e0100


From MAILER-DAEMON Tue Aug 28 05:13:27 2012
Received: from list by lists.gnu.org with archive (Exim 4.71)
	id 1T6HrS-0002cO-VU
	for mharc-bug-diffutils@gnu.org; Tue, 28 Aug 2012 05:13:27 -0400
Received: from eggs.gnu.org ([208.118.235.92]:47158)
	by lists.gnu.org with esmtp (Exim 4.71)
	(envelope-from <jim@meyering.net>) id 1T6HrL-0002av-GP
	for bug-diffutils@gnu.org; Tue, 28 Aug 2012 05:13:23 -0400
Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71)
	(envelope-from <jim@meyering.net>) id 1T6HrB-0006NB-M4
	for bug-diffutils@gnu.org; Tue, 28 Aug 2012 05:13:19 -0400
Received: from mx.meyering.net ([88.168.87.75]:54191)
	by eggs.gnu.org with esmtp (Exim 4.71)
	(envelope-from <jim@meyering.net>) id 1T6HrB-0006N6-BB
	for bug-diffutils@gnu.org; Tue, 28 Aug 2012 05:13:09 -0400
Received: from rho.meyering.net (rho.meyering.net [127.0.0.1])
	by rho.meyering.net (Acme Bit-Twister) with ESMTP id 842B86009A
	for <bug-diffutils@gnu.org>; Tue, 28 Aug 2012 11:13:08 +0200 (CEST)
From: Jim Meyering <jim@meyering.net>
To: bug-diffutils@gnu.org
Date: Tue, 28 Aug 2012 11:13:08 +0200
Message-ID: <87oblve5wb.fsf@rho.meyering.net>
Lines: 97
MIME-Version: 1.0
Content-Type: text/plain
X-detected-operating-system: by eggs.gnu.org: Genre and OS details not
	recognized.
X-Received-From: 88.168.87.75
Subject: [bug-diffutils] [PATCH 1/3] maint: avoid new syntax-check failure
	due to @xref use
X-BeenThere: bug-diffutils@gnu.org
X-Mailman-Version: 2.1.14
Precedence: list
List-Id: "All diffutils discussion." <bug-diffutils.gnu.org>
List-Unsubscribe: <https://lists.gnu.org/mailman/options/bug-diffutils>,
	<mailto:bug-diffutils-request@gnu.org?subject=unsubscribe>
List-Archive: <http://lists.gnu.org/archive/html/bug-diffutils>
List-Post: <mailto:bug-diffutils@gnu.org>
List-Help: <mailto:bug-diffutils-request@gnu.org?subject=help>
List-Subscribe: <https://lists.gnu.org/mailman/listinfo/bug-diffutils>,
	<mailto:bug-diffutils-request@gnu.org?subject=subscribe>
X-List-Received-Date: Tue, 28 Aug 2012 09:13:24 -0000

I've already posted 3/3 so have omitted it.
Updating to latest gnulib would have induced a new syntax-check failure,
so I've put that fix first.  Also, I've elided the boring bootstrap-
and init.sh-syncing diffs below:

>From 5cb0eea638e5db4de66bcf99d9ce4f6fd974e04e Mon Sep 17 00:00:00 2001
From: Jim Meyering <meyering@redhat.com>
Date: Tue, 28 Aug 2012 10:58:48 +0200
Subject: [PATCH 1/3] maint: avoid new syntax-check failure due to @xref use

* doc/diffutils.texi: Change several "; @xref{..." to ".  @xref{...",
since @xref should start a sentence.
---
 doc/diffutils.texi | 12 ++++++------
 1 file changed, 6 insertions(+), 6 deletions(-)

diff --git a/doc/diffutils.texi b/doc/diffutils.texi
index d4a68f8..2908120 100644
--- a/doc/diffutils.texi
+++ b/doc/diffutils.texi
@@ -821,7 +821,7 @@ The fractional seconds are omitted on hosts that do not support
 fractional time stamps.

 You can change the header's content with the
-@option{--label=@var{label}} option; see @xref{Alternate Names}.
+@option{--label=@var{label}} option. @xref{Alternate Names}.

 Next come one or more hunks of differences; each hunk shows one area
 where the files differ.  Unified format hunks look like this:
@@ -993,7 +993,7 @@ The corresponding lines differ, and only the second line is incomplete.
 @end table

 Normally, an output line is incomplete if and only if the lines that it
-contains are incomplete; @xref{Incomplete Lines}.  However, when an
+contains are incomplete.  @xref{Incomplete Lines}.  However, when an
 output line represents two differing lines, one might be incomplete
 while the other is not.  In this case, the output line is complete,
 but its the gutter is marked @samp{\} if the first line is incomplete,
@@ -1708,7 +1708,7 @@ might contain duplicate or otherwise incorrect code.

 The @command{patch} @option{-D @var{name}} option behaves like
 the @command{diff} @option{-D @var{name}} option, except it operates on
-a file and a diff to produce a merged file; @xref{patch Options}.
+a file and a diff to produce a merged file.  @xref{patch Options}.

 @node Incomplete Lines
 @chapter Incomplete Lines
@@ -1727,10 +1727,10 @@ line by a following line that starts with @samp{\}.  However, the
 @acronym{RCS} format (@pxref{RCS}) outputs the incomplete line as-is,
 without any trailing newline or following line.  The side by side
 format normally represents incomplete lines as-is, but in some cases
-uses a @samp{\} or @samp{/} gutter marker; @xref{Side by Side}.  The
+uses a @samp{\} or @samp{/} gutter marker.  @xref{Side by Side}.  The
 if-then-else line format preserves a line's incompleteness with
-@samp{%L}, and discards the newline with @samp{%l}; @xref{Line
-Formats}.  Finally, with the @command{ed} and forward @command{ed}
+@samp{%L}, and discards the newline with @samp{%l}.  @xref{Line Formats}.
+Finally, with the @command{ed} and forward @command{ed}
 output formats (@pxref{Output Formats}) @command{diff} cannot
 represent an incomplete line, so it pretends there was a newline and
 reports an error.
--
1.7.12.116.g31e0100


>From 6c273a4db62c4f7b0667545c31a5ad24b913598a Mon Sep 17 00:00:00 2001
From: Jim Meyering <meyering@redhat.com>
Date: Tue, 28 Aug 2012 08:47:27 +0200
Subject: [PATCH 2/3] build: update gnulib, bootstrap and init.sh to latest

---
 bootstrap     | 118 ++++++++++++++++++++++++++++++++++------------------------
 gnulib        |   2 +-
 tests/init.sh |  18 +++++++--
 3 files changed, 84 insertions(+), 54 deletions(-)

diff --git a/bootstrap b/bootstrap
index ce37a2c..e3e270b 100755
--- a/bootstrap
+++ b/bootstrap
@@ -1,6 +1,6 @@
 #! /bin/sh
 # Print a version string.
-scriptversion=2012-05-15.06; # UTC
+scriptversion=2012-07-19.14; # UTC
...
diff --git a/gnulib b/gnulib
index 258577f..68f693f 160000
--- a/gnulib
+++ b/gnulib
@@ -1 +1 @@
-Subproject commit 258577fccc77760d91f8c28286aaa4838ca9cd90
+Subproject commit 68f693ff1db33bf24695f0f42c62e7801966fd06
diff --git a/tests/init.sh b/tests/init.sh
...
--
1.7.12.116.g31e0100


From MAILER-DAEMON Thu Aug 30 10:56:05 2012
Received: from list by lists.gnu.org with archive (Exim 4.71)
	id 1T76A9-00043T-42
	for mharc-bug-diffutils@gnu.org; Thu, 30 Aug 2012 10:56:05 -0400
Received: from eggs.gnu.org ([208.118.235.92]:48689)
	by lists.gnu.org with esmtp (Exim 4.71)
	(envelope-from <eggert@cs.ucla.edu>) id 1T76A4-00043J-9h
	for bug-diffutils@gnu.org; Thu, 30 Aug 2012 10:56:04 -0400
Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71)
	(envelope-from <eggert@cs.ucla.edu>) id 1T769y-0004YT-5s
	for bug-diffutils@gnu.org; Thu, 30 Aug 2012 10:56:00 -0400
Received: from smtp.cs.ucla.edu ([131.179.128.62]:42983)
	by eggs.gnu.org with esmtp (Exim 4.71)
	(envelope-from <eggert@cs.ucla.edu>) id 1T769x-0004XY-UI
	for bug-diffutils@gnu.org; Thu, 30 Aug 2012 10:55:54 -0400
Received: from localhost (localhost.localdomain [127.0.0.1])
	by smtp.cs.ucla.edu (Postfix) with ESMTP id 5CE8E39E800E;
	Thu, 30 Aug 2012 07:55:52 -0700 (PDT)
X-Virus-Scanned: amavisd-new at smtp.cs.ucla.edu
Received: from smtp.cs.ucla.edu ([127.0.0.1])
	by localhost (smtp.cs.ucla.edu [127.0.0.1]) (amavisd-new, port 10024)
	with ESMTP id hHxAVCp-9Lg5; Thu, 30 Aug 2012 07:55:51 -0700 (PDT)
Received: from [192.168.1.3] (pool-108-23-119-2.lsanca.fios.verizon.net
	[108.23.119.2])
	by smtp.cs.ucla.edu (Postfix) with ESMTPSA id 2494E39E8007;
	Thu, 30 Aug 2012 07:55:51 -0700 (PDT)
Message-ID: <503F7EF7.3020807@cs.ucla.edu>
Date: Thu, 30 Aug 2012 07:55:51 -0700
From: Paul Eggert <eggert@cs.ucla.edu>
Organization: UCLA Computer Science Department
User-Agent: Mozilla/5.0 (X11; Linux i686;
	rv:14.0) Gecko/20120714 Thunderbird/14.0
MIME-Version: 1.0
To: Jim Meyering <jim@meyering.net>
References: <87txvne64v.fsf@rho.meyering.net>
In-Reply-To: <87txvne64v.fsf@rho.meyering.net>
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 7bit
X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.6 (newer, 3)
X-Received-From: 131.179.128.62
Cc: bug-diffutils@gnu.org
Subject: Re: [bug-diffutils] [PATCH] diff: avoid possible longjmp-triggered
 misbehavior
X-BeenThere: bug-diffutils@gnu.org
X-Mailman-Version: 2.1.14
Precedence: list
List-Id: "All diffutils discussion." <bug-diffutils.gnu.org>
List-Unsubscribe: <https://lists.gnu.org/mailman/options/bug-diffutils>,
	<mailto:bug-diffutils-request@gnu.org?subject=unsubscribe>
List-Archive: <http://lists.gnu.org/archive/html/bug-diffutils>
List-Post: <mailto:bug-diffutils@gnu.org>
List-Help: <mailto:bug-diffutils-request@gnu.org?subject=help>
List-Subscribe: <https://lists.gnu.org/mailman/listinfo/bug-diffutils>,
	<mailto:bug-diffutils-request@gnu.org?subject=subscribe>
X-List-Received-Date: Thu, 30 Aug 2012 14:56:04 -0000

On 08/28/2012 02:08 AM, Jim Meyering wrote:
> It seems legit, but perhaps it should be complaining about "match"
> rather than "file".

I don't see why it's legit.  Even if longjmp trashes
the value of 'match', the code right after the longjmp
immediately sets 'match' (there's a comment to the code
to that effect).

This bogus warning, along with the seemingly random variable
name that it mentions, may be the symptom of a deeper
problem in GCC, one that causes GCC to generate incorrect code,
so it's probably worth filing a GCC bug report.

For now I pushed this further patch into diffutils:

>From e1c27502ee286392ef2be93af752a51574930038 Mon Sep 17 00:00:00 2001
From: Paul Eggert <eggert@cs.ucla.edu>
Date: Thu, 30 Aug 2012 07:52:22 -0700
Subject: [PATCH] diff: silence GCC warning instead of slowing down

* src/dir.c (find_dir_file_pathname): Use 'IF_LINT (volatile)' to
silence the gcc warning, rather than using 'volatile', as the
warning appears to be bogus.
---
 src/dir.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)

diff --git a/src/dir.c b/src/dir.c
index a2f6bba..530807e 100644
--- a/src/dir.c
+++ b/src/dir.c
@@ -323,8 +323,13 @@ dir_loop (struct comparison const *cmp, int i)
 char *
 find_dir_file_pathname (char const *dir, char const *file)
 {
+  /* The 'IF_LINT (volatile)' works around what appears to be a bug in
+     gcc 4.8.0 20120825; see
+     <http://lists.gnu.org/archive/html/bug-diffutils/2012-08/msg00007.html>.
+     */
+  char const * IF_LINT (volatile) match = file;
+
   char *val;
-  char const *volatile match = file;
   struct dirdata dirdata;
   dirdata.names = NULL;
   dirdata.data = NULL;
-- 
1.7.11.4




