From 3e3761cbac56ba207a293928e9df0e864239c11b Mon Sep 17 00:00:00 2001 From: "Xibo.Wang" Date: Fri, 30 Dec 2022 14:37:01 +0800 Subject: [PATCH] Bakport commits from upstream 1. Skip "ed" test when the ed utility is not installed https://git.savannah.gnu.org/gitweb/?p=patch.git;a=commit;h=a5b442c 2. Abort when cleaning up fails https://git.savannah.gnu.org/gitweb/?p=patch.git;a=commit;h=b7b028a 3. Don't crash when RLIMIT_NOFILE is set to RLIM_INFINITY https://git.savannah.gnu.org/gitweb/?p=patch.git;a=commit;h=61d7788 4. Avoid invalid memory access in context format diffs https://git.savannah.gnu.org/gitweb/?p=patch.git;a=commit;h=15b158d 5. Fix failed assertion 'outstate->after_newline' https://git.savannah.gnu.org/gitweb/?p=patch.git;a=commit;h=76e7758 6. Add missing-section tests to context-format test case https://git.savannah.gnu.org/gitweb/?p=patch.git;a=commit;h=78ed9de 7. Fix test for presence of BASH_LINENO[0] https://git.savannah.gnu.org/gitweb/?p=patch.git;a=commit;h=7623b2d --- backport-Abort-when-cleaning-up-fails.patch | 53 +++++++ ...ion-tests-to-context-format-test-cas.patch | 132 ++++++++++++++++++ ...emory-access-in-context-format-diffs.patch | 28 ++++ ...-RLIMIT_NOFILE-is-set-to-RLIM_INFINI.patch | 91 ++++++++++++ ...led-assertion-outstate-after_newline.patch | 36 +++++ ...x-test-for-presence-of-BASH_LINENO-0.patch | 34 +++++ ...when-the-ed-utility-is-not-installed.patch | 32 +++++ patch.spec | 18 ++- 8 files changed, 423 insertions(+), 1 deletion(-) create mode 100644 backport-Abort-when-cleaning-up-fails.patch create mode 100644 backport-Add-missing-section-tests-to-context-format-test-cas.patch create mode 100644 backport-Avoid-invalid-memory-access-in-context-format-diffs.patch create mode 100644 backport-Don-t-crash-when-RLIMIT_NOFILE-is-set-to-RLIM_INFINI.patch create mode 100644 backport-Fix-failed-assertion-outstate-after_newline.patch create mode 100644 backport-Fix-test-for-presence-of-BASH_LINENO-0.patch create mode 100644 backport-Skip-ed-test-when-the-ed-utility-is-not-installed.patch diff --git a/backport-Abort-when-cleaning-up-fails.patch b/backport-Abort-when-cleaning-up-fails.patch new file mode 100644 index 0000000..d9f4371 --- /dev/null +++ b/backport-Abort-when-cleaning-up-fails.patch @@ -0,0 +1,53 @@ +From b5c17c6c6591e62173197f97d3113862bf23bd4d Mon Sep 17 00:00:00 2001 +From: Andreas Gruenbacher +Date: Fri, 28 Jun 2019 00:30:25 +0200 +Subject: [PATCH 2/8] Abort when cleaning up fails + +When a fatal error triggers during cleanup, another attempt will be made to +clean up, which will likely lead to the same fatal error. So instead, bail out +when that happens. +src/patch.c (cleanup): Bail out when called recursively. +(main): There is no need to call output_files() before cleanup() as cleanup() +already does that. + +Signed-off-by: Xibo.Wang +--- + src/patch.c | 8 ++++++-- + 1 file changed, 6 insertions(+), 2 deletions(-) + +diff --git a/src/patch.c b/src/patch.c +index e57cf19..1e1915d 100644 +--- a/src/patch.c ++++ b/src/patch.c +@@ -685,7 +685,6 @@ main (int argc, char **argv) + } + if (outstate.ofp && (ferror (outstate.ofp) || fclose (outstate.ofp) != 0)) + write_fatal (); +- output_files (NULL); + cleanup (); + delete_files (); + if (somefailed) +@@ -1991,7 +1990,6 @@ void + fatal_exit (int sig) + { + cleanup (); +- + if (sig) + exit_with_signal (sig); + +@@ -2011,6 +2009,12 @@ remove_if_needed (char const *name, bool *needs_removal) + static void + cleanup (void) + { ++ static bool already_cleaning_up; ++ ++ if (already_cleaning_up) ++ return; ++ already_cleaning_up = true; ++ + remove_if_needed (TMPINNAME, &TMPINNAME_needs_removal); + remove_if_needed (TMPOUTNAME, &TMPOUTNAME_needs_removal); + remove_if_needed (TMPPATNAME, &TMPPATNAME_needs_removal); +-- +1.8.3.1 + diff --git a/backport-Add-missing-section-tests-to-context-format-test-cas.patch b/backport-Add-missing-section-tests-to-context-format-test-cas.patch new file mode 100644 index 0000000..ed56629 --- /dev/null +++ b/backport-Add-missing-section-tests-to-context-format-test-cas.patch @@ -0,0 +1,132 @@ +From 3a4a357daa920361d709bbc4cf43865dff769112 Mon Sep 17 00:00:00 2001 +From: Andreas Gruenbacher +Date: Tue, 30 Jul 2019 12:10:19 +0200 +Subject: [PATCH 6/8] Add missing-section tests to context-format test case + +* tests/context-format: Add tests with a missing pattern and a missing +replacement section in a hunk. Patch should fill in the missing +sections from the existing sections. + +Signed-off-by: Xibo.Wang +--- + tests/context-format | 86 ++++++++++++++++++++++++++++++++++++++++++++++++++-- + 1 file changed, 84 insertions(+), 2 deletions(-) + +diff --git a/tests/context-format b/tests/context-format +index 8143448..b3276ff 100644 +--- a/tests/context-format ++++ b/tests/context-format +@@ -11,6 +11,46 @@ use_tmpdir + + # ============================================================== + ++printf "%s\n" 1 2 4 5 > a ++cat > ab.diff < a ++cat > ab.diff < a ++ + cat > ab.diff < ab.diff < a + check 'patch < ab.diff' < ba.diff < a ++ + cat > ab.diff < ab.diff < a + check 'patch < ab.diff' < ba.diff < +Date: Mon, 15 Jul 2019 19:10:02 +0200 +Subject: [PATCH 4/8] Avoid invalid memory access in context format diffs + +* src/pch.c (another_hunk): Avoid invalid memory access in context format +diffs. + +Signed-off-by: Xibo.Wang +--- + src/pch.c | 1 + + 1 file changed, 1 insertion(+) + +diff --git a/src/pch.c b/src/pch.c +index aa0caf4..87e6f93 100644 +--- a/src/pch.c ++++ b/src/pch.c +@@ -1335,6 +1335,7 @@ another_hunk (enum diff difftype, bool rev) + ptrn_prefix_context = context; + ptrn_suffix_context = context; + if (repl_beginning ++ || p_end <= 0 + || (p_end + != p_ptrn_lines + 1 + (p_Char[p_end - 1] == '\n'))) + { +-- +1.8.3.1 + diff --git a/backport-Don-t-crash-when-RLIMIT_NOFILE-is-set-to-RLIM_INFINI.patch b/backport-Don-t-crash-when-RLIMIT_NOFILE-is-set-to-RLIM_INFINI.patch new file mode 100644 index 0000000..d1c8947 --- /dev/null +++ b/backport-Don-t-crash-when-RLIMIT_NOFILE-is-set-to-RLIM_INFINI.patch @@ -0,0 +1,91 @@ +From 2523dced822caae56f24eff4899c6307e1844df6 Mon Sep 17 00:00:00 2001 +From: Andreas Gruenbacher +Date: Thu, 27 Jun 2019 11:10:43 +0200 +Subject: [PATCH 3/8] Don't crash when RLIMIT_NOFILE is set to RLIM_INFINITY + +* src/safe.c (min_cached_fds): Define minimum number of cached dir file +descriptors. +(max_cached_fds): Change type to rlim_t to allow storing RLIM_INFINITY. +(init_dirfd_cache): Set max_cached_fds to RLIM_INFINITY when RLIMIT_NOFILE is +RLIM_INFINITY. Set the initial hash table size to min_cached_fds, independent +of RLIMIT_NOFILE: patches commonly only affect one or a few files, so a small +hash table will usually suffice; if needed, the hash table will grow. +(insert_cached_dirfd): Don't shrink the cache when max_cached_fds is +RLIM_INFINITY. + +Signed-off-by: Xibo.Wang +--- + src/safe.c | 36 +++++++++++++++++++++++------------- + 1 file changed, 23 insertions(+), 13 deletions(-) + +diff --git a/src/safe.c b/src/safe.c +index 5a7202f..f147b0e 100644 +--- a/src/safe.c ++++ b/src/safe.c +@@ -67,7 +67,8 @@ struct cached_dirfd { + }; + + static Hash_table *cached_dirfds = NULL; +-static size_t max_cached_fds; ++static rlim_t min_cached_fds = 8; ++static rlim_t max_cached_fds; + LIST_HEAD (lru_list); + + static size_t hash_cached_dirfd (const void *entry, size_t table_size) +@@ -98,11 +99,17 @@ static void init_dirfd_cache (void) + { + struct rlimit nofile; + +- max_cached_fds = 8; + if (getrlimit (RLIMIT_NOFILE, &nofile) == 0) +- max_cached_fds = MAX (nofile.rlim_cur / 4, max_cached_fds); ++ { ++ if (nofile.rlim_cur == RLIM_INFINITY) ++ max_cached_fds = RLIM_INFINITY; ++ else ++ max_cached_fds = MAX (nofile.rlim_cur / 4, min_cached_fds); ++ } ++ else ++ max_cached_fds = min_cached_fds; + +- cached_dirfds = hash_initialize (max_cached_fds, ++ cached_dirfds = hash_initialize (min_cached_fds, + NULL, + hash_cached_dirfd, + compare_cached_dirfds, +@@ -148,20 +155,23 @@ static void insert_cached_dirfd (struct cached_dirfd *entry, int keepfd) + if (cached_dirfds == NULL) + init_dirfd_cache (); + +- /* Trim off the least recently used entries */ +- while (hash_get_n_entries (cached_dirfds) >= max_cached_fds) ++ if (max_cached_fds != RLIM_INFINITY) + { +- struct cached_dirfd *last = +- list_entry (lru_list.prev, struct cached_dirfd, lru_link); +- if (&last->lru_link == &lru_list) +- break; +- if (last->fd == keepfd) ++ /* Trim off the least recently used entries */ ++ while (hash_get_n_entries (cached_dirfds) >= max_cached_fds) + { +- last = list_entry (last->lru_link.prev, struct cached_dirfd, lru_link); ++ struct cached_dirfd *last = ++ list_entry (lru_list.prev, struct cached_dirfd, lru_link); + if (&last->lru_link == &lru_list) + break; ++ if (last->fd == keepfd) ++ { ++ last = list_entry (last->lru_link.prev, struct cached_dirfd, lru_link); ++ if (&last->lru_link == &lru_list) ++ break; ++ } ++ remove_cached_dirfd (last); + } +- remove_cached_dirfd (last); + } + + /* Only insert if the parent still exists. */ +-- +1.8.3.1 + diff --git a/backport-Fix-failed-assertion-outstate-after_newline.patch b/backport-Fix-failed-assertion-outstate-after_newline.patch new file mode 100644 index 0000000..d1cb383 --- /dev/null +++ b/backport-Fix-failed-assertion-outstate-after_newline.patch @@ -0,0 +1,36 @@ +From 2ab603a64f404d4724a867b8cf9f08ccacbff7c6 Mon Sep 17 00:00:00 2001 +From: Andreas Gruenbacher +Date: Tue, 16 Jul 2019 01:16:28 +0200 +Subject: [PATCH 5/8] Fix failed assertion 'outstate->after_newline' + +The assertion triggers when the -o FILE option is used, more than one output +file is written into FILE, and one of those files (except the last one) ends in +the middle of a line. +* src/patch.c (main): Fix the case described above. + +Signed-off-by: Xibo.Wang +--- + src/patch.c | 7 +++++++ + 1 file changed, 7 insertions(+) + +diff --git a/src/patch.c b/src/patch.c +index 1e1915d..9684794 100644 +--- a/src/patch.c ++++ b/src/patch.c +@@ -369,6 +369,13 @@ main (int argc, char **argv) + /* outstate.ofp now owns the file descriptor */ + outfd = -1; + } ++ else ++ { ++ /* When writing to a single output file (-o FILE), always pretend ++ that the output file ends in a newline. Otherwise, when another ++ file is written to the same output file, apply_hunk will fail. */ ++ outstate.after_newline = true; ++ } + + /* find out where all the lines are */ + if (!skip_rest_of_patch) { +-- +1.8.3.1 + diff --git a/backport-Fix-test-for-presence-of-BASH_LINENO-0.patch b/backport-Fix-test-for-presence-of-BASH_LINENO-0.patch new file mode 100644 index 0000000..e01c05d --- /dev/null +++ b/backport-Fix-test-for-presence-of-BASH_LINENO-0.patch @@ -0,0 +1,34 @@ +From 1b33a17c9a79de1b6d5acb4da76d3fcedd8dc262 Mon Sep 17 00:00:00 2001 +From: Kerin Millar +Date: Sun, 3 Jan 2021 07:25:00 +0000 +Subject: [PATCH 7/8] Fix test for presence of BASH_LINENO[0] + +eval is not some sort of magical sandbox for executing code that might cause +the shell's parser to take exception. Render the test resilient by carrying +it out within a subshell. While at it, position the redirection so that +STDERR is, in fact, muted. + +Signed-off-by: Kerin Millar +Reported-by: Paolo Pedroni +Closes: https://bugs.gentoo.org/738810 +Signed-off-by: Xibo.Wang +--- + tests/test-lib.sh | 2 +- + 1 file changed, 1 insertion(+), 1 deletion(-) + +diff --git a/tests/test-lib.sh b/tests/test-lib.sh +index 661da52..b2e787d 100644 +--- a/tests/test-lib.sh ++++ b/tests/test-lib.sh +@@ -113,7 +113,7 @@ cleanup() { + exit $status + } + +-if eval 'test -n "${BASH_LINENO[0]}" 2>/dev/null'; then ++if ( eval 'test -n "${BASH_LINENO[0]}"' 2>/dev/null ); then + eval ' + _start_test() { + printf "[${BASH_LINENO[2]}] %s -- " "$*" +-- +1.8.3.1 + diff --git a/backport-Skip-ed-test-when-the-ed-utility-is-not-installed.patch b/backport-Skip-ed-test-when-the-ed-utility-is-not-installed.patch new file mode 100644 index 0000000..51582bf --- /dev/null +++ b/backport-Skip-ed-test-when-the-ed-utility-is-not-installed.patch @@ -0,0 +1,32 @@ +From 78ee5267f712c38f3343964533fb9fb7c4f70539 Mon Sep 17 00:00:00 2001 +From: Andreas Gruenbacher +Date: Thu, 27 Jun 2019 11:09:31 +0200 +Subject: [PATCH 1/8] Skip "ed" test when the ed utility is not installed + +* tests/ed-style: Require ed. + +Signed-off-by: Xibo.Wang + +Conflicts: + tests/ed-style +--- + tests/ed-style | 3 ++- + 1 file changed, 2 insertions(+), 1 deletion(-) + +diff --git a/tests/ed-style b/tests/ed-style +index ca8e958..9907cb6 100644 +--- a/tests/ed-style ++++ b/tests/ed-style +@@ -6,7 +6,8 @@ + + . $srcdir/test-lib.sh + +-require_cat ++require cat ++require ed + use_local_patch + use_tmpdir + +-- +1.8.3.1 + diff --git a/patch.spec b/patch.spec index b988c43..a85374e 100644 --- a/patch.spec +++ b/patch.spec @@ -1,6 +1,6 @@ Name: patch Version: 2.7.6 -Release: 19 +Release: 20 Summary: Utiliity which applies a patch file to original files. License: GPLv3+ URL: http://www.gnu.org/software/patch/patch.html @@ -20,6 +20,13 @@ Patch11: Test-suite-fix-Korn-shell-incompatibility.patch Patch12: backport-maint-avoid-warnings-from-GCC8.patch Patch13: backport-Make-the-debug-2-output-more-useful.patch Patch14: backport--Improve-support-for-memory-leak-detection.patch +Patch15: backport-Skip-ed-test-when-the-ed-utility-is-not-installed.patch +Patch16: backport-Abort-when-cleaning-up-fails.patch +Patch17: backport-Don-t-crash-when-RLIMIT_NOFILE-is-set-to-RLIM_INFINI.patch +Patch18: backport-Avoid-invalid-memory-access-in-context-format-diffs.patch +Patch19: backport-Fix-failed-assertion-outstate-after_newline.patch +Patch20: backport-Add-missing-section-tests-to-context-format-test-cas.patch +Patch21: backport-Fix-test-for-presence-of-BASH_LINENO-0.patch BuildRequires: gcc libselinux-devel libattr-devel ed Buildroot: %{_tmppath}/%{name}-%{version}-%{release}-root-root @@ -62,6 +69,15 @@ CFLAGS="$RPM_OPT_FLAGS -D_GNU_SOURCE" %{_mandir}/man1/* %changelog +* Fri Dec 30 2022 Xibo.Wang - 2.7.6-20 +- Skip "ed" test when the ed utility is not installed +- Abort when cleaning up fails +- Don't crash when RLIMIT_NOFILE is set to RLIM_INFINITY +- Avoid invalid memory access in context format diffs +- Fix failed assertion 'outstate->after_newline' +- Add missing-section tests to context-format test case +- Fix test for presence of BASH_LINENO[0] + * Wed Dec 28 2022 Xibo.Wang - 2.7.6-19 - Improve support for memory leak detection