Re: [PATCH] patch: handle files with no final newline

"Roberto A. Foglietta via busybox" <[email protected]> Sun, 19 Apr 2026 12:38:15 +0200
Newsgroups gmane.linux.busybox
Message-ID <CAJGKYO4uaB9WmfDJvCF77ypVUqiTurrj8FSxybiTr1X6nb4KzQ@mail.gmail.com>
On Sun, 19 Apr 2026 at 11:58, Roberto A. Foglietta
<[email protected]> wrote:
>
> On Sun, 19 Apr 2026 at 11:04, Ron Yorston via busybox
> <[email protected]> wrote:

> if(*patchline == '\\') {
> do_stuff; continue;
> }
>
> Using keyword "continue" into a "switch" isn't a good idea because the
> "continue"/"break" duo would not refer to the same level of nested
> coding: "break" to the "switch" and "continue" to the upper loop.
> Therefore if(new case) continue; if(like before) is a better stylistic
> choice rather than injecting a "continue" into a "switch"/"case".
> However, it is not a mistake by itself.
>

Ron's patch applied and on the top of it another one for code janitoring.

https://github.com/robang74/busybox/commits/bugfixes/

+:git-shell:bugfixes:busybox> sw 62b5f147f
commit 62b5f147f27f7348cc52691c244ea037b4b8f2d6 (HEAD -> bugfixes,
origin/bugfixes)
Author: Roberto A. Foglietta <[email protected]>
Date:   Sun Apr 19 12:17:19 2026 +0200

    handle files with no final newline, p.2

    Code janitoring of the busybox code after the Ron's patch application.

    Ron's patch p.1 is a bugfix because busybox diff supports no-newline
    but patch didn't and this would have break the sha/md5sum check even
    when the busybox diff/patch would have used for applying a change.

    This patch p.2 is going to reorder the switch cases with the more
    frequent on the top and the no-newline after plus introduces the default
    case to keep the "continue" keyword out of the switch because the duo
    break/continue in a switch doesn't refer to the same nesting level. In
    this way the common "continue" statment happens at the end of the switch,
    correctly refering to that "if" branch the loop continuation.

    Finally, it sets the freed pointer to NULL as per defensive coding.

    Signed-off-by: Roberto A. Foglietta <[email protected]>

diff --git a/editors/patch.c b/editors/patch.c
index ee0ccd384..55244211e 100644
--- a/editors/patch.c
+++ b/editors/patch.c
@@ -436,13 +436,6 @@ int patch_main(int argc UNUSED_PARAM, char **argv)
                // Are we assembling a hunk?
                if (state >= 2) {
                        switch (*patchline) {
-                       case '\\':
-                               // '\ No newline at end of file' detected, mark
-                               // previous line, if it exists.
-                               if (TT.current_hunk->prev)
-
TT.current_hunk->prev->no_newline = TRUE;
-                               free(patchline);
-                               continue;
                        case ' ':
                        case '+':
                        case '-':
@@ -471,10 +464,17 @@ int patch_main(int argc UNUSED_PARAM, char **argv)
                                        }
                                        state = apply_one_hunk();
                                }
-                               continue;
-                       }
+                       case '\\':
+                               // '\ No newline at end of file' detected, mark
+                               // previous line, if it exists.
+                               if (TT.current_hunk->prev)
+
TT.current_hunk->prev->no_newline = TRUE;
+                               free(patchline);
+                               patchline = NULL;
+                       default:
                                fail_hunk();
                                state = 0;
+                       }
                        continue;
                }

________________________________________________________________________
files in commited:
editors/patch.c