Created attachment 6406 [details] fix-to-error-out-for-invalid-patch 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, got 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 )