Home | History | Annotate | Download | only in patch
History log of /src/usr.bin/patch/pch.c
RevisionDateAuthorComments
 1.34  12-Jul-2024  manu Let patch(1) handle lines of length beyond INT16_MAX

Borrowed from OpenBSD
https://marc.info/?l=openbsd-bugs&m=168907249732754
 1.33  16-Jun-2023  wiz branches: 1.33.2;
Comment out variable assignations that are not used.

Remove trailing whitespace.
 1.32  25-May-2021  cjep As per OpenBSD, use malloc for the line buffer. Fixes the known issue
with long lines and makes our ATF test suite pass fully.
Closes PR bin/54620 from coypu who suggested the approach.
Reviewed by christos.
 1.31  17-Nov-2020  rhialto branches: 1.31.2;
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.
 1.30  18-Jun-2018  christos Keep things portable (requested by joerg) by not depending on reallocarr
and instead doing the overflow check ourselves.
 1.29  05-Apr-2018  christos Pass -S to ed(1) so that patches containing ! commands don't run commands.
Real cause of CVS-2018-0492:
https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=894667)
 1.28  30-Jul-2015  christos branches: 1.28.8; 1.28.14;
from bitrieg:

Substitution commands might contain a newline in the replacement pattern
(escaped with a backslash before it), causing patch's understanding of
the state the ed child process is in to diverge from reality. This can
lead to patch unwillingly feeding '!' (execute shell command) lines to
ed. Finding out how to do this is left as an exercise to the reader.

XXX: pullup-7
 1.27  27-Nov-2014  christos PR/49422: Ryo ONODERA: patch(1) cannot handle context diffs
 1.26  26-Nov-2014  christos Avoid coredumps when the linenumbers in the hunks turn negative.
From: http://marc.info/?l=openbsd-tech&m=141693055412785

XXX: pullup 7
 1.25  29-Jan-2013  wiz branches: 1.25.8;
Add comma after i.e.
From Bug Hunting.
 1.24  06-Sep-2011  joerg branches: 1.24.2; 1.24.8;
Consistently use __dead and __printflike.
 1.23  19-Sep-2008  joerg Update patch to the version used by DragonFly and derived from OpenBSD.
Major changes are:
- better detection of double applied patches
- rejects remain unified diffs for unified patches
- far less limitations, e.g. patch lines may be arbitrary long

This addresses PR standards/11220 by changing patch -b behavior to be
POSIX compliant. Old behavior can be obtained using --suffix, which
works since NetBSD 1.4. pkgsrc has been adjusted accordingly.
 1.22  26-Sep-2006  christos PR/34627: Lubomir Kundrak: patch(1) segfaults, when fed from stdin in batch
or force mode
 1.21  24-May-2006  christos Coverity CID 3510: Don't leak memory.
 1.20  09-Apr-2006  christos branches: 1.20.2;
Coverity CID 2078, 2079, 2080: Plug memory leaks.
 1.19  30-Jul-2003  itojun use bounded string op
 1.18  12-Jul-2003  itojun copyright missing in files, copy them from README
 1.17  01-Jun-2003  kristerw Shorten long lines.
Some whitespace changes to get closer to KNF.
 1.16  30-May-2003  kristerw Simplify handling of memory allocation, and make sure all return values
from malloc etc. are handled.

This removes the old behavior to retry the operation with a less memory-
consuming method in case malloc failed (this mechanism has never really
worked, and is hard to test. Besides, it is less useful now than it was
20 years ago when the code was written...)
 1.15  29-May-2003  kristerw Be consistent with use of types (e.g. do not use LINENUM for quantities
that are not line numbers).
 1.14  20-Apr-2003  christos PR/5123: Ignatios Souvatzis: make patch exit gracefully on malformed input.
 1.13  24-Mar-2002  kristerw Handle patches in which diff(1) has added lines saying
"\ No newline at end of file"
 1.12  16-Mar-2002  kristerw Don't leak memory if realloc fails
Spell "FALLTHROUGH" correctly
 1.11  16-Mar-2002  kristerw Check result of malloc and strdup
Made some functions static
Removed unneccessary buffer
Increased size of some buffers that could overflow
 1.10  15-Mar-2002  kristerw KNF
 1.9  11-Mar-2002  kristerw Ansify
Remove unused defines
Remove #ifndef lint
Remove redundant and incorrect casts.
 1.8  08-Mar-2002  kristerw Ansify
Remove 'register'
Make local functions static.
Remove most '#ifndef lint'
 1.7  09-Feb-1999  sommerfe branches: 1.7.10;
