builtin/apply: make parse_chunk() return a negative integer on error

To libify `git apply` functionality we have to signal errors to the caller instead of die()ing or exit()ing. To do that in a compatible manner with the rest of the error handling in builtin/apply.c, parse_chunk() should return a negative integer instead of calling die() or exit(). As parse_chunk() is called only by apply_patch() which already returns either -1 or -128 when an error happened, let's make it also return -1 or -128. This makes it compatible with what find_header() and parse_binary() already return. Helped-by: Eric Sunshine <sunshine@sunshineco.com> Signed-off-by: Christian Couder <chriscool@tuxfamily.org> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Christian Couder committed Aug 8, 2016 at 23:03 UTC b654b34c1cf877709febb602991f46e7ba0d947d
1 file changed +14 -8
builtin/apply.c
+14 -8
@@ -1996,22 +1996,22 @@ static int use_patch(struct apply_state *state, struct patch *p)
1996 return !state->has_include;
1997 }
1998
1999 -
1999 /*
2000 * Read the patch text in "buffer" that extends for "size" bytes; stop
2001 * reading after seeing a single patch (i.e. changes to a single file).
2002 * Create fragments (i.e. patch hunks) and hang them to the given patch.
2004 - * Return the number of bytes consumed, so that the caller can call us
2005 - * again for the next patch.
2003 + *
2004 + * Returns:
2005 + * -1 if no header was found or parse_binary() failed,
2006 + * -128 on another error,
2007 + * the number of bytes consumed otherwise,
2008 + * so that the caller can call us again for the next patch.
2009 */
2010 static int parse_chunk(struct apply_state *state, char *buffer, unsigned long size, struct patch *patch)
2011 {
2012 int hdrsize, patchsize;
2013 int offset = find_header(state, buffer, size, &hdrsize, patch);
2014
2012 - if (offset == -128)
2013 - exit(128);
2014 -
2015 if (offset < 0)
2016 return offset;
2017
@@ -2071,8 +2071,10 @@ static int parse_chunk(struct apply_state *state, char *buffer, unsigned long si
2071 * empty to us here.
2072 */
2073 if ((state->apply || state->check) &&
2074 - (!patch->is_binary && !metadata_changes(patch)))
2075 - die(_("patch with only garbage at line %d"), state->linenr);
2074 + (!patch->is_binary && !metadata_changes(patch))) {
2075 + error(_("patch with only garbage at line %d"), state->linenr);
2076 + return -128;
2077 + }
2078 }
2079
2080 return offset + hdrsize + patchsize;
@@ -4455,6 +4457,10 @@ static int apply_patch(struct apply_state *state,
4457 nr = parse_chunk(state, buf.buf + offset, buf.len - offset, patch);
4458 if (nr < 0) {
4459 free_patch(patch);
4460 + if (nr == -128) {
4461 + res = -128;
4462 + goto end;
4463 + }
4464 break;
4465 }
4466 if (state->apply_in_reverse)