Re: [PATCH v2] regexec: work around macOS TRE leak on invalid UTF-8

Junio C Hamano <[email protected]> Tue, 28 Jul 2026 17:41:34 -0700
Newsgroups org.kernel.vger.git
Message-ID <[email protected]>
Chungmin Lee <[email protected]> writes:

> diff --git a/Makefile b/Makefile
> index 1cec251..81075c3 100644
> --- a/Makefile
> +++ b/Makefile
> @@ -2264,6 +2264,10 @@ ifdef USE_ENHANCED_BASIC_REGULAR_EXPRESSIONS
>  	COMPAT_CFLAGS += -DUSE_ENHANCED_BASIC_REGULAR_EXPRESSIONS
>  	COMPAT_OBJS += compat/regcomp_enhanced.o
>  endif
> +ifdef DARWIN_REGEXEC
> +	COMPAT_OBJS += compat/darwin/regexec.o
> +	BASIC_CFLAGS += -DDARWIN_REGEXEC
> +endif
>  endif
>  ifdef NATIVE_CRLF
>  	BASIC_CFLAGS += -DNATIVE_CRLF
> diff --git a/compat/darwin.h b/compat/darwin.h
> new file mode 100644
> index 0000000..6fbdc34
> --- /dev/null
> +++ b/compat/darwin.h
> @@ -0,0 +1,8 @@
> +#ifndef COMPAT_DARWIN_H
> +#define COMPAT_DARWIN_H
> +
> +int darwin_regexec_buf(const regex_t *preg, const char *buf, size_t size,
> +		       size_t nmatch, regmatch_t pmatch[], int eflags);
> +#define regexec_buf darwin_regexec_buf
> +
> +#endif


This iteration looks much easier to grok, at least to me.  Two
things:

 * The name of the header, <compat/darwin.h>, sounds so nice and
   central, that those who care a lot more about macOS than I do may
   want to consolidate other support for the peculiarities macOS has
   also into it.  I personally do not have a strong opinion.

 * We'd need a comment near the beginning of Makefile, like other
   symbolis like NO_FINK and NO_APPLE_COMMMON_CRYPTO do, to tell the
   users when to define this new symbol.

The latter I would feel strong enough, so here is a sample update in
a squashable form.  If you have reasons to send a new iteration, you
are free to include it.  After waiting for comments from others for
a few days, if you still don't have reasons to send an update, you
can just tell me to squash the change on my end (if you agree with
the change, that is).



diff --git c/Makefile w/Makefile
index 81075c38a2..ed2868ce10 100644
--- c/Makefile
+++ w/Makefile
@@ -110,6 +110,9 @@ include shared.mak
 # Define USE_HOMEBREW_LIBICONV to link against libiconv installed by
 # Homebrew, if present.
 #
+# Define DARWIN_REGEXEC if regexec() in your platform regex library
+# leaks when fed an invalid UTF-8 sequence.
+#
 # Define NO_APPLE_COMMON_CRYPTO if you are building on Darwin/Mac OS X
 # and do not want to use Apple's CommonCrypto library.  This allows you
 # to provide your own OpenSSL library, for example from MacPorts.