Bug 8826 - busybox reports zero exit code for invalid patch file
Summary: busybox reports zero exit code for invalid patch file
Status: NEW
Alias: None
Product: Busybox
Classification: Unclassified
Component: Other (show other bugs)
Version: 1.24.x
Hardware: PC Linux
: P5 normal
Target Milestone: ---
Assignee: unassigned
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2016-03-29 08:39 UTC by Athira Rajeev
Modified: 2016-04-05 10:30 UTC (History)
1 user (show)

See Also:
Host:
Target:
Build:


Attachments
fix-to-error-out-for-invalid-patch (1.43 KB, patch)
2016-03-29 08:39 UTC, Athira Rajeev
Details

Note You need to log in before you can comment on or make changes to this bug.
Description Athira Rajeev 2016-03-29 08:39:58 UTC
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 )