From: Patrick Steinhardt <ps@pks.im>
To: Chungmin Lee <chungmin@chungminlee.com>
Cc: git@vger.kernel.org, Junio C Hamano <gitster@pobox.com>
Subject: Re: [PATCH v2] regexec: work around macOS TRE leak on invalid UTF-8
Date: Wed, 5 Aug 2026 11:00:24 +0200 [thread overview]
Message-ID: <anL7qL2-4h8ZlLcg@pks.im> (raw)
In-Reply-To: <20260728052538.12429-1-chungmin@chungminlee.com>
On Mon, Jul 27, 2026 at 10:25:38PM -0700, Chungmin Lee wrote:
> On macOS, the system regex engine leaks an internal buffer when
> regexec() encounters an invalid multibyte sequence in a UTF-8 locale.
> The line-by-line path can call regexec_buf() for each pattern on every
> line, so "git grep" can leak repeatedly on a file containing invalid
> UTF-8. The total leak grows with the number of calls, and the per-call
> allocation grows with the pattern's automaton. In one case, grepping a
> repository containing PDFs exhausted memory and caused the machine to
> restart.
>
> ce025ae4f61e (grep: disable lookahead on error, 2024-10-20) made "git
> grep" fall back to line-by-line matching when regexec() reports an error
> on invalid UTF-8. That fallback cannot prevent this leak: the allocation
> has already leaked when regexec() returns REG_ILLSEQ.
>
> Avoid the leaking path by providing a Darwin-specific regexec_buf().
> Walk the input with mbrtowc(), split it at bytes that cannot form a
> complete multibyte character, and search each valid segment separately.
> This preserves matches in valid text on either side of an invalid byte.
Hm. I feel like we're adding quite a lot of logic only to fix an
upstream bug that we expect will be eventually fixed. At the same time
we already have a compatibility "regexec" implementation that I'd expect
doesn't have the bug. So would an alternative be to detect whether the
given platform is susceptible to the bug and, if so, define NO_REGEX and
then use our own regex implementation? Or are there good reasons to not
do that?
Patrick
prev parent reply other threads:[~2026-08-05 9:00 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-07-22 5:31 [PATCH] regexec: work around macOS TRE memory leak on invalid UTF-8 Chungmin Lee
2026-07-24 4:19 ` Junio C Hamano
2026-07-28 5:25 ` [PATCH v2] regexec: work around macOS TRE " Chungmin Lee
2026-07-29 0:41 ` Junio C Hamano
2026-08-05 9:00 ` Patrick Steinhardt [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=anL7qL2-4h8ZlLcg@pks.im \
--to=ps@pks.im \
--cc=chungmin@chungminlee.com \
--cc=git@vger.kernel.org \
--cc=gitster@pobox.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox