Commit b3d460dcfa for perl

commit b3d460dcfa5c05687f051570e6de78395afedbad
Author: Richard Leach <rich+perl@hyphen-dash-hyphen.info>
Date:   Fri Nov 27 22:47:08 2026 +0000

    regexp struct must fit all RX_COMPFLAGS

    A regex is recompiled if - amongst other conditions - the previous
    flags retrieved via `RX_COMPFLAGS(re)` do not match `orig_rx_flags`:

        if (   old_re
            && !recompile
            && cBOOL(RX_UTF8(old_re)) == RExC_utf8
            && ( RX_COMPFLAGS(old_re) == ( orig_rx_flags & RXf_PMf_FLAGCOPYMASK ) )
            && RX_PRELEN(old_re) == plen
            && memEQ(RX_PRECOMP(old_re), exp, plen)
            && !runtime_code /* with runtime code, always recompile */ )

    However, this is a comparison of 9 bits:

        PERL_BITFIELD32 compflags:9;

    against 11 bits:

        #define RXf_PMf_SHIFT_COMPILETIME_ (RXf_PMf_STD_PMMOD_SHIFT+11)
        #define RXf_PMf_SPLIT (1U<<(RXf_PMf_STD_PMMOD_SHIFT+11))
        #define RXf_PMf_FLAGCOPYMASK    \
            ( RXf_PMf_COMPILETIME       \
            | RXf_PMf_SPLIT )

    A size mismatch has arisen over time; this could cause unnecessary
    recompilation of patterns. This commit attempts to keep the size of
    `compflags` in line with the actual number of flags defined.

    core's _mktables_ was the starting point for identifying the mismatch.
    It runs over 2.5x faster (on the author's dev VM) following this commit.

diff --git a/pod/perldelta.pod b/pod/perldelta.pod
index 5dc02b3dda..f7c632cbd2 100644
--- a/pod/perldelta.pod
+++ b/pod/perldelta.pod
@@ -378,6 +378,12 @@ manager will later use a regex to expand these into links.

 =item *

+Regex patterns could have been unnecessarily recompiled at runtime due to a
+mismatched comparison of old and new flags.
+[GH #25126]
+
+=item *
+
 XXX

 =back
diff --git a/pod/perlreapi.pod b/pod/perlreapi.pod
index a19a54fb8a..1c3ea13c98 100644
--- a/pod/perlreapi.pod
+++ b/pod/perlreapi.pod
@@ -700,7 +700,8 @@ values.

         /* original flags used to compile the pattern, may differ from
          * extflags in various ways */
-        PERL_BITFIELD32 compflags:9;
+        /* MUST be at least as wide as RXf_PMf_FLAGCOPYMASK */
+        PERL_BITFIELD32 compflags:RXf_PMf_SHIFT_NEXT_;

         /* Is offs_spare already in use? */
         PERL_BITFIELD32 offs_spare_used:1;
diff --git a/regexp.h b/regexp.h
index 9123c01bd3..18e6599c59 100644
--- a/regexp.h
+++ b/regexp.h
@@ -200,7 +200,8 @@ typedef struct regexp {

     /* original flags used to compile the pattern, may differ from
      * extflags in various ways */
-    PERL_BITFIELD32 compflags:9;
+    /* MUST be at least as wide as RXf_PMf_FLAGCOPYMASK */
+    PERL_BITFIELD32 compflags:RXf_PMf_SHIFT_NEXT_;

     /* Is offs_spare already in use? */
     PERL_BITFIELD32 offs_spare_used:1;
diff --git a/t/re/recompile.t b/t/re/recompile.t
index 5a6f7d7f91..b88931d1e0 100644
--- a/t/re/recompile.t
+++ b/t/re/recompile.t
@@ -20,7 +20,7 @@ BEGIN {
 use strict;
 use warnings;

-plan tests => 48;
+plan tests => 50;

 my $results = runperl(
 			switches => [ '-Dr' ],
@@ -103,6 +103,14 @@ comp_1(<<'CODE', 'simple qr');
 "a" =~ qr/$_/ for qw(a a a);
 CODE

+# "compflags" in the regex struct must be large enough to hold all
+# RXf_PMf_* bits. At the time of writing, this exercised one of
+# the higher bits and demonstrated a size mismatch bug.
+comp_1(<<'CODE', "re_strict compflags comparison");
+BEGIN{ $^H{reflags} = 1 << 10; $^H |= 0x02000000; }
+"a" =~ /$_/ for qw(a a a);
+CODE
+
 comp_1(<<'CODE', 'literal utf8');
 "a" =~ /$_/ for "\x{100}", "\x{100}", "\x{100}";
 CODE