Skip to content

Commit 98a51c6

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 d5c8b17 + d60360c commit 98a51c6

File tree

5 files changed

+57
-20
lines changed

5 files changed

+57
-20
lines changed

diff.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -951,7 +951,8 @@ static int find_word_boundaries(mmfile_t *buffer, regex_t *word_regex,
951951
{
952952
if (word_regex && *begin < buffer->size) {
953953
regmatch_t match[1];
954-
if (!regexec(word_regex, buffer->ptr + *begin, 1, match, 0)) {
954+
if (!regexec_buf(word_regex, buffer->ptr + *begin,
955+
buffer->size - *begin, 1, match, 0)) {
955956
char *p = memchr(buffer->ptr + *begin + match[0].rm_so,
956957
'\n', match[0].rm_eo - match[0].rm_so);
957958
*end = p ? p - buffer->ptr : match[0].rm_eo + *begin;

diffcore-pickaxe.c

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ static void diffgrep_consume(void *priv, char *line, unsigned long len)
2323
{
2424
struct diffgrep_cb *data = priv;
2525
regmatch_t regmatch;
26-
int hold;
2726

2827
if (line[0] != '+' && line[0] != '-')
2928
return;
@@ -33,11 +32,8 @@ static void diffgrep_consume(void *priv, char *line, unsigned long len)
3332
* caller early.
3433
*/
3534
return;
36-
/* Yuck -- line ought to be "const char *"! */
37-
hold = line[len];
38-
line[len] = '\0';
39-
data->hit = !regexec(data->regexp, line + 1, 1, &regmatch, 0);
40-
line[len] = hold;
35+
data->hit = !regexec_buf(data->regexp, line + 1, len - 1, 1,
36+
&regmatch, 0);
4137
}
4238

4339
static int diff_grep(mmfile_t *one, mmfile_t *two,
@@ -50,9 +46,11 @@ static int diff_grep(mmfile_t *one, mmfile_t *two,
5046
xdemitconf_t xecfg;
5147

5248
if (!one)
53-
return !regexec(regexp, two->ptr, 1, &regmatch, 0);
49+
return !regexec_buf(regexp, two->ptr, two->size,
50+
1, &regmatch, 0);
5451
if (!two)
55-
return !regexec(regexp, one->ptr, 1, &regmatch, 0);
52+
return !regexec_buf(regexp, one->ptr, one->size,
53+
1, &regmatch, 0);
5654

5755
/*
5856
* We have both sides; need to run textual diff and see if
@@ -83,8 +81,8 @@ static unsigned int contains(mmfile_t *mf, regex_t *regexp, kwset_t kws)
8381
regmatch_t regmatch;
8482
int flags = 0;
8583

86-
assert(data[sz] == '\0');
87-
while (*data && !regexec(regexp, data, 1, &regmatch, flags)) {
84+
while (*data &&
85+
!regexec_buf(regexp, data, sz, 1, &regmatch, flags)) {
8886
flags |= REG_NOTBOL;
8987
data += regmatch.rm_eo;
9088
if (*data && regmatch.rm_so == regmatch.rm_eo)

git-compat-util.h

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -983,6 +983,27 @@ void git_qsort(void *base, size_t nmemb, size_t size,
983983
#define qsort git_qsort
984984
#endif
985985

986+
static inline int regexec_buf(const regex_t *preg, const char *buf, size_t size,
987+
size_t nmatch, regmatch_t pmatch[], int eflags)
988+
{
989+
#ifdef REG_STARTEND
990+
assert(nmatch > 0 && pmatch);
991+
pmatch[0].rm_so = 0;
992+
pmatch[0].rm_eo = size;
993+
return regexec(preg, buf, nmatch, pmatch, eflags | REG_STARTEND);
994+
#else
995+
char *buf2 = xmalloc(size + 1);
996+
int ret;
997+
998+
memcpy(buf2, buf, size);
999+
buf2[size] = '\0';
1000+
ret = regexec(preg, buf2, nmatch, pmatch, eflags);
1001+
free(buf2);
1002+
1003+
return ret;
1004+
#endif
1005+
}
1006+
9861007
#ifndef DIR_HAS_BSD_GROUP_SEMANTICS
9871008
# define FORCE_DIR_SET_GID S_ISGID
9881009
#else

t/t4061-diff-pickaxe.sh

Lines changed: 22 additions & 0 deletions
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

xdiff-interface.c

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -214,11 +214,10 @@ struct ff_regs {
214214
static long ff_regexp(const char *line, long len,
215215
char *buffer, long buffer_size, void *priv)
216216
{
217-
char *line_buffer;
218217
struct ff_regs *regs = priv;
219218
regmatch_t pmatch[2];
220219
int i;
221-
int result = -1;
220+
int result;
222221

223222
/* Exclude terminating newline (and cr) from matching */
224223
if (len > 0 && line[len-1] == '\n') {
@@ -228,18 +227,16 @@ static long ff_regexp(const char *line, long len,
228227
len--;
229228
}
230229

231-
line_buffer = xstrndup(line, len); /* make NUL terminated */
232-
233230
for (i = 0; i < regs->nr; i++) {
234231
struct ff_reg *reg = regs->array + i;
235-
if (!regexec(&reg->re, line_buffer, 2, pmatch, 0)) {
232+
if (!regexec_buf(&reg->re, line, len, 2, pmatch, 0)) {
236233
if (reg->negate)
237-
goto fail;
234+
return -1;
238235
break;
239236
}
240237
}
241238
if (regs->nr <= i)
242-
goto fail;
239+
return -1;
243240
i = pmatch[1].rm_so >= 0 ? 1 : 0;
244241
line += pmatch[i].rm_so;
245242
result = pmatch[i].rm_eo - pmatch[i].rm_so;
@@ -248,8 +245,6 @@ static long ff_regexp(const char *line, long len,
248245
while (result > 0 && (isspace(line[result - 1])))
249246
result--;
250247
memcpy(buffer, line, result);
251-
fail:
252-
free(line_buffer);
253248
return result;
254249
}
255250

0 commit comments

Comments
 (0)