Commit graph

78 commits

Author SHA1 Message Date
Martin Tournoij 25696725b6 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
69099ba2ec), and there's been some previous patches (ee3ca711a8
8c98e6b1a7 1a2a4fc8ce) 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
2024-04-19 15:52:22 -06:00
Dag-Erling Smørgrav 851a9da38f patch: Support long context lines.
MFC after:	1 week
Sponsored by:	Klara, Inc.
Reviewed by:	allanjude
Differential Revision:	https://reviews.freebsd.org/D43850
2024-02-12 19:26:13 +01:00
Pedro F. Giffuni 7e8afbb6d6 patch: fix locate_hunk in empty files
if `first_guess' is zero then main() assumes that locate_hunk has failed
and aborts the patch operation.  Instead, make sure to return 1 (the
line number) so that the patch operation can continue.

Issue originally found by Neels Hofmeyr in the regress suite of the diff
implementation for got, where the tests assume that applying a diff with
`patch' and then again with `patch -R' yields back the original file.

Obtained from:	OpenBSD (CVS patch.c,v 1.71)
2023-12-03 12:33:03 -05:00
Warner Losh b2c76c41be Remove $FreeBSD$: one-line nroff pattern
Remove /^\.\\"\s*\$FreeBSD\$$\n/
2023-08-16 11:55:15 -06:00
Warner Losh d0b2dbfa0e Remove $FreeBSD$: one-line sh pattern
Remove /^\s*#[#!]?\s*\$FreeBSD\$.*$\n/
2023-08-16 11:55:03 -06:00
Warner Losh 42b388439b Remove $FreeBSD$: one-line .h pattern
Remove /^\s*\*+\s*\$FreeBSD\$.*$\n/
2023-08-16 11:54:23 -06:00
Pedro F. Giffuni 9610cbc09e 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)
2023-08-06 22:27:27 -05:00
Warner Losh 4d846d260e spdx: The BSD-2-Clause-FreeBSD identifier is obsolete, drop -FreeBSD
The SPDX folks have obsoleted the BSD-2-Clause-FreeBSD identifier. Catch
up to that fact and revert to their recommended match of BSD-2-Clause.

Discussed with:		pfg
MFC After:		3 days
Sponsored by:		Netflix
2023-05-12 10:44:03 -06:00
Simon J. Gerraty d9a4274795 Update/fix Makefile.depend for userland 2023-04-18 17:14:23 -07:00
Kyle Evans 7e688ed493 patch: omit filename if the prompt was ignored
When a file is missing, patch(1) will prompt for a filename to try and
patch it.  If we're doing a dry-run, we'll output that the patch to the
source file was either ignored/failed.  If you ignore the prompt in a
dry-run (i.e. just hit enter), we'll output:

X out of X hunks ignored while patching (null)

Let's improve the aesthetics a bit and just omit the last part if the
prompt was ignored:

X out of X hunks ignored

