History log of /freebsd-current/usr.bin/patch/pch.c
Revision Date Author Comments
# 25696725 19-Apr-2024 Martin Tournoij <martin@arp242.net>

patch: use getline() instead of fgetln()

This replaces fgetln() with getline(). The main reason for this is
portability, making things easier for people who want to compile these
tools on non-FreeBSD systems.

I appreciate that's probably not the top concern for FreeBSD base tools,
but fgetln() is impossible to port to most platforms, as concurrent
access is essentially impossible to implement fully correct without the
line buffer on the FILE struct. Other than this, many generic FreeBSD
tools compile fairly cleanly on Linux with a few small changes.

Most uses of fgetln() pre-date getline() support (added in 2009 with
69099ba2ec8b), and there's been some previous patches (ee3ca711a898
8c98e6b1a7f3 1a2a4fc8ce1b) for other tools.

Obtained from: https://github.com/dcantrell/bsdutils and
https://github.com/chimera-linux/chimerautils
Signed-off-by: Martin Tournoij <martin@arp242.net>
Reviewed by: imp
Pull Request: https://github.com/freebsd/freebsd-src/pull/893


# 851a9da3 12-Feb-2024 Dag-Erling Smørgrav <des@FreeBSD.org>

patch: Support long context lines.

MFC after: 1 week
Sponsored by: Klara, Inc.
Reviewed by: allanjude
Differential Revision: https://reviews.freebsd.org/D43850


# 42b38843 16-Aug-2023 Warner Losh <imp@FreeBSD.org>

Remove $FreeBSD$: one-line .h pattern

Remove /^\s*\*+\s*\$FreeBSD\$.*$\n/


# 9610cbc0 06-Aug-2023 Pedro F. Giffuni <pfg@FreeBSD.org>

patch: don't run off the end of path if it ends in '/'.

Found by fuzzing (afl) in OpenBSD.

Obtained from: OpenBSD (CVS 1.65)


# c384a278 22-Jul-2021 Pedro F. Giffuni <pfg@FreeBSD.org>

patch: cleanup variable initialization a bit.

musl libc fgetln is a bit more pickier.

Hinted by: chimera-linux (git 31491e1de2e1241885984cd9e4b978965f14eda4)


# c7cddf95 16-Aug-2020 Warner Losh <imp@FreeBSD.org>

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

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.

Reviewed by: phk@
Differential Revision: https://reviews.freebsd.org/D26081


# 50dacbf6 03-Nov-2019 Kyle Evans <kevans@FreeBSD.org>

patch(1): give /dev/null patches special treatment

We have a bad habit of duplicating contents of files that are sourced from
/dev/null and applied more than once... take the more sane (in most ways)
GNU route and complain if the file exists and offer reversal options.

This still falls short a little bit as selecting "don't reverse, apply
anyway" will still give you duplicated file contents. There's probably other
issues as well, but awareness is the first step to happiness.

MFC after: 2 weeks
Differential Revision: https://reviews.freebsd.org/D21535


# ef30b5a8 28-Feb-2019 Kyle Evans <kevans@FreeBSD.org>

patch(1): Exit successfully if we're fed a 0-length patch

This change is made in the name of GNU patch compatibility. If GNU patch is
fed a zero-length patch, it will exit successfully with no output. This is
used in at least one port to date (comms/wsjtx), and we break on this usage.

It seems unlikely that anyone relies on patch(1) calling their completely
empty patch garbage and failing, and GNU compatibility is a plus if it helps
with porting, so make the switch.

Reported by: db
MFC after: 2 weeks


# 76df519f 21-Dec-2017 Pedro F. Giffuni <pfg@FreeBSD.org>

patch: further cleanup to git-style diffs.

Fix adding and removing files with git-style a/ b/ diffs: only skip
six letters if they actually match "--- a/" and "+++ b/" instead of
laxer checks.

Obtained from: OpenBSD (CVS 1.59)


# 4835edfa 09-Oct-2017 Kyle Evans <kevans@FreeBSD.org>

patch(1): Don't overrun line buffer in some cases

Patches like file.txt attached to PR 190195 with a final line formed
like ">(EOL)" could cause a copy past the end of the current line buffer. In the
case of PR 191641, this caused a duplicate line to be copied into the resulting
file.

Instead of running past the end, treat it as if it were a blank line.

PR: 191641
Reviewed by: cem, emaste, pfg
Approved by: emaste (mentor)
Differential Revision: https://reviews.freebsd.org/D12609


# 85823a60 02-Jul-2017 Pedro F. Giffuni <pfg@FreeBSD.org>

patch(1): add support for git generated diffs.

Sometimes patches coming from other places have extra a/ and b/
directories prepended to filenames.