Fix PR2429: catch doubly-applied patches which create new files.
 1.6  06-Nov-1998  christos char -> unsigned char
rindex -> strrchr
 1.5  22-Feb-1998  christos WARNSify
 1.4  29-Jul-1997  phil Set appropiate flag when wanting to skip a patch. Fixes only true
bug reported in PR 2215.
 1.3  19-Sep-1996  thorpej RCS id police.
 1.2  02-Aug-1993  mycroft Add RCS identifiers, remove some completely useless RCS logs and patchkit
headers, and a few other insignificant changes.
 1.1  09-Apr-1993  cgd branches: 1.1.1;
patch 2.0.12u8, from prep.ai.mit.edu. this is not under the GPL.
 1.1.1.1  09-Jan-1997  tls Import from 4.4BSD-Lite2
 1.7.10.1  06-Sep-2002  itojun usr.bin/patch/pch.c revision 1.13 (jon@bullers.net)

has a fix to allow patch to accept the output of diff when the
diff'd files do not end with newlines. (I ran into this when making
the devel/arch package. The result was to require GNU patch. I
suspect other uses of diff and patch may also require this change
for proper operation.)
 1.20.2.1  19-Jun-2006  chap Sync with head.
 1.24.8.1  25-Feb-2013  tls resync with head
 1.24.2.1  22-May-2014  yamt sync with head.

for a reference, the tree before this commit was tagged
as yamt-pagecache-tag8.

this commit was splitted into small chunks to avoid
a limitation of cvs. ("Protocol error: too many arguments")
 1.25.8.3  09-Apr-2018  martin Pull up following revision(s) (requested by christos in ticket #1596):
bin/ed/ed.1: revision 1.32
bin/ed/main.c: revision 1.29
usr.bin/patch/pch.c: revision 1.29
Pass -S to ed(1) so that patches containing ! commands don't run commands.
Real cause of CVS-2018-0492:
https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=894667)
add -S to disable ! commands.
 1.25.8.2  31-Jul-2015  snj branches: 1.25.8.2.2; 1.25.8.2.6;
Pull up following revision(s) (requested by christos in ticket #915):
usr.bin/patch/pch.c: revision 1.28
from bitrieg:
Substitution commands might contain a newline in the replacement pattern
(escaped with a backslash before it), causing patch's understanding of
the state the ed child process is in to diverge from reality. This can
lead to patch unwillingly feeding '!' (execute shell command) lines to
ed. Finding out how to do this is left as an exercise to the reader.
XXX: pullup-7
 1.25.8.1  28-Nov-2014  martin Pull up following revision(s) (requested by christos in ticket #270):
usr.bin/patch/common.h: revision 1.20
usr.bin/patch/pch.c: revision 1.26
usr.bin/patch/pch.c: revision 1.27
Avoid coredumps when the linenumbers in the hunks turn negative.
From OpenBSD.
PR/49422: Ryo ONODERA: patch(1) cannot handle context diffs
 1.25.8.2.6.1  09-Apr-2018  martin Pull up following revision(s) (requested by christos in ticket #1596):
bin/ed/ed.1: revision 1.32
bin/ed/main.c: revision 1.29
usr.bin/patch/pch.c: revision 1.29
Pass -S to ed(1) so that patches containing ! commands don't run commands.
Real cause of CVS-2018-0492:
https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=894667)
add -S to disable ! commands.
 1.25.8.2.2.1  09-Apr-2018  martin Pull up following revision(s) (requested by christos in ticket #1596):
bin/ed/ed.1: revision 1.32
bin/ed/main.c: revision 1.29
usr.bin/patch/pch.c: revision 1.29
Pass -S to ed(1) so that patches containing ! commands don't run commands.
Real cause of CVS-2018-0492:
https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=894667)
add -S to disable ! commands.
 1.28.14.2  25-Jun-2018  pgoyette Sync with HEAD
 1.28.14.1  07-Apr-2018  pgoyette Sync with HEAD. 77 conflicts resolved - all of them $NetBSD$
 1.28.8.1  08-Apr-2018  snj Pull up following revision(s) (requested by christos in ticket #699):
bin/ed/ed.1: 1.32-1.33
bin/ed/main.c: 1.29
usr.bin/patch/pch.c: 1.29
Pass -S to ed(1) so that patches containing ! commands don't run commands.
Real cause of CVE-2018-0492:
https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=894667)
--
add -S to disable ! commands.
--
Fix date.
 1.31.2.1  31-May-2021  cjep sync with head
 1.33.2.1  02-Aug-2025  perseant Sync with HEAD

RSS XML Feed