Unfortunately we can't really test this without expect(1) because both
force and batch mode will use the first best guess, which is wiped out
by the "File to patch:" prompt.  We could record the initially derived
bestguess there and use *that*, but given that this is only possible in
an interactive session I think it's fine to just omit the filename
rather than adding a fair amount of complexity (which could also break
other scenarios I haven't considered yet)..

Reviewed by:	des
Sponsored by:	Klara, Inc.
Differential Revision:	https://reviews.freebsd.org/D38179
2023-02-13 00:15:08 -06:00
Pedro F. Giffuni c384a27805 patch: cleanup variable initialization a bit.
musl libc fgetln is a bit more pickier.

Hinted by:	chimera-linux (git 31491e1de2e1241885984cd9e4b978965f14eda4)
2021-07-22 14:23:22 -05:00
Xin LI 9769f6f808 usr.bin/patch: remove unneeded header.
MFC after:	2 weeks
2021-05-02 09:27:32 -07:00
Gordon Bergling a2f1c81b28 patch(1): Fix a few mandoc related issues
- no blank before trailing delimiter

MFC after:	1 week
2020-12-19 13:00:17 +00:00
Warner Losh c7cddf955f 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
2020-08-16 23:55:23 +00:00
Kyle Evans 50dacbf6c2 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
2019-11-04 03:07:01 +00:00
Kyle Evans 89b5571e0b patch(1): fix the file removal test, strengthen it a bit
To remain compatible with GNU patch, we should ensure that once we're
removing empty files after a reversed /dev/null patch we don't remove files
that have been modified. GNU patch leaves these intact and just reverses the
hunk that created the file, effectively implying --remove-empty-files for
reversed /dev/null patches.
2019-09-05 15:35:57 +00:00
Kyle Evans b27524973c patch(1): add some basic tests
Summary:
- basic: test application of patches created by diff -u at the
  beginning/middle/end of file, which have differing amounts of context
  before and after chunks being added
- limited_ctx: stems from PR 74127 in which a rogue line was getting added
  when the patch should have been rejected. Similar behavior was
  reproducible with larger contexts near the beginning/end of a file. See
  r326084 for details
- file_creation: patch sourced from /dev/null should create the file
- file_nodupe: said patch sourced from /dev/null shouldn't dupe the contents
  when re-applied (personal vendetta, WIP, see comment)
- file_removal: this follows from nodupe; the reverse of a patch sourced
  from /dev/null is most naturally deleting the file, as is expected based
  on GNU patch behavior (WIP)
2019-09-05 03:16:14 +00:00
Kyle Evans ef30b5a809 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
2019-03-01 01:20:21 +00:00
Kyle Evans 21ab148d78 patch(1): Don't check for NUL bytes in Plan A
Plan A mmap()'s the entire input file and operates on it in memory. The
map(2) call succeeded, so we shouldn't need to bother checking for the NUL
byte as long as we're within our buffer space.

This was clearly intentional to match "the behavior of the original code",
but it creates a discrepancy between Plan A and Plan B that doesn't seem
sensible and it's not inherently wrong to allow a NUL byte.

This change was motivated by the gemspec in net/rubygem-grpc failing to
patch, despite the patch being generated with diff, because a NUL byte was
used as a delimiter in the header briefly in an otherwise text file.

An alternative was considered: to fallback to plan B if plan A won't process
the entire file due to a NUL byte, but I deemed this to be the better option
since plan A isn't failing due to memory limitations and will fail later on
if it's really dealing with a file it shouldn't be.

PR:		224842 (exp-run)
Reported by:	swills
Reviewed by:	emaste, pfg
MFC after:	2 weeks
Differential Revision:	https://reviews.freebsd.org/D13738
2018-01-11 15:01:48 +00:00
Pedro F. Giffuni 76df519fe0 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)
2017-12-21 16:25:33 +00:00
Pedro F. Giffuni c844f14e37 patch: rejname[] is also -r option buffer, and should be PATH_MAX.
Obtained from:	OpenBSD (CVS 1.64)
2017-12-21 16:19:10 +00:00
Kyle Evans bc4f0fe358 patch(1): don't assume a match if we run out of context to check
Patches with very little context (-U0 and -U1) could get misapplied if
the file to be patched changes and a hunk is no longer applicable. Matching
with fuzz would be attempted and default to a match when we unexpectedly ran
out of context.

This also affected patches with higher levels of context but had limited
actual context due to the hunk being located near the beginning/end of file.

PR:		74127, 223545 (exp-run)
Reviewed by:	emaste, pfg
Approved by:	emaste (mentor)
Differential Revision:	https://reviews.freebsd.org/D12631
2017-11-22 03:44:19 +00:00
Pedro F. Giffuni 8a16b7a18f General further adoption of SPDX licensing ID tags.
Mainly focus on files that use BSD 3-Clause license.

The Software Package Data Exchange (SPDX) group provides a specification
to make it easier for automated tools to detect and summarize well known
opensource licenses. We are gradually adopting the specification, noting
that the tags are considered only advisory and do not, in any way,
superceed or replace the license texts.

Special thanks to Wind River for providing access to "The Duke of
Highlander" tool: an older (2014) run over FreeBSD tree was useful as a
starting point.
2017-11-20 19:49:47 +00:00
Kyle Evans 752e881db7 Revert r325365
r325365 caused several ports to fail to patch correctly. Revert it for the
time being until an exp-run can be completed.

Requested by:	antoine
Approved by:	emaste (implicit)
2017-11-08 23:11:15 +00:00
Kyle Evans ed3b305993 patch(1): don't assume a match if we run out of context to check
Patches with very little context (-U0 and -U1) could get misapplied if
the file to be patched changes and a hunk is no longer applicable. Matching
with fuzz would be attempted and default to a match when we unexpectedly ran
out of context.

PR:		74127
Reviewed by:	emaste, pfg
Approved by:	emaste (mentor)
Differential Revision:	https://reviews.freebsd.org/D12631
2017-11-03 17:04:30 +00:00
Bryan Drewery ea825d0274 DIRDEPS_BUILD: Update dependencies.
Sponsored by:	Dell EMC Isilon
2017-10-31 00:07:04 +00:00
Kyle Evans 4835edfa0d 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
2017-10-09 14:50:02 +00:00
Pedro F. Giffuni 85823a601c 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)
2017-07-02 21:00:30 +00:00
Pedro F. Giffuni 12300d3aa0 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
2017-06-08 03:15:08 +00:00
Pedro F. Giffuni 63433bc937 bc/dc/patch: make some use of reallocarray(3).
reallocarray(3) is a non portable extension from OpenBSD. Given that it is
already in FreeBSD, make easier future merges by adopting in some cases
where the code has some shared heritage with OpenBSD.

