summaryrefslogtreecommitdiffstats
path: root/diffcore-pickaxe.c
diff options
context:
space:
mode:
authorSZEDER Gábor <szeder.dev@gmail.com>2017-03-18 19:24:08 +0100
committerJunio C Hamano <gitster@pobox.com>2017-03-18 12:22:33 -0700
commitf53c5de29cec68e3294a008052251631eaffcf07 (patch)
tree54f07a808c0cdf9c1f5275e286fafb42f4ea0109 /diffcore-pickaxe.c
parent842a516cb02a53cf0291ff67ed6f8517966345c0 (diff)
downloadgit-f53c5de29cec68e3294a008052251631eaffcf07.tar.gz
git-f53c5de29cec68e3294a008052251631eaffcf07.tar.xz
pickaxe: fix segfault with '-S<...> --pickaxe-regex'
'git {log,diff,...} -S<...> --pickaxe-regex' can segfault as a result of out-of-bounds memory reads. diffcore-pickaxe.c:contains() looks for all matches of the given regex in a buffer in a loop, advancing the buffer pointer to the end of the last match in each iteration. When we switched to REG_STARTEND in b7d36ffca (regex: use regexec_buf(), 2016-09-21), we started passing the size of that buffer to the regexp engine, too. Unfortunately, this buffer size is never updated on subsequent iterations, and as the buffer pointer advances on each iteration, this "bufptr+bufsize" points past the end of the buffer. This results in segmentation fault, if that memory can't be accessed. In case of 'git log' it can also result in erroneously listed commits, if the memory past the end of buffer is accessible and happens to contain data matching the regex. Reduce the buffer size on each iteration as the buffer pointer is advanced, thus maintaining the correct end of buffer location. Furthermore, make sure that the buffer pointer is not dereferenced in the control flow statements when we already reached the end of the buffer. The new test is flaky, I've never seen it fail on my Linux box even without the fix, but this is expected according to db5dfa3 (regex: -G<pattern> feeds a non NUL-terminated string to regexec() and fails, 2016-09-21). However, it did fail on Travis CI with the first (and incomplete) version of the fix, and based on that commit message I would expect the new test without the fix to fail most of the time on Windows. Signed-off-by: SZEDER Gábor <szeder.dev@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Diffstat (limited to 'diffcore-pickaxe.c')
-rw-r--r--diffcore-pickaxe.c7
1 files changed, 5 insertions, 2 deletions
diff --git a/diffcore-pickaxe.c b/diffcore-pickaxe.c
index 8413d7658..e627140b8 100644
--- a/diffcore-pickaxe.c
+++ b/diffcore-pickaxe.c
@@ -79,12 +79,15 @@ static unsigned int contains(mmfile_t *mf, regex_t *regexp, kwset_t kws)
regmatch_t regmatch;
int flags = 0;
- while (*data &&
+ while (sz && *data &&
!regexec_buf(regexp, data, sz, 1, &regmatch, flags)) {
flags |= REG_NOTBOL;
data += regmatch.rm_eo;
- if (*data && regmatch.rm_so == regmatch.rm_eo)
+ sz -= regmatch.rm_eo;
+ if (sz && *data && regmatch.rm_so == regmatch.rm_eo) {
data++;
+ sz--;
+ }
cnt++;
}