Skip to content

Commit b654b34

Browse files
chriscoolgitster
authored andcommitted
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>
1 parent 5950851 commit b654b34

File tree

1 file changed

+14
-8
lines changed

1 file changed

+14
-8
lines changed

builtin/apply.c

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1996,22 +1996,22 @@ static int use_patch(struct apply_state *state, struct patch *p)
19961996
return !state->has_include;
19971997
}
19981998

1999-
20001999
/*
20012000
* Read the patch text in "buffer" that extends for "size" bytes; stop
20022001
* reading after seeing a single patch (i.e. changes to a single file).
20032002
* 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.
20062009
*/
20072010
static int parse_chunk(struct apply_state *state, char *buffer, unsigned long size, struct patch *patch)
20082011
{
20092012
int hdrsize, patchsize;
20102013
int offset = find_header(state, buffer, size, &hdrsize, patch);
20112014

2012-
if (offset == -128)
2013-
exit(128);
2014-
20152015
if (offset < 0)
20162016
return offset;
20172017

@@ -2071,8 +2071,10 @@ static int parse_chunk(struct apply_state *state, char *buffer, unsigned long si
20712071
* empty to us here.
20722072
*/
20732073
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+
}
20762078
}
20772079

20782080
return offset + hdrsize + patchsize;
@@ -4455,6 +4457,10 @@ static int apply_patch(struct apply_state *state,
44554457
nr = parse_chunk(state, buf.buf + offset, buf.len - offset, patch);
44564458
if (nr < 0) {
44574459
free_patch(patch);
4460+
if (nr == -128) {
4461+
res = -128;
4462+
goto end;
4463+
}
44584464
break;
44594465
}
44604466
if (state->apply_in_reverse)

0 commit comments

Comments
 (0)