Inefficient RegexConstraint on StringLong fields causes "JIT stack limit reached"
>>> [!note] Migrated issue <!-- Drupal.org comment --> <!-- Migrated from issue #3575644. --> Reported by: [barakgalili](https://www.drupal.org/user/387269) >>> <h3 id="overview">Overview</h3> <p>The canvas module overrides the property definitions for the string_long field type in \Drupal\canvas\Plugin\Field\FieldTypeOverride\StringLongItemOverride.</p> <p>It currently adds a RegexConstraint to the value property with the pattern /(.|\r?\n)*/ to match any string, including newlines.</p> <p>When saving entities with large amounts of text (e.g., 10,000+ characters) in a string_long field, this specific regex pattern causes PHP's PCRE engine to hit the JIT stack limit (PREG_JIT_STACKLIMIT_ERROR, code 6). This happens because the alternating group (.|\r?\n)<br> combined with the * quantifier forces the engine to create an excessive number of capture groups and backtrack, exhausting memory.</p> <p>When the JIT limit is hit, the regex execution fails, which causes the field validation to fail. This results in the user seeing a generic "This value is not valid" validation error when trying to save the entity, with no clear indication in the logs as to why.</p> <h3>Steps to reproduce</h3> <p>Install and enable the canvas module.<br> Create a node type with a string_long field (e.g., a plain long text field).<br> Attempt to save a node with a very large string in this field (e.g., 15,000+ characters).<br> The entity save fails with a validation error on the field: "This value is not valid."<br> (Developer side) Manually running preg_match('/(.|\r?\n)*/', $long_string) returns 0 and preg_last_error() returns 6 (PREG_JIT_STACKLIMIT_ERROR).</p> <h3 id="proposed-resolution">Proposed resolution</h3> <p>Change the regex pattern to be more efficient. Since the goal of the regex seems to be matching any string including newlines, it can be replaced with the /s modifier (PCRE_DOTALL), which makes the dot . match all characters including newlines.<br> <strong>Change:</strong></p> <pre>&nbsp;&nbsp;&nbsp; <br>$properties['value']-&gt;addConstraint('Regex', [<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 'pattern' =&gt; '/(.|\r?\n)*/',<br>&nbsp;&nbsp;&nbsp; ]);</pre><p> To:</p> <pre>&nbsp;&nbsp;&nbsp; <br>$properties['value']-&gt;addConstraint('Regex', [<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 'pattern' =&gt; '/.*/s',<br>&nbsp;&nbsp;&nbsp; ]);</pre><p> This is exponentially faster and successfully validates long strings without exhausting the JIT stack.</p>
issue