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

"Roberto A. Foglietta via busybox" <[email protected]> Sun, 19 Apr 2026 11:58:30 +0200
Newsgroups gmane.linux.busybox
Message-ID <CAJGKYO4j1zo21S2HysF2GR6ZcgOPJzqsx_fFKP=sPq+Dx5Vx2w@mail.gmail.com>
On Sun, 19 Apr 2026 at 11:04, Ron Yorston via busybox
<[email protected]> wrote:
>
> GNU and BSD patch both handle patches which include the annotation
> '\ No newline at end of file'.  BusyBox patch doesn't, even though
> its diff emits it.
>

This patch is worth applying because sha/md5sum would break otherwise
compared to the expected result of a patched file. In particular,
because busybox diff/patch duo aren't currently aligned on this
feature. Therefore, it can be considered also a bugfix rather than a
mere feature addition.

Did not check the whole patch yet, but this below caught my eyes.

> @@ -433,7 +435,17 @@ int patch_main(int argc UNUSED_PARAM, char **argv)
>
>                 // Are we assembling a hunk?
>                 if (state >= 2) {
> -                       if (*patchline == ' ' || *patchline == '+' || *patchline == '-') {
> +                       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;

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.

Best regards, R-