Obtained from:	OpenBSD
2017-03-05 16:10:35 +00:00
Pedro F. Giffuni fa81223743 patch(1): replace strnlen() with a simpler strlen().
Small style fix with here.

Pointed out by:	kib
2017-01-02 18:27:35 +00:00
Pedro F. Giffuni 4f548c1916 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
2017-01-02 18:23:31 +00:00
Pedro F. Giffuni ad8469feec 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
2017-01-02 17:12:14 +00:00
Pedro F. Giffuni 758a3cff59 patch(1): make some macros look boolean.
Similar to r306560, plus remove an unused macro.

Suggested by:	jmallett
2016-10-01 20:46:01 +00:00
Pedro F. Giffuni c7ef297a8e patch(1): make some macros look boolean.
Minor cleanup inspired by a new patch(1) variant in schily tools.

For reference:
https://sourceforge.net/p/schillix-on/

MFC after:	1 week
2016-10-01 20:31:00 +00:00
Pedro F. Giffuni 3548708c47 Adjust a type from r267490.
Independent of the maximum length, the return type for strnlen(3)
is always size_t.
2016-04-24 04:28:04 +00:00
Pedro F. Giffuni 2c4eed4723 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
2016-04-24 04:08:36 +00:00
Bryan Drewery b1f92fa229 META MODE: Update dependencies with 'the-lot' and add missing directories.
This is not properly respecting WITHOUT or ARCH dependencies in target/.
Doing so requires a massive effort to rework targets/ to do so.  A
better approach will be to either include the SUBDIR Makefiles directly
and map to DIRDEPS or just dynamically lookup the SUBDIR.  These lose
the benefit of having a userland/lib, userland/libexec, etc, though and
results in a massive package.  The current implementation of targets/ is
very unmaintainable.

Currently rescue/rescue and sys/modules are still not connected.

Sponsored by:	EMC / Isilon Storage Division
2015-12-01 05:23:19 +00:00
Eitan Adler 463a577b27 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
2015-10-21 05:37:09 +00:00
Xin LI e678759c30 Remove automatic checkout feature.
Obtained from:	DragonFly via OpenBSD
Relnotes:	yes
MFC:		never
2015-08-15 00:42:33 +00:00
Xin LI 812aa46f45 Use __DECONST instead of doing strdup/free.
Suggested by:	ed
MFC after:	2 weeks
2015-08-11 05:58:33 +00:00
Xin LI 2cf624f68a use posix_spawn(3) instead of fork() and exec() manually as suggested
by jmg@.

Reviewed By:	pfg
MFC after:	2 weeks
Differential Revision: https://reviews.freebsd.org/D3353
2015-08-10 21:31:50 +00:00
Xin LI 3fd78bfab2 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]
2015-08-05 22:04:54 +00:00
Xin LI 1e3e581593 Fix shell injection vulnerability in patch(1) and drop SCCS
support by replacing system() with execve().

Future revisions may remove the functionality completely.

Obtained from:	Bitrig
Security:	CVE-2015-1416
2015-07-28 19:58:36 +00:00
Conrad Meyer 300ca9a86e patch(1): Add -Vnone option to disable backup files
Differential Revision:	https://reviews.freebsd.org/D3146
Reviewed by:	pfg
Approved by:	markj (mentor)
MFC after:	1 week
Relnotes:	yes
Sponsored by:	EMC / Isilon Storage Division
2015-07-21 22:57:27 +00:00
Pedro F. Giffuni df6e4074c0 patch(1): small include changes.
Mostly to match OpenBSD, no functional change.

Obtained from:	OpenBSD
2015-04-20 22:15:18 +00:00
Pedro F. Giffuni a45060f0cc patch: Bring small updates from OpenBSD
Prevent null pointer dereference on empty input files when diff requires
a specific version.

Fix division by zero for files with long lines (> 1024) in Plan B mode
by supporting arbitrarily long lines.

Obtained from:	OpenBSD (CVS Rev 1.41, 1.42)
MFC after:	1 week
2015-04-07 18:06:46 +00:00
Pedro F. Giffuni f718bedc4f Fixes to exit status.
Exit with EXIT_FAILURE for invalid arguments.
Fixes NetBSD-PR 43517.

Print version string to stdout instead of stderr;
it is user-requested and not an error.

Obtained from:	NetBSD
MFC after:	5 days
2014-12-31 16:30:33 +00:00
Pedro F. Giffuni 547e0acbec 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
2014-12-25 21:51:28 +00:00
Pedro F. Giffuni d3fc0cb884 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
2014-12-08 15:10:48 +00:00