regex: use regexec_buf()
The new regexec_buf() function operates on buffers with an explicitly specified length, rather than NUL-terminated strings. We need to use this function whenever the buffer we want to pass to regexec(3) may have been mmap(2)ed (and is hence not NUL-terminated). Note: the original motivation for this patch was to fix a bug where `git diff -G <regex>` would crash. This patch converts more callers, though, some of which allocated to construct NUL-terminated strings, or worse, modified buffers to temporarily insert NULs while calling regexec(3). By converting them to use regexec_buf(), the code has become much cleaner. Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:
committed by
Junio C Hamano
parent
2f8952250a
commit
b7d36ffca0
@ -216,11 +216,10 @@ struct ff_regs {
|
||||
static long ff_regexp(const char *line, long len,
|
||||
char *buffer, long buffer_size, void *priv)
|
||||
{
|
||||
char *line_buffer;
|
||||
struct ff_regs *regs = priv;
|
||||
regmatch_t pmatch[2];
|
||||
int i;
|
||||
int result = -1;
|
||||
int result;
|
||||
|
||||
/* Exclude terminating newline (and cr) from matching */
|
||||
if (len > 0 && line[len-1] == '\n') {
|
||||
@ -230,18 +229,16 @@ static long ff_regexp(const char *line, long len,
|
||||
len--;
|
||||
}
|
||||
|
||||
line_buffer = xstrndup(line, len); /* make NUL terminated */
|
||||
|
||||
for (i = 0; i < regs->nr; i++) {
|
||||
struct ff_reg *reg = regs->array + i;
|
||||
if (!regexec(®->re, line_buffer, 2, pmatch, 0)) {
|
||||
if (!regexec_buf(®->re, line, len, 2, pmatch, 0)) {
|
||||
if (reg->negate)
|
||||
goto fail;
|
||||
return -1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (regs->nr <= i)
|
||||
goto fail;
|
||||
return -1;
|
||||
i = pmatch[1].rm_so >= 0 ? 1 : 0;
|
||||
line += pmatch[i].rm_so;
|
||||
result = pmatch[i].rm_eo - pmatch[i].rm_so;
|
||||
@ -250,8 +247,6 @@ static long ff_regexp(const char *line, long len,
|
||||
while (result > 0 && (isspace(line[result - 1])))
|
||||
result--;
|
||||
memcpy(buffer, line, result);
|
||||
fail:
|
||||
free(line_buffer);
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user