Skip to content

Commit 2f89522

Browse files
dschogitster
authored andcommitted
regex: add regexec_buf() that can work on a non NUL-terminated string
We just introduced a test that demonstrates that our sloppy use of regexec() on a mmap()ed area can result in incorrect results or even hard crashes. So what we need to fix this is a function that calls regexec() on a length-delimited, rather than a NUL-terminated, string. Happily, there is an extension to regexec() introduced by the NetBSD project and present in all major regex implementation including Linux', MacOSX' and the one Git includes in compat/regex/: by using the (non-POSIX) REG_STARTEND flag, it is possible to tell the regexec() function that it should only look at the offsets between pmatch[0].rm_so and pmatch[0].rm_eo. That is exactly what we need. Since support for REG_STARTEND is so widespread by now, let's just introduce a helper function that always uses it, and tell people on a platform whose regex library does not support it to use the one from our compat/regex/ directory. Signed-off-by: Johannes Schindelin <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent db5dfa3 commit 2f89522

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

Makefile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,8 @@ all::
296296
# Define USE_NED_ALLOCATOR if you want to replace the platforms default
297297
# memory allocators with the nedmalloc allocator written by Niall Douglas.
298298
#
299-
# Define NO_REGEX if you have no or inferior regex support in your C library.
299+
# Define NO_REGEX if your C library lacks regex support with REG_STARTEND
300+
# feature.
300301
#
301302
# Define HAVE_DEV_TTY if your system can open /dev/tty to interact with the
302303
# user.

git-compat-util.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -942,6 +942,19 @@ void git_qsort(void *base, size_t nmemb, size_t size,
942942
#define qsort git_qsort
943943
#endif
944944

945+
#ifndef REG_STARTEND
946+
#error "Git requires REG_STARTEND support. Compile with NO_REGEX=NeedsStartEnd"
947+
#endif
948+
949+
static inline int regexec_buf(const regex_t *preg, const char *buf, size_t size,
950+
size_t nmatch, regmatch_t pmatch[], int eflags)
951+
{
952+
assert(nmatch > 0 && pmatch);
953+
pmatch[0].rm_so = 0;
954+
pmatch[0].rm_eo = size;
955+
return regexec(preg, buf, nmatch, pmatch, eflags | REG_STARTEND);
956+
}
957+
945958
#ifndef DIR_HAS_BSD_GROUP_SEMANTICS
946959
# define FORCE_DIR_SET_GID S_ISGID
947960
#else

0 commit comments

Comments
 (0)