Skip to content

Commit c4f481a

Browse files
committed
Merge branch 'mmap-regexec'
This topic branch fixes a segmentation fault when using `-G` or `-S --pickaxe-regex` with `git diff` on new-born files that are configured without user diff drivers, and that hence get mmap()ed into memory. Signed-off-by: Johannes Schindelin <[email protected]>
2 parents 732a511 + a264f55 commit c4f481a

File tree

3 files changed

+33
-0
lines changed

3 files changed

+33
-0
lines changed

diff.c

+9
Original file line numberDiff line numberDiff line change
@@ -2826,6 +2826,15 @@ int diff_populate_filespec(struct diff_filespec *s, unsigned int flags)
28262826
s->data = strbuf_detach(&buf, &size);
28272827
s->size = size;
28282828
s->should_free = 1;
2829+
} else {
2830+
/* data must be NUL-terminated so e.g. for regexec() */
2831+
char *data = xmalloc(s->size + 1);
2832+
memcpy(data, s->data, s->size);
2833+
data[s->size] = '\0';
2834+
munmap(s->data, s->size);
2835+
s->should_munmap = 0;
2836+
s->data = data;
2837+
s->should_free = 1;
28292838
}
28302839
}
28312840
else {

diffcore-pickaxe.c

+2
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ static int diff_grep(mmfile_t *one, mmfile_t *two,
4949
xpparam_t xpp;
5050
xdemitconf_t xecfg;
5151

52+
assert(!one || one->ptr[one->size] == '\0');
53+
assert(!two || two->ptr[two->size] == '\0');
5254
if (!one)
5355
return !regexec(regexp, two->ptr, 1, &regmatch, 0);
5456
if (!two)

t/t4059-diff-pickaxe.sh

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#!/bin/sh
2+
#
3+
# Copyright (c) 2016 Johannes Schindelin
4+
#
5+
6+
test_description='Pickaxe options'
7+
8+
. ./test-lib.sh
9+
10+
test_expect_success setup '
11+
test_commit initial &&
12+
printf "%04096d" 0 >4096-zeroes.txt &&
13+
git add 4096-zeroes.txt &&
14+
test_tick &&
15+
git commit -m "A 4k file"
16+
'
17+
test_expect_success '-G matches' '
18+
git diff --name-only -G "^0{4096}$" HEAD^ >out &&
19+
test 4096-zeroes.txt = "$(cat out)"
20+
'
21+
22+
test_done

0 commit comments

Comments
 (0)