679 Commits

Author SHA1 Message Date
Daniel Black
d161c9a9db Skip read-only check when output file specified
Only check if input files are read-only when overwriting them; skip
that check when the output goes to a different file.

* src/patch.c (main): Set read_only_behavior to RO_IGNORE when an output
file has been specified.
* tests/read-only-files: Add test case.
2025-05-01 13:40:20 +02:00
Andreas Gruenbacher
4c302306a8 Reject empty filenames
* src/safe.c (safe_xstat): Reject empty pathnames.
* tests/bad-filenames: Add a new test.
* src/patch.c (main): Don't check if the input file is writable when
we're not going to modify it.
2025-04-05 17:26:56 +02:00
Andreas Gruenbacher
aea66268c9 Add some missing filename quoting
* src/patch.c (main): Quote outname.
2025-04-05 17:25:45 +02:00
Andreas Gruenbacher
77c27209f6 Fix 'make release' for proper releases
(Proper releases have a signed git tag.)
2025-03-29 22:40:26 +01:00
Andreas Gruenbacher
48ceda8200 Version 2.8
* NEWS: Update.
v2.8
2025-03-29 17:17:03 +01:00
Petr Vaněk
b3d0c93338 Regression in commit abe92e8010ab affecting MariaDB tests
I have disovered a regression in commit abe92e8010ab ("Prefer idx_t,
ptrdiff_t to lin") while I was running MariaDB tests. The regression is
related to a diff file [1], where the patch fails to apply it with
following error:

  patch: **** '---' expected at line 2 of patch

To illustrate the issue, I have attached a git patch containing a
testcase with simplified reproducer.

[1] https://github.com/MariaDB/server/blob/mariadb-10.6.21/mysql-test/suite/innodb/r/innodb-wl5522%2Cstrict_crc32.rdiff file
2025-02-26 16:46:07 -08:00
Paul Eggert
29fced6251 Count traditional diff pattern lines correctly
This fixes a bug I introduced on Thu Sep 5 16:37:50 2024 -0700.
Problem reported by Petr Vaněk in:
https://lists.gnu.org/archive/html/bug-patch/2025-02/msg00017.html
* src/pch.c (another_hunk): Fix method for counting number
of lines in a traditional diff hunk.
2025-02-26 16:46:07 -08:00
Paul Eggert
b5d2124e2e patch: fix --no-backup-if-mismatch regression
Problem reported by Sam James in:
https://lists.gnu.org/archive/html/bug-patch/2025-02/msg00014.html
https://bugs.gentoo.org/show_bug.cgi?id=949834
* src/patch.c (backup_if_mismatch_specified): New static var.
(get_some_switches): Set it.
(main): Default backup_if_mismatch only if not set on command line.
* tests/no-backup: New file.
* tests/Makefile.am (TESTS): Add it.
2025-02-24 23:29:59 -08:00
Sam James
86ac7e2d7b Fix dodgy assert with side-effects in insert_cached_dirfd
Michał Górny <mgorny@gentoo.org> reported that patch was running out of
FDs and that the `deep-directories` test was failing. This turns out
to be because `hash_insert` isn't called at all with `-DNDEBUG` because
`insert_cached_dirfd` only calls it in one case inside of an `assert`.

See https://github.com/conda-forge/patch-feedstock/issues/11.

This regressed in 025a54b789bd88ed15430f8633514e296826983e.

* src/safe.c (insert_cached_dirfd): Don't use 'assert' for 'hash_insert'
  call with side-effects.
2025-02-06 21:13:20 +01:00
Bruno Haible
7d876522e9 Declare an expected test failure on Haiku.
* bootstrap.conf (gnulib_modules): Add test-xfail.
* tests/preserve-mode-and-timestamp: Add a comment regarding Haiku.
* tests/Makefile.am (XFAIL_TESTS): On Haiku, add preserve-mode-and-timestamp.
2025-02-06 21:03:10 +01:00
Andreas Gruenbacher
86baf979f4 build: update gnulib submodule to latest 2025-02-06 21:02:41 +01:00
Bruno Haible
1ba2c1bdf8 Fix two test failures on Haiku.
On Haiku, all error numbers are negative, see
<https://www.gnu.org/software/gnulib/manual/html_node/errno_002eh.html>.

Bisected by Eli Schwartz <eschwartz@gentoo.org>.

This partially reverts commit 043355371a76de8ea7d06f79a69fde905af7cc45.

* src/inp.c (get_input_file):
* src/patch.c (main):
* src/safe.c (read_symlink):
* src/util.c (move_file):
Don't assume that all system-defined errno values are positive.
2025-02-05 13:29:32 -08:00
Paul Eggert
1da6bf84db Check for newlines only when creating a file name
Also, check only the last file name component.
In other words, mimic operating systems that follow POSIX.1-2024’s
encouragement to fail with EILSEQ when openat etc. create a file name.
This is more conservative than the previous patch to prohibit
newlines in file names.
* src/patch.c (main, backup_file_name_option, get_some_switches):
* src/util.c (parse_c_string, make_tempfile):
Don’t check for newlines in a file name unless we are definitely
creating a file, as it’s harmless to read and stat file with
newlines in their names if the OS allows that.
* src/safe.c (traverse_another_path, traverse_path): New arg
REJECT_NL.  If set, reject any file name whose last component
contains a newline.  Also, do not do traversal if unsafe.  All
callers changed to pass true if they are creating the file name,
false otherwise, and to not bother checking whether we are unsafe.
(safe_open): Special case for when O_CREAT is set but O_EXCL is not.
* src/util.c (pfatal): Report "Invalid byte sequence" for EILSEQ.
This POSIX wording is less confusing than glibc's "Invalid or
incomplete multibyte or wide character".  Also, this lets
the test cases check for this wording.
* tests/bad-filenames: Adjust to new diagnostic wording.
2025-01-08 09:53:45 -08:00
Paul Eggert
30ee6103a5 Gnulib renamed some modules
* bootstrap.conf (gnulib_modules):
Adjust to recent module renaming in Gnulib.
2025-01-05 13:33:00 -08:00
Paul Eggert
6dbc381055 maint: update bootstrap from gnulib 2025-01-05 13:33:00 -08:00
Paul Eggert
05ac924fef build: update gnulib submodule to latest 2025-01-05 13:33:00 -08:00
Paul Eggert
95e0092f97 maint: make update-copyright 2025-01-05 13:33:00 -08:00
Andreas Gruenbacher
5bac274507 Set --no-backup-if-mismatch when in --posix mode
When in POSIX mode, the --no-backup-if-mismatch option should be
enabled.  However, this is only true when the POSIXLY_CORRECT
environmant variable is set but not when the --posix command line
option is given.  Fix that by setting backup_if_mismatch after
evaluating the command line arguments.
2024-11-20 23:54:48 +01:00
Andreas Gruenbacher
910fecf695 Add missing feature tests to the test suite
Check for chmod, hardlink, symlink, and special character support to
prevent test suite failures in feature constrained environments.

Thanks to Bruno Haible and Nelson H. F. Beebe for their testing and
analysis.

* tests/test-lib.sh: Add new feature tests.
* tests/hardlinks: Split this hardlinks related test off from
tests/remember-backup-files.
* tests/Makefile.am (TESTS): Add new hardlinks test here.
* tests/file-create-modes, tests/file-modes, tests/read-only-files,
tests/preserve-mode-and-timestamp, tests/no-mode-change-git-diff: These
tests require chmod support.
* tests/hardlinks, tests/unmodified-files: These tests require hardlink
support.
* tests/symlinks: This test requires symlink support.
* tests/quoted-filenames: This test requires special character support
in filenames.
2024-11-11 22:21:27 +01:00
Andreas Gruenbacher
be8b3c68b0 Disable release-prep
* cfg.mk: Disable release-prep by overriding the release-prep-hook for
now.  With that, "make release" succeeds for alpha releases.
2024-10-11 13:56:15 +02:00
Eli Schwartz
c61485bd05 Fix "make release" to handle alpha releases
news-check-regexp to scan for unreleased changes, has to be set
conditional on the release type. It has to be defined in cfg.mk, not
Makefile.am, so key off of the RELEASE_TYPE as automake conditionals are
not available.

Signed-off-by: Eli Schwartz <eschwartz@gentoo.org>
2024-10-11 13:53:07 +02:00
Andreas Gruenbacher
499916fdd8 Add announce-gen module for "make release"
* bootstrap.conf (gnulib_modules): Add announce-gen.
2024-10-11 11:43:59 +02:00
Paul Eggert
adb1ebce7f Pacify gcc -fsanitize=address
Some of this merely pacifies -fsanitize=address by pointing
to storage rather than freeing it when we are about to exit anyway.
Other parts of it keep track of storage more carefully so that it
can be freed rather than leak.
* src/common.h (struct outfile): New member ‘alloc’.
* src/patch.c (files_to_output_root) [SANITIZE_ADDRESS]:
New externally visible variable, to pacify -fsanitize=address.
(main): Use new functions described below to remove
files and free memory.
(delete_files): Do not free the list as we go, as we are about
to exit and -fsanitize=address doesn’t care about this storage.
(output_file_later): Set and use new member ‘alloc’ to avoid
memory leaks.
(output_files): WIth -fsanitize=address, record the list head
in files_to_output_root that the address sanitizer can see,
so that it won’t complain when we don’t free storage just before exit.
Free only when not exiting, and free using ‘alloc’ rather than ‘name’.
(perfile_cleanup_remove): New function.
(cleanup_remove): Rename from ‘cleanup’.  All uses changed.
Reimplement in terms of perfile_cleanup_remove.
(free_outfile_name, perfile_cleanup_free): New functions.
* src/util.c (make_tempfile): Set new member ‘alloc’.
2024-09-20 00:20:06 -07:00
Paul Eggert
6bdae94eb3 Fix memory leak when malformed unidiff patch
* src/pch.c (another_hunk): Fix memory leak when scanning a
unidiff patch malformed due to a line that does not begin with
‘ ’, ‘-’, ‘+’, or ‘=’.
2024-09-20 00:20:06 -07:00
Paul Eggert
72a146cfaf Port to clang address sanitizer
* src/patch.c (FREE_BEFORE_EXIT): Port to clang, which
uses __has_feature (address_sanitizer) instead of
defined __SANITIZE_ADDRESS__.  Also, rename this to
SANITIZE_ADDRESS since it is really about -fsanitize=address
rather than freeing before exit and as subsequent patches
will show there are simpler ways to pacify -fsanitize=address.
All uses changed.
2024-09-20 00:20:06 -07:00
Paul Eggert
e2e6820f71 Refactor argc+argv processing
* src/patch.c (Argc, Argv): Remove these confusing static variables.
They date back to before the code used getopt_long,
and are no longer needed.  All uses changed.
(get_some_switches): New args argc+argv, which are now used
instead of the static vars.  All uses changed.
2024-09-20 00:20:06 -07:00
Paul Eggert
606c09176c Omit needless get_some_switches code
* src/patch.c (get_some_switches): Remove unnecessary
initialization and testing.
2024-09-20 00:20:06 -07:00
Andreas Gruenbacher
ee3cc4016d Revert "Remove obsolete require_gnu_diff function"
Oops, function require_gnu_diff is still needed in two places.

This reverts commit 8cae4fc2213649e36e8f9a4cf21c28a82de3705c.
2024-09-18 14:51:33 +02:00
Andreas Gruenbacher
8cae4fc221 Remove obsolete require_gnu_diff function
* tests/test-lib.sh (require_gnu_diff): Remove.
2024-09-18 14:42:49 +02:00
Paul Eggert
164b529f53 savebuf can return a null pointer
* src/util.h (savebuf): Do not declare with ATTRIBUTE_RETURNS_NONNULL.
Bug caught by gcc -fsanitize=undefined.
2024-09-17 23:46:23 -07:00
Paul Eggert
91c1e4f071 Spelling fixes 2024-09-17 22:51:04 -07:00
Paul Eggert
a03e1bac4a Port other reject-format test to non-GNU diff
* tests/reject-format: Also don’t assume GNU diff
for the ab.diff file.  Problem discovered on Solaris 11.4.
2024-09-17 22:28:45 -07:00
Paul Eggert
9ba5eb00b3 Don’t be fooled by NUL bytes in diff directives
* src/pch.c (get_line, pget_line): New arg ALLOW_NUL.
It is true when getting data lines, which can contain NUL,
but false when getting ‘diff’ directives, which cannot.
All uses changed.
* tests/bad-filenames: Check that ‘patch’ rejects
directives containing NUL.
2024-09-17 17:43:23 -07:00
Paul Eggert
79dd5e762c Don’t be fooled by "\000" in file name
* src/util.c (parse_c_string): Don’t be tricked by perverse
C-style quoted strings containing backslash, '0', '0', '0'.
* tests/quoted-filenames: Test this.
2024-09-17 17:43:23 -07:00
Paul Eggert
8492a6a212 Port to quasi-GNU diff
Problem reported by Jim Meyering: ‘diff’ acted like GNU diff,
and generated correct output, but the output differed slightly
from what the test wanted.  As the output of ‘diff’ is not
completely determined from its input, it’s better to put
the desired ‘diff’ output directly in the test when the test
depends on the exact output.
* tests/preserve-c-function-names, tests/reject-format:
Do not require GNU diff or use ‘diff’.
Instead, cat the desired ‘diff’ output.
2024-09-17 17:43:23 -07:00
Paul Eggert
f6f2c6f31a In previous patch, make w_q static 2024-09-17 08:32:25 -07:00
Paul Eggert
0525681eb5 Pacify -Wunterminated-string-initialization
Problem reported by Jim Meyering.
* src/pch.c (do_ed_script): Pacify bleeding-edge GCC
-Wunterminated-string-initialization.
2024-09-17 08:27:49 -07:00
Andreas Gruenbacher
301411d927 Spelling fixes
Prefer 'timestamp' over 'time stamp'.
2024-09-17 10:59:25 +02:00
Andreas Gruenbacher
461520693e Remove double semicolon
* src/safe.c (remove_cached_dirfd): Remove a stray double semicolon.
2024-09-17 10:59:25 +02:00
Andreas Gruenbacher
923e0ef280 Prefer angle bracket headers
* src/util.h: Prefer angle brackets for gnulib header files.
2024-09-17 10:59:25 +02:00
Andreas Gruenbacher
5d17ca0e5f Update more old copyright notices
* configure.ac: Replace the old copyright notice with the current
version from COPYING.
* ChangeLog-2011: Likewise.
2024-09-17 10:59:15 +02:00
Paul Eggert
5f4edd389e Simplify memory allocation of files to delete
This pacifies ‘make sc_cast_of_argument_to_free’, which otherwise
complains about the ‘free ((void *) elt)’ in dispose_file_to_delete.
Rather than worry about pacifying that ‘make’ rule,
simplify memory allocation by doing the linked list by hand,
with a ‘next’ member the way our grandfathers did it.
This reduces the number of source code lines by 23,
removes the need for Gnulib’s linked-list and xlist modules,
and makes the code type-safer (as opposed to going through void *).
* bootstrap.conf (gnulib_modules): Remove linked-list, xlist.
* src/patch.c: Do not include gl_linked_list.h, gl_xlist.h.
(struct file_to_delete): New member ‘next’.
(files_to_delete): Now struct file_to_delete *, not gl_list_t.
(dispose_file_to_delete, init_files_to_delete):
Remove; no longer needed.
(files_to_delete_tail): New static var.
(delete_file_later): Append the new file by hand.
(delete_files): Iterate and free by hand.
2024-09-16 22:58:28 -07:00
Paul Eggert
802511c09b Report patch read errors more immediately
* src/pch.c (open_patch_file): Cache patch file descriptor.
When reading a patch, report read errors right away rather
than possibly waiting until end of input.
2024-09-16 17:39:10 -07:00
Paul Eggert
a93b50de71 Port fflush usage to OpenBSD 7.5
* src/inp.c (scan_input): Accept file descriptor, not stream.
All callers changed.
* src/patch.c (main): Do not obtain a stream for the patch
file descriptor, as scan_input merely needs a file descriptor.
This removes the need to call Fclose, which calls fflush,
which fails on OpenBSD 7.5 which (contra POSIX) does not
let you fflush an input stream.
2024-09-16 17:39:10 -07:00
Andreas Gruenbacher
55e224bd73 Update old copyright notices
* NEWS: Replace the old copyright notice with the current version from
COPYING.
* m4/setmode.m4: Likewise.
2024-09-17 00:07:22 +02:00
Paul Eggert
33a7fd89ec Fix gl_gcc_warnings typo in ‘configure’
* configure.ac (gl_gcc_warnings): Always set before using,
fixing a typo I introduced Sat Aug 24 08:28:18 2024 -0700.
2024-09-16 14:11:43 -07:00
Paul Eggert
2313b37f6a build: update gnulib submodule to latest 2024-09-16 13:35:24 -07:00
Paul Eggert
37fec39c58 Remove some dependencies no longer needed
* bootstrap.conf (gnulib_modules): Remove clock-time,
nstrftime, stdc_bit_ceil, time.
* src/inp.c: Do not include stdbit.h.
2024-09-16 13:34:01 -07:00
Paul Eggert
cc87173e46 Update NEWS as per recent changes 2024-09-15 22:50:38 -07:00
Paul Eggert
7887622b77 Update POSIX citations 2024-09-15 22:50:38 -07:00