Skip to content

Commit

Permalink
diff: don't feed large files to xdiff
Browse files Browse the repository at this point in the history
  • Loading branch information
Edward Thomson committed Oct 1, 2015
1 parent 8f168e8 commit 327eddf
Show file tree
Hide file tree
Showing 8 changed files with 22 additions and 20 deletions.
2 changes: 1 addition & 1 deletion src/checkout.c
Expand Up @@ -27,7 +27,7 @@
#include "diff.h"
#include "pathspec.h"
#include "buf_text.h"
#include "merge_file.h"
#include "diff_xdiff.h"
#include "path.h"
#include "attr.h"
#include "pool.h"
Expand Down
4 changes: 4 additions & 0 deletions src/diff_patch.c
Expand Up @@ -30,6 +30,10 @@ static void diff_patch_update_binary(git_patch *patch)
(patch->nfile.file->flags & GIT_DIFF_FLAG_BINARY) != 0)
patch->delta->flags |= GIT_DIFF_FLAG_BINARY;

else if (patch->ofile.file->size > GIT_XDIFF_MAX_SIZE ||
patch->nfile.file->size > GIT_XDIFF_MAX_SIZE)
patch->delta->flags |= GIT_DIFF_FLAG_BINARY;

else if ((patch->ofile.file->flags & DIFF_FLAGS_NOT_BINARY) != 0 &&
(patch->nfile.file->flags & DIFF_FLAGS_NOT_BINARY) != 0)
patch->delta->flags |= GIT_DIFF_FLAG_NOT_BINARY;
Expand Down
7 changes: 7 additions & 0 deletions src/diff_xdiff.c
Expand Up @@ -4,6 +4,7 @@
* 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 "git2/errors.h"
#include "common.h"
#include "diff.h"
#include "diff_driver.h"
Expand Down Expand Up @@ -208,6 +209,12 @@ static int git_xdiff(git_diff_output *output, git_patch *patch)
git_patch__old_data(&info.xd_old_data.ptr, &info.xd_old_data.size, patch);
git_patch__new_data(&info.xd_new_data.ptr, &info.xd_new_data.size, patch);

if (info.xd_old_data.size > GIT_XDIFF_MAX_SIZE ||
info.xd_new_data.size > GIT_XDIFF_MAX_SIZE) {
giterr_set(GITERR_INVALID, "files too large for diff");
return -1;
}

xdl_diff(&info.xd_old_data, &info.xd_new_data,
&xo->params, &xo->config, &xo->callback);

Expand Down
5 changes: 5 additions & 0 deletions src/diff_xdiff.h
Expand Up @@ -11,6 +11,11 @@
#include "diff_patch.h"
#include "xdiff/xdiff.h"

/* xdiff cannot cope with large files. these files should not be passed to
* xdiff. callers should treat these large files as binary.
*/
#define GIT_XDIFF_MAX_SIZE (1024UL * 1024 * 1023)

/* A git_xdiff_output is a git_diff_output with extra fields necessary
* to use libxdiff. Calling git_xdiff_init() will set the diff_cb field
* of the output to use xdiff to generate the diffs.
Expand Down
1 change: 0 additions & 1 deletion src/merge.c
Expand Up @@ -20,7 +20,6 @@
#include "diff.h"
#include "checkout.h"
#include "tree.h"
#include "merge_file.h"
#include "blob.h"
#include "oid.h"
#include "index.h"
Expand Down
4 changes: 2 additions & 2 deletions src/merge_file.c
Expand Up @@ -7,10 +7,10 @@

#include "common.h"
#include "repository.h"
#include "merge_file.h"
#include "posix.h"
#include "fileops.h"
#include "index.h"
#include "diff_xdiff.h"

#include "git2/repository.h"
#include "git2/object.h"
Expand Down Expand Up @@ -199,7 +199,7 @@ static bool merge_file__is_binary(const git_merge_file_input *file)
{
size_t len = file ? file->size : 0;

if (len > GIT_MERGE_FILE_XDIFF_MAX)
if (len > GIT_XDIFF_MAX_SIZE)
return true;
if (len > GIT_MERGE_FILE_BINARY_SIZE)
len = GIT_MERGE_FILE_BINARY_SIZE;
Expand Down
13 changes: 0 additions & 13 deletions src/merge_file.h

This file was deleted.

6 changes: 3 additions & 3 deletions tests/merge/files.c
Expand Up @@ -3,10 +3,10 @@
#include "git2/merge.h"
#include "buffer.h"
#include "merge.h"
#include "merge_file.h"
#include "merge_helpers.h"
#include "refs.h"
#include "fileops.h"
#include "diff_xdiff.h"

#define TEST_REPO_PATH "merge-resolve"
#define TEST_INDEX_PATH TEST_REPO_PATH "/.git/index"
Expand Down Expand Up @@ -296,11 +296,11 @@ void test_merge_files__skips_large_files(void)
git_merge_file_options opts = GIT_MERGE_FILE_OPTIONS_INIT;
git_merge_file_result result = {0};

ours.size = GIT_MERGE_FILE_XDIFF_MAX + 1;
ours.size = GIT_XDIFF_MAX_SIZE + 1;
ours.path = "testfile.txt";
ours.mode = 0100755;

theirs.size = GIT_MERGE_FILE_XDIFF_MAX + 1;
theirs.size = GIT_XDIFF_MAX_SIZE + 1;
theirs.path = "testfile.txt";
theirs.mode = 0100755;

Expand Down

0 comments on commit 327eddf

Please sign in to comment.