Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 61c6f44

Browse files
Edward Thomsonethomson
Edward Thomson
authored andcommitted
diff: don't feed large files to xdiff
1 parent 7eb2626 commit 61c6f44

File tree

8 files changed

+23
-20
lines changed

8 files changed

+23
-20
lines changed

src/checkout.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include "git2/submodule.h"
1919
#include "git2/sys/index.h"
2020
#include "git2/sys/filter.h"
21+
#include "git2/merge.h"
2122

2223
#include "refs.h"
2324
#include "repository.h"
@@ -27,7 +28,7 @@
2728
#include "diff.h"
2829
#include "pathspec.h"
2930
#include "buf_text.h"
30-
#include "merge_file.h"
31+
#include "diff_xdiff.h"
3132
#include "path.h"
3233
#include "attr.h"
3334
#include "pool.h"

src/diff_patch.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ static void diff_patch_update_binary(git_patch *patch)
3030
(patch->nfile.file->flags & GIT_DIFF_FLAG_BINARY) != 0)
3131
patch->delta->flags |= GIT_DIFF_FLAG_BINARY;
3232

33+
else if (patch->ofile.file->size > GIT_XDIFF_MAX_SIZE ||
34+
patch->nfile.file->size > GIT_XDIFF_MAX_SIZE)
35+
patch->delta->flags |= GIT_DIFF_FLAG_BINARY;
36+
3337
else if ((patch->ofile.file->flags & DIFF_FLAGS_NOT_BINARY) != 0 &&
3438
(patch->nfile.file->flags & DIFF_FLAGS_NOT_BINARY) != 0)
3539
patch->delta->flags |= GIT_DIFF_FLAG_NOT_BINARY;

src/diff_xdiff.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
* This file is part of libgit2, distributed under the GNU GPL v2 with
55
* a Linking Exception. For full terms see the included COPYING file.
66
*/
7+
#include "git2/errors.h"
78
#include "common.h"
89
#include "diff.h"
910
#include "diff_driver.h"
@@ -208,6 +209,12 @@ static int git_xdiff(git_diff_output *output, git_patch *patch)
208209
git_patch__old_data(&info.xd_old_data.ptr, &info.xd_old_data.size, patch);
209210
git_patch__new_data(&info.xd_new_data.ptr, &info.xd_new_data.size, patch);
210211

212+
if (info.xd_old_data.size > GIT_XDIFF_MAX_SIZE ||
213+
info.xd_new_data.size > GIT_XDIFF_MAX_SIZE) {
214+
giterr_set(GITERR_INVALID, "files too large for diff");
215+
return -1;
216+
}
217+
211218
xdl_diff(&info.xd_old_data, &info.xd_new_data,
212219
&xo->params, &xo->config, &xo->callback);
213220

src/diff_xdiff.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@
1111
#include "diff_patch.h"
1212
#include "xdiff/xdiff.h"
1313

14+
/* xdiff cannot cope with large files. these files should not be passed to
15+
* xdiff. callers should treat these large files as binary.
16+
*/
17+
#define GIT_XDIFF_MAX_SIZE (1024LL * 1024 * 1023)
18+
1419
/* A git_xdiff_output is a git_diff_output with extra fields necessary
1520
* to use libxdiff. Calling git_xdiff_init() will set the diff_cb field
1621
* of the output to use xdiff to generate the diffs.

src/merge.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
#include "diff.h"
2121
#include "checkout.h"
2222
#include "tree.h"
23-
#include "merge_file.h"
2423
#include "blob.h"
2524
#include "oid.h"
2625
#include "index.h"

src/merge_file.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@
77

88
#include "common.h"
99
#include "repository.h"
10-
#include "merge_file.h"
1110
#include "posix.h"
1211
#include "fileops.h"
1312
#include "index.h"
13+
#include "diff_xdiff.h"
1414

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

202-
if (len > GIT_MERGE_FILE_XDIFF_MAX)
202+
if (len > GIT_XDIFF_MAX_SIZE)
203203
return true;
204204
if (len > GIT_MERGE_FILE_BINARY_SIZE)
205205
len = GIT_MERGE_FILE_BINARY_SIZE;

src/merge_file.h

Lines changed: 0 additions & 13 deletions
This file was deleted.

tests/merge/files.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33
#include "git2/merge.h"
44
#include "buffer.h"
55
#include "merge.h"
6-
#include "merge_file.h"
76
#include "merge_helpers.h"
87
#include "refs.h"
98
#include "fileops.h"
9+
#include "diff_xdiff.h"
1010

1111
#define TEST_REPO_PATH "merge-resolve"
1212
#define TEST_INDEX_PATH TEST_REPO_PATH "/.git/index"
@@ -296,11 +296,11 @@ void test_merge_files__skips_large_files(void)
296296
git_merge_file_options opts = GIT_MERGE_FILE_OPTIONS_INIT;
297297
git_merge_file_result result = {0};
298298

299-
ours.size = GIT_MERGE_FILE_XDIFF_MAX + 1;
299+
ours.size = GIT_XDIFF_MAX_SIZE + 1;
300300
ours.path = "testfile.txt";
301301
ours.mode = 0100755;
302302

303-
theirs.size = GIT_MERGE_FILE_XDIFF_MAX + 1;
303+
theirs.size = GIT_XDIFF_MAX_SIZE + 1;
304304
theirs.path = "testfile.txt";
305305
theirs.mode = 0100755;
306306

0 commit comments

Comments
 (0)