builtin/apply: make parse_single_patch() return -1 on error
[gitweb.git] / builtin / apply.c
index c07d142be8e82eec8ae65375d6e4219e196f6897..10aaba77255bbf6991582a96e0a58a25e4bad1e3 100644 (file)
@@ -1671,6 +1671,10 @@ static int parse_fragment(struct apply_state *state,
  *
  * The (fragment->patch, fragment->size) pair points into the memory given
  * by the caller, not a copy, when we return.
+ *
+ * Returns:
+ *   -1 in case of error,
+ *   the number of bytes in the patch otherwise.
  */
 static int parse_single_patch(struct apply_state *state,
                              const char *line,
@@ -1688,8 +1692,10 @@ static int parse_single_patch(struct apply_state *state,
                fragment = xcalloc(1, sizeof(*fragment));
                fragment->linenr = state->linenr;
                len = parse_fragment(state, line, size, patch, fragment);
-               if (len <= 0)
-                       die(_("corrupt patch at line %d"), state->linenr);
+               if (len <= 0) {
+                       free(fragment);
+                       return error(_("corrupt patch at line %d"), state->linenr);
+               }
                fragment->patch = line;
                fragment->size = len;
                oldlines += fragment->oldlines;
@@ -1725,9 +1731,9 @@ static int parse_single_patch(struct apply_state *state,
                patch->is_delete = 0;
 
        if (0 < patch->is_new && oldlines)
-               die(_("new file %s depends on old contents"), patch->new_name);
+               return error(_("new file %s depends on old contents"), patch->new_name);
        if (0 < patch->is_delete && newlines)
-               die(_("deleted file %s still has contents"), patch->old_name);
+               return error(_("deleted file %s still has contents"), patch->old_name);
        if (!patch->is_delete && !newlines && context)
                fprintf_ln(stderr,
                           _("** warning: "
@@ -2029,6 +2035,9 @@ static int parse_chunk(struct apply_state *state, char *buffer, unsigned long si
                                       size - offset - hdrsize,
                                       patch);
 
+       if (patchsize < 0)
+               return -128;
+
        if (!patchsize) {
                static const char git_binary[] = "GIT binary patch\n";
                int hd = hdrsize + offset;