Skip to content

Commit

Permalink
Merge branch 'mmap-regexec'
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
dscho committed Sep 5, 2016
2 parents 732a511 + a264f55 commit c4f481a
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 0 deletions.
9 changes: 9 additions & 0 deletions diff.c
Original file line number Diff line number Diff line change
Expand Up @@ -2826,6 +2826,15 @@ int diff_populate_filespec(struct diff_filespec *s, unsigned int flags)
s->data = strbuf_detach(&buf, &size);
s->size = size;
s->should_free = 1;
} else {
/* data must be NUL-terminated so e.g. for regexec() */
char *data = xmalloc(s->size + 1);
memcpy(data, s->data, s->size);
data[s->size] = '\0';
munmap(s->data, s->size);
s->should_munmap = 0;
s->data = data;
s->should_free = 1;
}
}
else {
Expand Down
2 changes: 2 additions & 0 deletions diffcore-pickaxe.c
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ static int diff_grep(mmfile_t *one, mmfile_t *two,
xpparam_t xpp;
xdemitconf_t xecfg;

assert(!one || one->ptr[one->size] == '\0');
assert(!two || two->ptr[two->size] == '\0');
if (!one)
return !regexec(regexp, two->ptr, 1, &regmatch, 0);
if (!two)
Expand Down
22 changes: 22 additions & 0 deletions t/t4059-diff-pickaxe.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#!/bin/sh
#
# Copyright (c) 2016 Johannes Schindelin
#

test_description='Pickaxe options'

. ./test-lib.sh

test_expect_success setup '
test_commit initial &&
printf "%04096d" 0 >4096-zeroes.txt &&
git add 4096-zeroes.txt &&
test_tick &&
git commit -m "A 4k file"
'
test_expect_success '-G matches' '
git diff --name-only -G "^0{4096}$" HEAD^ >out &&
test 4096-zeroes.txt = "$(cat out)"
'

test_done

0 comments on commit c4f481a

Please sign in to comment.