Obtained from: OpenBSD (CVS rev. 1.57, 1.58)


# 12300d3a 07-Jun-2017 Pedro F. Giffuni <pfg@FreeBSD.org>

patch: if reading fails, do not go into infinite loop asking for a filename.

This can happen if no tty is available.

Obtained from: OpenBSD (CVS rev 1.54)
MFC after: 5 days


# 4f548c19 02-Jan-2017 Pedro F. Giffuni <pfg@FreeBSD.org>

Revert r311106:
patch(1): extend the maximum length of a line from USHRT_MAX to UINT_MAX.

This doesn't really work for 32 bit platforms.

Pointed out by: kib


# ad8469fe 02-Jan-2017 Pedro F. Giffuni <pfg@FreeBSD.org>

patch(1): extend the maximum length of a line from USHRT_MAX to UINT_MAX.

We can handle such "big data" without much trouble.
Try to do a better job at detecting the rejection cause while here.

MFC after: 2 weeks


# 2c4eed47 23-Apr-2016 Pedro F. Giffuni <pfg@FreeBSD.org>

patch(1): avoid signed integer overflow when debugging.

Integer i is used to index p_end of type LINENUM (actually long).

Match the types.

MFC after: 5 days


# 463a577b 20-Oct-2015 Eitan Adler <eadler@FreeBSD.org>

Fix a ton of speelling errors

arc lint is helpful

Reviewed By: allanjude, wblock, #manpages, chris@bsdjunk.com
Differential Revision: https://reviews.freebsd.org/D3337


# e678759c 14-Aug-2015 Xin LI <delphij@FreeBSD.org>

Remove automatic checkout feature.

Obtained from: DragonFly via OpenBSD
Relnotes: yes
MFC: never


# 3fd78bfa 05-Aug-2015 Xin LI <delphij@FreeBSD.org>

Fix shell injection vulnerability in patch(1) via ed(1) by
tightening sanity check of the input. [1]

While I'm there also replace ed(1) with red(1) because we do
not need the unrestricted functionality. [2]

Obtained from: Bitrig [1], DragonFly [2]
Security: CVE-2015-1418 [1]


# df6e4074 20-Apr-2015 Pedro F. Giffuni <pfg@FreeBSD.org>

patch(1): small include changes.

Mostly to match OpenBSD, no functional change.

Obtained from: OpenBSD


# 547e0acb 25-Dec-2014 Pedro F. Giffuni <pfg@FreeBSD.org>

patch: Bring in xstrdup and use it when appropriate.

The function savestr allows NULL return values during Plan A patching so in
case of out of memory conditions, Plan B can step in. In many cases, NULL
value is not properly handled, so use xstrdup here (it's outside Plan A/B
patching, which means that even Plan B relies on successful operations).

Clean up some whitespaces while here

Obtained from: OpenBSD
MFC after: 2 weeks


# d3fc0cb8 08-Dec-2014 Pedro F. Giffuni <pfg@FreeBSD.org>

patch(1): avoid line number overflows

Introduce strtolinenum to properly check line numbers while parsing:
no signs, no spaces, just digits, 0 <= x <= LONG_MAX

Properly validate line ranges supplied in diff file to prevent overflows.
Also fixes an out of boundary memory access because the resulting values
are used as array indices.

PR: 195436
Obtained from: OpenBSD (CVS pch.c rev 1.45, 1,46, common.h rev 1.28)
MFC after: 1 week


# 2b468eba 07-Dec-2014 Pedro F. Giffuni <pfg@FreeBSD.org>

Small space changes

Mostly to keep in sync with OpenBSD and update the TAG.

Obtained from: OpenBSD
MFC after: 5 days


# e91a64de 05-Dec-2014 Pedro F. Giffuni <pfg@FreeBSD.org>

Merge fixes from OpenBSD.

Check fstat return value. Also, use off_t for file size and offsets.
Avoid iterating over end of string.

Obtained from: OpenBSD (CVS rev. 1.41, 1.43)
MFC after: 1 week


# dbfbf4a8 05-Dec-2014 Pedro F. Giffuni <pfg@FreeBSD.org>

Update OpenBSD CVS revision for our r255232.

This is a no-op to make it easier to track changes from OpenBSD

MFC after: 3 days


# c1a08643 14-Jun-2014 Pedro F. Giffuni <pfg@FreeBSD.org>

patch: unsign the line length to avoid overflows.

Patch(1) uses a short int for the line length, which is usually
sufficient for regular diffs, but makes no effort to signal
when there is an overflow.

Change the line length to an unsigned short int to better use
the fact that a length is never negative. The change is loosely
inspired on a related change in DragonFly, but we avoid spending
more memory than necessary.

