Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/usr.bin/patch Remove heuristic for dealing with trailing new...



details:   https://anonhg.NetBSD.org/src/rev/cc64d9bf1fd8
branches:  trunk
changeset: 946137:cc64d9bf1fd8
user:      rhialto <rhialto%NetBSD.org@localhost>
date:      Tue Nov 17 20:49:12 2020 +0000

description:
Remove heuristic for dealing with trailing newlines being truncated by mailers.

Patch and explanation taken from bsdimp:
https://bsdimp.blogspot.com/2020/08/a-35-year-old-bug-in-patch-found-in.html
https://svnweb.freebsd.org/base?view=revision&revision=364291

Every version of patch since the first one posted to mod.sources in 1985 have
included a heuristic for coping with the state of email messaging at the
time. This heuristic would add up to 4 blank lines to a patch if it thought it
needed it. The trouble is, though this causes at least one bug.

The bug in my case is that if you have a context diff whose last hunk only
deletes 3 or fewer lines, then if you try to reverse apply it with -R, it will
fail. The reason for this is the heuristic builds an internal representation
that includes those blank lines. However, it should really replicate the lines
from the pattern lines line it would any other time, not assume they are blank
lines. Removing this heuristic will prevent patch from misapplying the lines
removed after applying a 'fuzz' factor to the previous blank line in the file. I
believe this will only affect 'new-style' 4.3BSD context diffs and not the
older-style 4.2BSD diffs and plain, non-context diffs. It won't affect any of
the newer formats, since they don't use the 'omitted' construct in the same way.

Since this heuristic was put into patch at a time when email / etc ate trailing
white space on a regular basis, and since it's clear that this heuristic is the
wrong thing to do at least some of the time, it's better to remove it
entirely. It's not been needed for maybe 20 years since patch files are not
usually corrupted. If there are a small number of patch files that would benefit
from this corruption fixing, those already-currupt patches can be fixed by the
addition of blank lines. I'd wager that no one will ever come to me with an
example of a once-working patch file that breaks with this change. However, I
have 2 patches from the first 195 patches to 2.11BSD that are affected by this
bug, suggesting that the relative frequency of the issue has changed
signficantly since the original heuristic was put into place.

diffstat:

 usr.bin/patch/pch.c  |  17 ++++++-----------
 usr.bin/patch/util.c |   6 +++---
 2 files changed, 9 insertions(+), 14 deletions(-)

diffs (72 lines):

diff -r 789d19e46847 -r cc64d9bf1fd8 usr.bin/patch/pch.c
--- a/usr.bin/patch/pch.c       Tue Nov 17 20:27:35 2020 +0000
+++ b/usr.bin/patch/pch.c       Tue Nov 17 20:49:12 2020 +0000
@@ -1,7 +1,7 @@
 /*
  * $OpenBSD: pch.c,v 1.37 2007/09/02 15:19:33 deraadt Exp $
  * $DragonFly: src/usr.bin/patch/pch.c,v 1.6 2008/08/10 23:35:40 joerg Exp $
- * $NetBSD: pch.c,v 1.30 2018/06/18 18:33:31 christos Exp $
+ * $NetBSD: pch.c,v 1.31 2020/11/17 20:49:12 rhialto Exp $
  */
 
 /*
@@ -31,7 +31,7 @@
  */
 
 #include <sys/cdefs.h>
-__RCSID("$NetBSD: pch.c,v 1.30 2018/06/18 18:33:31 christos Exp $");
+__RCSID("$NetBSD: pch.c,v 1.31 2020/11/17 20:49:12 rhialto Exp $");
 
 #include <sys/types.h>
 #include <sys/stat.h>
@@ -556,16 +556,11 @@
                        ret = pgets(buf, buf_len, pfp);
                        p_input_line++;
                        if (ret == NULL) {
-                               if (p_max - p_end < 4) {
-                                       /* assume blank lines got chopped */
-                                       strlcpy(buf, "  \n", buf_len);
-                               } else {
-                                       if (repl_beginning && repl_could_be_missing) {
-                                               repl_missing = true;
-                                               goto hunk_done;
-                                       }
-                                       fatal("unexpected end of file in patch\n");
+                               if (repl_beginning && repl_could_be_missing) {
+                                       repl_missing = true;
+                                       goto hunk_done;
                                }
+                               fatal("unexpected end of file in patch\n");
                        }
                        p_end++;
                        if (p_end >= hunkmax)
diff -r 789d19e46847 -r cc64d9bf1fd8 usr.bin/patch/util.c
--- a/usr.bin/patch/util.c      Tue Nov 17 20:27:35 2020 +0000
+++ b/usr.bin/patch/util.c      Tue Nov 17 20:49:12 2020 +0000
@@ -1,7 +1,7 @@
 /*
  * $OpenBSD: util.c,v 1.32 2006/03/11 19:41:30 otto Exp $
  * $DragonFly: src/usr.bin/patch/util.c,v 1.9 2007/09/29 23:11:10 swildner Exp $
- * $NetBSD: util.c,v 1.28 2018/06/18 18:33:31 christos Exp $
+ * $NetBSD: util.c,v 1.29 2020/11/17 20:49:12 rhialto Exp $
  */
 
 /*
@@ -31,7 +31,7 @@
  */
 
 #include <sys/cdefs.h>
-__RCSID("$NetBSD: util.c,v 1.28 2018/06/18 18:33:31 christos Exp $");
+__RCSID("$NetBSD: util.c,v 1.29 2020/11/17 20:49:12 rhialto Exp $");
 
 #include <sys/param.h>
 #include <sys/stat.h>
@@ -417,7 +417,7 @@
 void
 version(void)
 {
-       printf("Patch version 2.0-12u8-NetBSD\n");
+       printf("Patch version 2.0-12u9-NetBSD\n");
        my_exit(EXIT_SUCCESS);
 }
 



Home | Main Index | Thread Index | Old Index