]> git.proxmox.com Git - libgit2.git/blobdiff - src/patch_generate.c
New upstream version 1.4.3+dfsg.1
[libgit2.git] / src / patch_generate.c
index 804fc0e09368050075ab293f8cc08f673f1cd33f..bc598fea870123b5e771c6cd53e8000918cc6cc6 100644 (file)
@@ -4,17 +4,18 @@
  * This file is part of libgit2, distributed under the GNU GPL v2 with
  * a Linking Exception. For full terms see the included COPYING file.
  */
-#include "common.h"
+
+#include "patch_generate.h"
+
 #include "git2/blob.h"
 #include "diff.h"
 #include "diff_generate.h"
 #include "diff_file.h"
 #include "diff_driver.h"
-#include "patch_generate.h"
 #include "diff_xdiff.h"
 #include "delta.h"
 #include "zstream.h"
-#include "fileops.h"
+#include "futils.h"
 
 static void diff_output_init(
        git_patch_generated_output *, const git_diff_options *, git_diff_file_cb,
@@ -83,7 +84,7 @@ static int patch_generated_normalize_options(
        const git_diff_options *opts)
 {
        if (opts) {
-               GITERR_CHECK_VERSION(opts, GIT_DIFF_OPTIONS_VERSION, "git_diff_options");
+               GIT_ERROR_CHECK_VERSION(opts, GIT_DIFF_OPTIONS_VERSION, "git_diff_options");
                memcpy(out, opts, sizeof(git_diff_options));
        } else {
                git_diff_options default_opts = GIT_DIFF_OPTIONS_INIT;
@@ -98,8 +99,8 @@ static int patch_generated_normalize_options(
                git__strdup(opts->new_prefix) :
                git__strdup(DIFF_NEW_PREFIX_DEFAULT);
 
-       GITERR_CHECK_ALLOC(out->old_prefix);
-       GITERR_CHECK_ALLOC(out->new_prefix);
+       GIT_ERROR_CHECK_ALLOC(out->old_prefix);
+       GIT_ERROR_CHECK_ALLOC(out->new_prefix);
 
        return 0;
 }
@@ -134,11 +135,11 @@ static int patch_generated_alloc_from_diff(
 {
        int error;
        git_patch_generated *patch = git__calloc(1, sizeof(git_patch_generated));
-       GITERR_CHECK_ALLOC(patch);
+       GIT_ERROR_CHECK_ALLOC(patch);
 
        if (!(error = patch_generated_init(patch, diff, delta_index))) {
                patch->flags |= GIT_PATCH_GENERATED_ALLOCATED;
-               GIT_REFCOUNT_INC(patch);
+               GIT_REFCOUNT_INC(&patch->base);
        } else {
                git__free(patch);
                patch = NULL;
@@ -208,9 +209,7 @@ static int patch_generated_load(git_patch_generated *patch, git_patch_generated_
 
        if ((error = git_diff_file_content__load(
                        &patch->ofile, &patch->base.diff_opts)) < 0 ||
-               should_skip_binary(patch, patch->ofile.file))
-               goto cleanup;
-       if ((error = git_diff_file_content__load(
+           (error = git_diff_file_content__load(
                        &patch->nfile, &patch->base.diff_opts)) < 0 ||
                should_skip_binary(patch, patch->nfile.file))
                goto cleanup;
@@ -247,7 +246,7 @@ static int patch_generated_invoke_file_callback(
        if (!output->file_cb)
                return 0;
 
-       return giterr_set_after_callback_function(
+       return git_error_set_after_callback_function(
                output->file_cb(patch->base.delta, progress, output->payload),
                "git_patch");
 }
@@ -262,7 +261,7 @@ static int create_binary(
        const char *b_data,
        size_t b_datalen)
 {
-       git_buf deflate = GIT_BUF_INIT, delta = GIT_BUF_INIT;
+       git_str deflate = GIT_STR_INIT, delta = GIT_STR_INIT;
        size_t delta_data_len = 0;
        int error;
 
@@ -303,18 +302,18 @@ static int create_binary(
        if (delta.size && delta.size < deflate.size) {
                *out_type = GIT_DIFF_BINARY_DELTA;
                *out_datalen = delta.size;
-               *out_data = git_buf_detach(&delta);
+               *out_data = git_str_detach(&delta);
                *out_inflatedlen = delta_data_len;
        } else {
                *out_type = GIT_DIFF_BINARY_LITERAL;
                *out_datalen = deflate.size;
-               *out_data = git_buf_detach(&deflate);
+               *out_data = git_str_detach(&deflate);
                *out_inflatedlen = b_datalen;
        }
 
 done:
-       git_buf_free(&deflate);
-       git_buf_free(&delta);
+       git_str_dispose(&deflate);
+       git_str_dispose(&delta);
 
        return error;
 }
@@ -349,7 +348,7 @@ static int diff_binary(git_patch_generated_output *output, git_patch_generated *
                        return error;
        }
 
-       error = giterr_set_after_callback_function(
+       error = git_error_set_after_callback_function(
                output->binary_cb(patch->base.delta, &binary, output->payload),
                "git_patch");
 
@@ -396,7 +395,7 @@ static int diff_required(git_diff *diff, const char *action)
 {
        if (diff)
                return 0;
-       giterr_set(GITERR_INVALID, "must provide valid diff to %s", action);
+       git_error_set(GIT_ERROR_INVALID, "must provide valid diff to %s", action);
        return -1;
 }
 
@@ -500,12 +499,12 @@ static int patch_generated_with_delta_alloc(
        size_t new_len = *new_path ? strlen(*new_path) : 0;
        size_t alloc_len;
 
-       GITERR_CHECK_ALLOC_ADD(&alloc_len, sizeof(*pd), old_len);
-       GITERR_CHECK_ALLOC_ADD(&alloc_len, alloc_len, new_len);
-       GITERR_CHECK_ALLOC_ADD(&alloc_len, alloc_len, 2);
+       GIT_ERROR_CHECK_ALLOC_ADD(&alloc_len, sizeof(*pd), old_len);
+       GIT_ERROR_CHECK_ALLOC_ADD(&alloc_len, alloc_len, new_len);
+       GIT_ERROR_CHECK_ALLOC_ADD(&alloc_len, alloc_len, 2);
 
        *out = pd = git__calloc(1, alloc_len);
-       GITERR_CHECK_ALLOC(pd);
+       GIT_ERROR_CHECK_ALLOC(pd);
 
        pd->patch.flags = GIT_PATCH_GENERATED_ALLOCATED;
 
@@ -562,7 +561,7 @@ static int patch_from_sources(
        patch_generated_with_delta *pd;
        git_xdiff_output xo;
 
-       assert(out);
+       GIT_ASSERT_ARG(out);
        *out = NULL;
 
        if ((error = patch_generated_with_delta_alloc(
@@ -641,7 +640,7 @@ int git_patch_from_blob_and_buffer(
        git_patch **out,
        const git_blob *old_blob,
        const char *old_path,
-       const char *buf,
+       const void *buf,
        size_t buflen,
        const char *buf_path,
        const git_diff_options *opts)
@@ -680,7 +679,7 @@ int git_patch_from_buffers(
        const void *old_buf,
        size_t old_len,
        const char *old_path,
-       const char *new_buf,
+       const void *new_buf,
        size_t new_len,
        const char *new_path,
        const git_diff_options *opts)
@@ -707,7 +706,7 @@ int git_patch_generated_from_diff(
 
        delta = git_vector_get(&diff->deltas, idx);
        if (!delta) {
-               giterr_set(GITERR_INVALID, "index out of range for delta in diff");
+               git_error_set(GIT_ERROR_INVALID, "index out of range for delta in diff");
                return GIT_ENOTFOUND;
        }
 
@@ -751,18 +750,34 @@ git_diff_driver *git_patch_generated_driver(git_patch_generated *patch)
        return patch->ofile.driver;
 }
 
-void git_patch_generated_old_data(
-       char **ptr, size_t *len, git_patch_generated *patch)
+int git_patch_generated_old_data(
+       char **ptr, long *len, git_patch_generated *patch)
 {
+       if (patch->ofile.map.len > LONG_MAX ||
+           patch->ofile.map.len > GIT_XDIFF_MAX_SIZE) {
+               git_error_set(GIT_ERROR_INVALID, "files too large for diff");
+               return -1;
+       }
+
        *ptr = patch->ofile.map.data;
-       *len = patch->ofile.map.len;
+       *len = (long)patch->ofile.map.len;
+
+       return 0;
 }
 
-void git_patch_generated_new_data(
-       char **ptr, size_t *len, git_patch_generated *patch)
+int git_patch_generated_new_data(
+       char **ptr, long *len, git_patch_generated *patch)
 {
+       if (patch->ofile.map.len > LONG_MAX ||
+           patch->ofile.map.len > GIT_XDIFF_MAX_SIZE) {
+               git_error_set(GIT_ERROR_INVALID, "files too large for diff");
+               return -1;
+       }
+
        *ptr = patch->nfile.map.data;
-       *len = patch->nfile.map.len;
+       *len = (long)patch->nfile.map.len;
+
+       return 0;
 }
 
 static int patch_generated_file_cb(
@@ -787,7 +802,7 @@ static int patch_generated_binary_cb(
 
        if (binary->old_file.data) {
                patch->binary.old_file.data = git__malloc(binary->old_file.datalen);
-               GITERR_CHECK_ALLOC(patch->binary.old_file.data);
+               GIT_ERROR_CHECK_ALLOC(patch->binary.old_file.data);
 
                memcpy((char *)patch->binary.old_file.data,
                        binary->old_file.data, binary->old_file.datalen);
@@ -795,7 +810,7 @@ static int patch_generated_binary_cb(
 
        if (binary->new_file.data) {
                patch->binary.new_file.data = git__malloc(binary->new_file.datalen);
-               GITERR_CHECK_ALLOC(patch->binary.new_file.data);
+               GIT_ERROR_CHECK_ALLOC(patch->binary.new_file.data);
 
                memcpy((char *)patch->binary.new_file.data,
                        binary->new_file.data, binary->new_file.datalen);
@@ -815,7 +830,7 @@ static int git_patch_hunk_cb(
        GIT_UNUSED(delta);
 
        hunk = git_array_alloc(patch->base.hunks);
-       GITERR_CHECK_ALLOC(hunk);
+       GIT_ERROR_CHECK_ALLOC(hunk);
 
        memcpy(&hunk->hunk, hunk_, sizeof(hunk->hunk));
 
@@ -835,16 +850,16 @@ static int patch_generated_line_cb(
 {
        git_patch_generated  *patch = payload;
        git_patch_hunk *hunk;
-       git_diff_line   *line;
+       git_diff_line *line;
 
        GIT_UNUSED(delta);
        GIT_UNUSED(hunk_);
 
        hunk = git_array_last(patch->base.hunks);
-       assert(hunk); /* programmer error if no hunk is available */
+       GIT_ASSERT(hunk); /* programmer error if no hunk is available */
 
        line = git_array_alloc(patch->base.lines);
-       GITERR_CHECK_ALLOC(line);
+       GIT_ERROR_CHECK_ALLOC(line);
 
        memcpy(line, line_, sizeof(*line));