When given an invalid patch, busybox reports zero exit code even if the patch 
is not applied.
With GNU patch, the behaviour is:

# patch < test.patch 
patch: **** Only garbage was found in the patch input.

With busybox patch, I get the following output:

busybox patch < test.patch 
[root@localhost ~]# echo $?
0

Input patch file contents:
==================

cat test.patch:

-- test.c.org   2015-07-15 11:27:51.100000000 +0000
++ test.c       2015-07-15 11:28:11.189000000 +0000
@@ -1,8 +1,8 @@
 #include <stdio.h>
 int main()
 {
-       int i;
-       for (int j ; j <10; j++)
+       int i,j;
+       for (j ; j <10; j++)
        {
                printf("j is %d\n", j);
        }


and test.c file contents:

# cat test.c 
#include <stdio.h>
int main()
{
        int i;
        for (int j ; j <10; j++)
        {
                printf("j is %d\n", j);
        }

}


busybox code loops through patchlines and check for +++, switches state when 
patches found. 

// state 0: Not in a hunk, look for +++.
// state 1: Found +++ file indicator, look for @@
// state 2: In hunk: counting initial context lines
// state 3: In hunk: getting body

state 2 implies code is getting into the hunk. 
Loop exits if patchline is empty

Adding a patch which has following changes:

- When state is 2, set patch_found to true ( implies a valid patch exists )
- when patchline is NULL,  ( i.e. when end of patch is reached ), set exitval 
to 1 if:
                patchfile size is non-zero ( that means some content is there 
in the patch file ) 
                and patch_found is false ( that means no valid patches were 
found )

Signed-off-by: Athira Rajeev<[email protected]>


diff --git a/editors/patch.c b/editors/patch.c
index 988021d..6bfdf19 100644
--- a/editors/patch.c
+++ b/editors/patch.c
@@ -352,11 +352,13 @@ int patch_main(int argc, char **argv) 
MAIN_EXTERNALLY_VISIBLE;
 int patch_main(int argc UNUSED_PARAM, char **argv)
 {
        int opts;
-       int reverse, state = 0;
+       int reverse, size, state = 0;
+       bool patch_found = false;
        char *oldname = NULL, *newname = NULL;
        char *opt_p, *opt_i;
        long oldlen = oldlen; /* for compiler */
        long newlen = newlen; /* for compiler */
+       struct stat buf;
 
        INIT_TT();
 
@@ -373,12 +375,22 @@ int patch_main(int argc UNUSED_PARAM, char **argv)
                }
        }
 
+       // Get the file size using fstat
+       fstat(STDIN_FILENO, &buf);
+       size = buf.st_size;
+
        // Loop through the lines in the patch
        for(;;) {
                char *patchline;
 
                patchline = xmalloc_fgetline(stdin);
-               if (!patchline) break;
+               if (!patchline) {
+                       if (size !=0 && !patch_found) {
+                               printf("Couldnt find a valid patch. \n");
+                               TT.exitval = 1;
+                       }
+                       break;
+               }       
 
                // Other versions of patch accept damaged patches,
                // so we need to also.
@@ -460,6 +472,9 @@ int patch_main(int argc UNUSED_PARAM, char **argv)
                        TT.context = 0;
                        state = 2;
 
+                       // In hunk, found unified diff, set patch_found
+                       patch_found = true;
+
                        // If the --- line is missing or malformed, either 
oldname
                        // or (for -R) newname could be NULL -- but not both.  
Like
                        // GNU patch, proceed based on the +++ line, and avoid 
SEGVs.




_______________________________________________
busybox mailing list
[email protected]
http://lists.busybox.net/mailman/listinfo/busybox

Reply via email to