builtin/apply: make parse_single_patch() return -1 on error

To libify `git apply` functionality we have to signal errors to the caller instead of die()ing. To do that in a compatible manner with the rest of the error handling in builtin/apply.c, parse_single_patch() should return a negative integer instead of calling die(). Let's do that by using error() and let's adjust the related test cases accordingly. 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 dae197f753c8b3ccdc9c97cfc04f0dbd99a5cc3c
2 files changed +15 -6
builtin/apply.c
+13 -4
@@ -1671,6 +1671,10 @@ static int parse_fragment(struct apply_state *state,
1671 *
1672 * The (fragment->patch, fragment->size) pair points into the memory given
1673 * by the caller, not a copy, when we return.
1674 + *
1675 + * Returns:
1676 + * -1 in case of error,
1677 + * the number of bytes in the patch otherwise.
1678 */
1679 static int parse_single_patch(struct apply_state *state,
1680 const char *line,
@@ -1688,8 +1692,10 @@ static int parse_single_patch(struct apply_state *state,
1692 fragment = xcalloc(1, sizeof(*fragment));
1693 fragment->linenr = state->linenr;
1694 len = parse_fragment(state, line, size, patch, fragment);
1691 - if (len <= 0)
1692 - die(_("corrupt patch at line %d"), state->linenr);
1695 + if (len <= 0) {
1696 + free(fragment);
1697 + return error(_("corrupt patch at line %d"), state->linenr);
1698 + }
1699 fragment->patch = line;
1700 fragment->size = len;
1701 oldlines += fragment->oldlines;
@@ -1725,9 +1731,9 @@ static int parse_single_patch(struct apply_state *state,
1731 patch->is_delete = 0;
1732
1733 if (0 < patch->is_new && oldlines)
1728 - die(_("new file %s depends on old contents"), patch->new_name);
1734 + return error(_("new file %s depends on old contents"), patch->new_name);
1735 if (0 < patch->is_delete && newlines)
1730 - die(_("deleted file %s still has contents"), patch->old_name);
1736 + return error(_("deleted file %s still has contents"), patch->old_name);
1737 if (!patch->is_delete && !newlines && context)
1738 fprintf_ln(stderr,
1739 _("** warning: "
@@ -2029,6 +2035,9 @@ static int parse_chunk(struct apply_state *state, char *buffer, unsigned long si
2035 size - offset - hdrsize,
2036 patch);
2037
2038 + if (patchsize < 0)
2039 + return -128;
2040 +
2041 if (!patchsize) {
2042 static const char git_binary[] = "GIT binary patch\n";
2043 int hd = hdrsize + offset;
t/t4012-diff-binary.sh
+2 -2
@@ -68,7 +68,7 @@ test_expect_success C_LOCALE_OUTPUT 'apply detecting corrupt patch correctly' '
68 sed -e "s/-CIT/xCIT/" <output >broken &&
69 test_must_fail git apply --stat --summary broken 2>detected &&
70 detected=$(cat detected) &&
71 - detected=$(expr "$detected" : "fatal.*at line \\([0-9]*\\)\$") &&
71 + detected=$(expr "$detected" : "error.*at line \\([0-9]*\\)\$") &&
72 detected=$(sed -ne "${detected}p" broken) &&
73 test "$detected" = xCIT
74 '
@@ -77,7 +77,7 @@ test_expect_success C_LOCALE_OUTPUT 'apply detecting corrupt patch correctly' '
77 git diff --binary | sed -e "s/-CIT/xCIT/" >broken &&
78 test_must_fail git apply --stat --summary broken 2>detected &&
79 detected=$(cat detected) &&
80 - detected=$(expr "$detected" : "fatal.*at line \\([0-9]*\\)\$") &&
80 + detected=$(expr "$detected" : "error.*at line \\([0-9]*\\)\$") &&
81 detected=$(sed -ne "${detected}p" broken) &&
82 test "$detected" = xCIT
83 '