Subject: bug#47264: [PATCH v2] pcre: migrate to pcre2

From: Carlo Marcelo Arenas Belón <[email-masked]>
Date: Mon, 15 Nov 2021 16:11:19 -0800

On Mon, Nov 15, 2021 at 03:24:41PM -0800, Paul Eggert wrote:
> On 11/15/21 12:49, Carlo Marcelo Arenas Belón wrote:
> 
> > Apologies, I realize it is difficult to talk about code in abstract when
> > not inlined, but I think it will better addressed by "fixing" it as shown
> > in the attached patch.
> 
> That patch isn't right, because the relevant code inside libpcre2 uses
> size_t, not PCRE2_SIZE and that means our hacky workaround should use
> SIZE_MAX, not PCRE2_SIZE_MAX, so that it's consistent with libpcre2's
> internals.

You are correct, got confused by the changes going to PCRE2_SIZE -> idx, and
should had check, thanks for catching this.

Hopefully the next patch (which I was planning to do with some of the
cleanup you beat me to), will at least clear some possible future valgrind
complain.

> Of course this is all academic on all existing platforms, since IDX_MAX is
> way less than SIZE_MAX.

Agree, and frankly I still think it will be better if IDX_MAX would be used
unconditionally instead.

Carlo
>From 75cd3b441c216134c9708e3e4b67720a4cdb096b Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Carlo=20Marcelo=20Arenas=20Bel=C3=B3n?= <[email-masked]>
Date: Mon, 15 Nov 2021 14:19:15 -0800
Subject: [PATCH] pcre: only make tables when needed and make them always
 reachable
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

When doing a match in non utf mode, chartables that reflect the
corresponding attributes that the locale define are needed, and
therefore that information is passed through a compile context.

Before ad6e5cb (grep: fix minor -P memory leak, 2021-11-14), the
table will leak together with the compile context on the hopes it
will be eventually free if a cleanup callback is even invented,
but now is not only leaking but unreachable, so at least make it
reachable again and while at it, make sure it is only created when
needed.

Signed-off-by: Carlo Marcelo Arenas Belón <[email-masked]>
---
 src/pcresearch.c | 12 +++++++++++-
 1 file changed, 11 insertions(+), 1 deletion(-)

diff --git a/src/pcresearch.c b/src/pcresearch.c
index c12c674..1f322f4 100644
--- a/src/pcresearch.c
+++ b/src/pcresearch.c
@@ -41,6 +41,9 @@ struct pcre_comp
   /* Compiled internal form of a Perl regular expression.  */
   pcre2_code *cre;
 
+  /* Charset tables.  */
+  const uint8_t *tables;
+
   /* Match context and data block.  */
   pcre2_match_context *mcontext;
   pcre2_match_data *data;
@@ -193,7 +196,14 @@ Pcompile (char *pattern, idx_t size, reg_syntax_t ignored, bool exact)
       size = re_size;
     }
 
-  pcre2_set_character_tables (ccontext, pcre2_maketables (gcontext));
+  if (! localeinfo.using_utf8)
+    {
+      pc->tables = pcre2_maketables (gcontext);
+      pcre2_set_character_tables (ccontext, pc->tables);
+    }
+  else
+    pc->tables = NULL;
+
   pc->cre = pcre2_compile ((PCRE2_SPTR) pattern, size, flags,
                            &ec, &e, ccontext);
   if (!pc->cre)
-- 
2.34.0.352.g07dee3c5e1