While here adjust the messages to be clearer on what is happening.

MFC after: 1 week


# c44f94d0 13-Jun-2014 Pedro F. Giffuni <pfg@FreeBSD.org>

patch: cleanup some unnecessary cruft.

- Drop some bogus casts to size_t.
- The new_p_foo variables are not needed after r267426.

Pointed out by: bde
MFC after: 1 week


# 9f00ccd3 12-Jun-2014 Pedro F. Giffuni <pfg@FreeBSD.org>

Avoid zeroing during allocation.

This change reverts a change from OpenBSD which made use of
calloc, and therefore wasted time initializing arrays that
will later be realloc'ed. Consistently use FreeBSD's
reallocf().

While here also merge the changes from OpenBSD's manpage
patch.1 Rev 1.27:
"patch was moved from user portability (UP) to base in issue 7
and is no longer optional"

MFC after: 1 week


# 79d8aaa9 04-Sep-2013 Stefan Eßer <se@FreeBSD.org>

Fix file selection logic for the RCS/SCCS case, as was done for the simple
file case before. Bump version because of the changed behavior, which now
matches the documentation.

Reviewed by: pfg


# 6d85e39b 03-Jul-2013 David E. O'Brien <obrien@FreeBSD.org>

Merge r252513 from src/gnu/usr.bin/patch into src/usr.bin/patch:

Properly handle input lines containing NUL characters such that pgets()
accurately fills the read buffer.

Callers of pgets() still mis-process the buffer contents if the read line
contains NUL characters, but this at least makes pgets() accurate.


# 0571fd57 03-Jul-2013 David E. O'Brien <obrien@FreeBSD.org>

Merge r252512 from src/gnu/usr.bin/patch into src/usr.bin/patch:

Make it so that 'patch < FUBAR' and 'patch -i FUBAR' operate the same.

The former makes a copy of stdin, but was not accurately putting the
content of stdin into a temp file. This lead to the undercounting
the number of lines in hunks containing NUL characters when reading
from stdin. Thus resulting in "unexpected end of file in patch" errors.


# 6b239879 23-May-2013 Stefan Eßer <se@FreeBSD.org>

Fix target selection logic, which did not comply with the man page.

Instead of using the file with the least order of path name components,
shortest filename and finally the shortest basename (with the search
stopping as soon as one of these conditions is true), the first filename
checked was used as the reference, and another filename was only selected
if all of the above comparisons are in favour of the latter file.

This was wrong, because filenames with path less components were only
considered, if both of the other conditions were true as well. In fact,
the first filename to be checked had good chances to be selected in the
end, since it only needed to be better with regard to any one of the
three criteria ...

Reviewed by: delphij@freebsd.org


# e56ef7d3 29-Jan-2013 Xin LI <delphij@FreeBSD.org>

- Refresh code with latest OpenBSD revisions.
- Remove $DragonFly$ tags as they are using git nowadays and VCS tags will
not help merging.
- Other changes to Copyright headers to make them consistent with other
source code, we intend to fork from this point.

Reviewed by: pfg


# 2dd076b8 29-Jan-2013 Gabor Kovesdan <gabor@FreeBSD.org>

- Add a BSD-licensed patch, ported by Pedro F. Giffuni (pfg) from
DragonflyBSD and install it as bsdpatch. WITH_BSD_PATCH makes it
default and installs GNU patch as gnupatch.

Submitted by: pfg
Obtained from: The DragonflyBSD Project


# a9c7144e 01-Aug-2004 Garance A Drosehn <gad@FreeBSD.org>

Import of a BSD-licensed version of `patch', which will eventually
replace the version we currently have in src/gnu/usr.bin/patch/.
Among other things, this version includes a --posix option for strict
POSIX conformance.

This version is the current source from OpenBSD as of today. It is
their 3.5-release, plus a few updates to patch.c and pch.c that they
made about three weeks ago.


# 65b3003d 13-Jul-1997 Peter Wemm <peter@FreeBSD.org>

kill the undead


# c44252b6 06-Jul-1997 Bruce Evans <bde@FreeBSD.org>

Finish importing Lite2's src/usr.bin, except for ex, diff, grep, mail,
pascal and vmstat.sparc. All changed files on the vendor branch should
already have been imported.


# 6065a0be 30-Dec-1995 Peter Wemm <peter@FreeBSD.org>

This commit was generated by cvs2svn to compensate for changes in r13122,
which included commits to RCS files with non-trunk default branches.


# 9b50d902 26-May-1994 Rodney W. Grimes <rgrimes@FreeBSD.org>

BSD 4.4 Lite Usr.bin Sources