# [PATCH v2] builtin/apply: exit when parse_binary() fails

2 messages from 2016-03-17 to 2016-03-17. Participants: Christian Couder.
Thread: https://gitlist.dev/t/41724

## Christian Couder, 2016-03-17 09:23

Subject: [PATCH v2] builtin/apply: exit when parse_binary() fails
Message-ID: <1458206614-2898-1-git-send-email-chriscool@tuxfamily.org>
URL: https://gitlist.dev/e/1458206614-2898-1-git-send-email-chriscool%40tuxfamily.org

```
In parse_binary() there is:

	forward = parse_binary_hunk(&buffer, &size, &status, &used);
	if (!forward && !status)
		/* there has to be one hunk (forward hunk) */
		return error(_("unrecognized binary patch at line %d"), linenr-1);

so parse_binary() can return -1, because that's what error() returns.

Also parse_binary_hunk() sets "status" to -1 in case of error and
parse_binary() does "if (status) return status;".

In this case parse_chunk() should not add -1 to the patchsize it computes.
It is better for future libification efforts to make it just return -1.

Signed-off-by: Christian Couder <chriscool@tuxfamily.org>
---
 builtin/apply.c | 7 +++++++
 1 file changed, 7 insertions(+)

diff --git a/builtin/apply.c b/builtin/apply.c
index 42c610e..c399c97 100644
--- a/builtin/apply.c
+++ b/builtin/apply.c
@@ -1872,6 +1872,11 @@ static struct fragment *parse_binary_hunk(char **buf_p,
 	return NULL;
 }
 
+/*
+ * Returns:
+ *   -1 in case of error,
+ *   the length of the parsed binary patch otherwise
+ */
 static int parse_binary(char *buffer, unsigned long size, struct patch *patch)
 {
 	/*
@@ -2017,6 +2022,8 @@ static int parse_chunk(char *buffer, unsigned long size, struct patch *patch)
 			linenr++;
 			used = parse_binary(buffer + hd + llen,
 					    size - hd - llen, patch);
+			if (used < 0)
+				return -1;
 			if (used)
 				patchsize = used + llen;
 			else
-- 
2.8.0.rc2.1.g21d258f

```

## Christian Couder, 2016-03-17 09:34

Subject: Re: [PATCH v2] builtin/apply: exit when parse_binary() fails
Message-ID: <CAP8UFD2NgSfVVZrshimM+=7dFmhjqGmUVsTymgoQOXvhegr_1g@mail.gmail.com>
URL: https://gitlist.dev/e/CAP8UFD2NgSfVVZrshimM%2B%3D7dFmhjqGmUVsTymgoQOXvhegr_1g%40mail.gmail.com
In-Reply-To: <1458206614-2898-1-git-send-email-chriscool@tuxfamily.org>

```
On Thu, Mar 17, 2016 at 10:23 AM, Christian Couder
<christian.couder@gmail.com> wrote:
> In parse_binary() there is:
...

Of course just after sending this I realized that I should probably
change the title of the patch to something like "builtin/apply: handle
parse_binary() failure".

I will resend a v3 with the above change later...

```
