On Thu, Apr 02, 2026, Kirill A. Korinsky wrote:
> at least this hunk changes how patch works.

You are right, the !skip_rest_of_patch guard was wrong.
do_ed_script() must always run to consume the ed script
content so the next patch in the file can be found.

Updated diff below drops that guard.  The fd leak fix and
the stall detection in there_is_another_patch() are kept.

Tested with your example: the ed script is skipped and the
following unified diff creates foo.txt correctly.

Index: usr.bin/patch/patch.c
===================================================================
RCS file: /cvs/src/usr.bin/patch/patch.c,v
retrieving revision 1.78
diff -u -p -r1.78 patch.c
--- usr.bin/patch/patch.c       23 Feb 2026 16:40:45 -0000      1.78
+++ usr.bin/patch/patch.c
@@ -293,6 +293,12 @@ main(int argc, char *argv[])
                /* for ed script just up and do it and exit */
                if (diff_type == ED_DIFF) {
                        do_ed_script();
+                       if (ofp)
+                               fclose(ofp);
+                       ofp = NULL;
+                       if (rejfp)
+                               fclose(rejfp);
+                       rejfp = NULL;
                        continue;
                }

Index: usr.bin/patch/pch.c
===================================================================
RCS file: /cvs/src/usr.bin/patch/pch.c,v
retrieving revision 1.66
diff -u -p -r1.66 pch.c
--- usr.bin/patch/pch.c 12 Jul 2023 15:45:34 -0000      1.66
+++ usr.bin/patch/pch.c
@@ -180,6 +180,8 @@ static char *posix_name(const struct fil
 bool
 there_is_another_patch(void)
 {
+       static off_t prev_p_start;
+       static int prev_diff_type;
        bool exists = false;

        if (p_base != 0 && p_base >= p_filesize) {
@@ -190,6 +192,14 @@ there_is_another_patch(void)
        if (verbose)
                say("Hmm...");
        diff_type = intuit_diff_type();
+       if (diff_type && p_start == prev_p_start &&
+           diff_type == prev_diff_type) {
+               if (verbose)
+                       say("  Ignoring the trailing garbage.\ndone\n");
+               return false;
+       }
+       prev_p_start = p_start;
+       prev_diff_type = diff_type;
        if (!diff_type) {
                if (p_base != 0) {
                        if (verbose)

Reply via email to