Skip to content

Commit 203113e

Browse files
committed
range-diff: add configurable memory limit for cost matrix
When comparing large commit ranges (e.g., 250,000+ commits), range-diff attempts to allocate an n×n cost matrix that can exhaust available memory. For example, with 256,784 commits (n = 513,568), the matrix would require approximately 256GB of memory (513,568² × 4 bytes), causing either immediate segmentation faults due to integer overflow or system hangs. Add a memory limit check in get_correspondences() before allocating the cost matrix. This check uses the total size in bytes (n² × sizeof(int)) and compares it against a configurable maximum, preventing both excessive memory usage and integer overflow issues. The limit is configurable via a new --max-memory option that accepts human-readable sizes (e.g., "1G", "500M"). The default is 4GB for 64 bit systems and 2GB for 32 bit systems. This allows comparing ranges of approximately 32,000 (16,000) commits - generous for real-world use cases while preventing impractical operations. When the limit is exceeded, range-diff now displays a clear error message showing both the requested memory size and the maximum allowed, formatted in human-readable units for better user experience. Example usage: git range-diff --max-memory=1G branch1...branch2 git range-diff --max-memory=500M base..topic1 base..topic2 This approach was chosen over alternatives: - Pre-counting commits: Would require spawning additional git processes and reading all commits twice - Limiting by commit count: Less precise than actual memory usage - Streaming approach: Would require significant refactoring of the current algorithm This issue was previously discussed in: https://lore.kernel.org/git/[email protected]/ Acked-by: Johannes Schindelin <[email protected]> Signed-off-by: Paulo Casaretto <[email protected]>
1 parent 954d33a commit 203113e

File tree

5 files changed

+44
-4
lines changed

5 files changed

+44
-4
lines changed

builtin/log.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1404,6 +1404,7 @@ static void make_cover_letter(struct rev_info *rev, int use_separate_file,
14041404
struct range_diff_options range_diff_opts = {
14051405
.creation_factor = rev->creation_factor,
14061406
.dual_color = 1,
1407+
.max_memory = RANGE_DIFF_MAX_MEMORY_DEFAULT,
14071408
.diffopt = &opts,
14081409
.other_arg = &other_arg
14091410
};

builtin/range-diff.c

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include "parse-options.h"
77
#include "range-diff.h"
88
#include "config.h"
9+
#include "parse.h"
910

1011

1112
static const char * const builtin_range_diff_usage[] = {
@@ -15,6 +16,21 @@ N_("git range-diff [<options>] <base> <old-tip> <new-tip>"),
1516
NULL
1617
};
1718

19+
static int parse_max_memory(const struct option *opt, const char *arg, int unset)
20+
{
21+
size_t *max_memory = opt->value;
22+
uintmax_t val;
23+
24+
if (unset)
25+
return 0;
26+
27+
if (!git_parse_unsigned(arg, &val, SIZE_MAX))
28+
return error(_("invalid max-memory value: %s"), arg);
29+
30+
*max_memory = (size_t)val;
31+
return 0;
32+
}
33+
1834
int cmd_range_diff(int argc,
1935
const char **argv,
2036
const char *prefix,
@@ -25,6 +41,7 @@ int cmd_range_diff(int argc,
2541
struct strvec diff_merges_arg = STRVEC_INIT;
2642
struct range_diff_options range_diff_opts = {
2743
.creation_factor = RANGE_DIFF_CREATION_FACTOR_DEFAULT,
44+
.max_memory = RANGE_DIFF_MAX_MEMORY_DEFAULT,
2845
.diffopt = &diffopt,
2946
.other_arg = &other_arg
3047
};
@@ -40,6 +57,10 @@ int cmd_range_diff(int argc,
4057
PARSE_OPT_OPTARG),
4158
OPT_PASSTHRU_ARGV(0, "diff-merges", &diff_merges_arg,
4259
N_("style"), N_("passed to 'git log'"), 0),
60+
OPT_CALLBACK(0, "max-memory", &range_diff_opts.max_memory,
61+
N_("size"),
62+
N_("maximum memory for cost matrix (default 4G)"),
63+
parse_max_memory),
4364
OPT_PASSTHRU_ARGV(0, "remerge-diff", &diff_merges_arg, NULL,
4465
N_("passed to 'git log'"), PARSE_OPT_NOARG),
4566
OPT_BOOL(0, "left-only", &left_only,

log-tree.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -717,6 +717,7 @@ static void show_diff_of_diff(struct rev_info *opt)
717717
struct range_diff_options range_diff_opts = {
718718
.creation_factor = opt->creation_factor,
719719
.dual_color = 1,
720+
.max_memory = RANGE_DIFF_MAX_MEMORY_DEFAULT,
720721
.diffopt = &opts
721722
};
722723

range-diff.c

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -325,13 +325,24 @@ static int diffsize(const char *a, const char *b)
325325
}
326326

327327
static void get_correspondences(struct string_list *a, struct string_list *b,
328-
int creation_factor)
328+
int creation_factor, size_t max_memory)
329329
{
330330
int n = a->nr + b->nr;
331331
int *cost, c, *a2b, *b2a;
332332
int i, j;
333-
334-
ALLOC_ARRAY(cost, st_mult(n, n));
333+
size_t cost_size = st_mult(n, n);
334+
size_t cost_bytes = st_mult(sizeof(int), cost_size);
335+
if (cost_bytes >= max_memory) {
336+
struct strbuf cost_str = STRBUF_INIT;
337+
struct strbuf max_str = STRBUF_INIT;
338+
strbuf_humanise_bytes(&cost_str, cost_bytes);
339+
strbuf_humanise_bytes(&max_str, max_memory);
340+
die(_("range-diff: unable to compute the range-diff, since it "
341+
"exceeds the maximum memory for the cost matrix: %s "
342+
"(%"PRIuMAX" bytes) needed, limited to %s (%"PRIuMAX" bytes)"),
343+
cost_str.buf, (uintmax_t)cost_bytes, max_str.buf, (uintmax_t)max_memory);
344+
}
345+
ALLOC_ARRAY(cost, cost_size);
335346
ALLOC_ARRAY(a2b, n);
336347
ALLOC_ARRAY(b2a, n);
337348

@@ -591,7 +602,8 @@ int show_range_diff(const char *range1, const char *range2,
591602
if (!res) {
592603
find_exact_matches(&branch1, &branch2);
593604
get_correspondences(&branch1, &branch2,
594-
range_diff_opts->creation_factor);
605+
range_diff_opts->creation_factor,
606+
range_diff_opts->max_memory);
595607
output(&branch1, &branch2, range_diff_opts);
596608
}
597609

range-diff.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@
55
#include "strvec.h"
66

77
#define RANGE_DIFF_CREATION_FACTOR_DEFAULT 60
8+
#define RANGE_DIFF_MAX_MEMORY_DEFAULT \
9+
(sizeof(void*) >= 8 ? \
10+
((size_t)(1024L * 1024L) * (size_t)(4L * 1024L)) : /* 4GB on 64-bit */ \
11+
((size_t)(1024L * 1024L) * (size_t)(2L * 1024L))) /* 2GB on 32-bit */
812

913
/*
1014
* A much higher value than the default, when we KNOW we are comparing
@@ -17,6 +21,7 @@ struct range_diff_options {
1721
unsigned dual_color:1;
1822
unsigned left_only:1, right_only:1;
1923
unsigned include_merges:1;
24+
size_t max_memory;
2025
const struct diff_options *diffopt; /* may be NULL */
2126
const struct strvec *other_arg; /* may be NULL */
2227
};

0 commit comments

Comments
 (0)