Overlapping Character Sets
Identifier: regex.lint.overlap.charset
When it triggers: Alternation branches have overlapping character sets, and the alternation is repeated by an unbounded quantifier: a character both branches accept can be taken by either, on every iteration. An alternation matched once, such as /[a-c]|[b-d]/, is not reported, nor is one inside a lookbehind ((?<!http:|https:)): PCRE runs a lookaround atomically, so the loop around it never comes back to try the other branch. A loop at the very end of the pattern is still reported. Two branches that start with literal text which tells them apart, associative-array|array or callable-(?:string|object)|class-string, are not reported either: the input one branch takes, the other never does, whatever character they share at their start. The text is read through an atomic group and a script run, so /(?:(?>ab)|ac)+/ and /(?:(*sr:öa)|öb)+x/u are not reported. Two texts one of which starts the other are told apart by what follows the shorter one, where both are read in their own case: in /(?:a\d|aa)+/ the second a is no digit and in /(?:a\d|a[a-z])+/ no character follows both texts, so neither is reported, while /(?:a\w|ab)+/ is. Read caselessly, or where the bytes of what follows are not known, as those of \d under /u, the texts alone decide: /(?:a\d|aa)+/i and /(?:a\d|aa)+/u are reported. Two literal branches written alike are one branch twice, and are reported whatever their first characters tell: /(?:é|é|)+/u, where the empty branch keeps the engine from being asked and the first bytes say nothing past ASCII. Past ASCII the branches are compared by the characters the target’s PCRE reads, not by their bytes: /(?:ö|è)+/u is not reported, though both start with the byte \xC3. Without /u a byte past ASCII, written as it is or as an escape (\xE9, \x{E9}, \351 or \o{351}), is read as the tables of the target’s locale class it, and read caselessly it meets its other case as they give it, whether the engine judges the branches or, one of them matching empty, their bytes decide: beside \W it overlaps under C, where \W takes every byte past ASCII, and beside \w or [[:alpha:]] under fr_FR.ISO8859-1, where \xE9 is a letter; in PHP, "/(?:\xC9|[\xC9\xCA]|)+/i", with raw bytes, is reported under both, and '/(?:\xC9|[\xE9\xEA]|)+/i', written with escapes, under fr_FR.ISO8859-1 and not under C, where \xC9 has no other case. An ASCII letter read caselessly meets its other case as those tables give it too, where the engine’s paths read every case alike: under glibc’s tr_TR.ISO-8859-9, which pairs i with \xDD and I with \xFD, /(?:i|\xDD)+$/i is reported and /(?:i|I)+$/i is not, its branches taking no character in common there. Under /u an ASCII letter written alone, i, \x69, [i] or [^i], has the case those tables give it too, as PCRE2 reads it, where a class of several characters reads Unicode’s: under glibc’s tr_TR.UTF-8, which gives i and I no other case, /(?:[i]|I)+$/iu is not reported, and /(?:[ij]|I)+$/iu and /(?:[^i]|I)+$/iu are. Under (*TURKISH_CASING), which pairs i with İ and I with ı, two characters that start with one byte, the rule stays silent in a pattern that may read a letter caselessly: /(*TURKISH_CASING)(?:i|I)+$/iu, whose branches never meet, is not reported. The texts branches start with, escapes included, are read character by character in the case in force where each stands, as the tables give it, so "/(?:\xC9x\\d|\xE9x\\w)+/i" is reported under fr_FR.ISO8859-1, and /(?:a(?-i)B\d|A(?-i)b\w)+/i is not, its second letters telling the branches apart; two branches of literal text alone that fold alike, "/(?:\xC9|\xE9)+/i" or '/(?:\xC9|\xE9|)+/i' there, are reported as overlapping branches instead. Under (*UCP) a byte read caselessly has the case the target’s PCRE2 gives it, the character of the same value’s from 10.35 and the tables’ before: /(*UCP)(?:\xC9|\xE9)+$/i is reported for PHP 8.0 and later, not for PHP 7.4 with PCRE2 10.34, where \xC9 matches itself alone. Where the engine’s paths read a byte apart under another locale’s tables, the branches meet where either path starts both with the same byte: under el_GR.ISO8859-7 the JIT matches "/\xF2/i" on \xD3 and the interpreter does not, so "/(?:\xF2|\xD3)+/i" is reported. A branch is read past a first item that may read nothing (/(?:ö?è|ê)+x/u) and past a lookaround, which looks at characters without taking them: /^(?:(?!x)a|a)+$/ is reported, /(?:[^$]+|(?<=\\)\$)+/, /^(?:>(?:(?!!)|!))+$/ and /^(?:(?=ö)ö|è)+$/u are not. A lookahead narrows the characters the branch starts with: a positive one to those its body starts with, a negative one to all but those its body refuses whatever follows them (a for (?!a), (?!ab?) or (?!a+), none for (?!ab), (?!a{2}) or (?!a$)). So /\A(?:(?=b)[ab]|a)+\z/ and /\A(?:(?!a)[ab]|a)+\z/ are not reported, /\A(?:(?=a)[ab]|a)+\z/ and /(?:(?!ab)a|a)+/ are. Lookaheads that differ by the way taken let through what any way does: /\A(?:(?:(?=a)|(?=b))[ab]|a)+\z/ is reported. A lookahead the rule cannot read, or lookaheads in more than sixteen ways, leave the branch unjudged. A script run starts with what its body starts with: /(?:(*sr:ö)|è)+/u is not reported. Where the target’s PCRE cannot be asked about a branch, as for a class holding every delimiter, the first bytes are compared, under /u the ASCII ones alone: a lead byte two characters share is no overlap, and a character past ASCII is known by the byte it starts with, written as it is, as an escape or in a range: /(?:[a-z]|\x{E9}|[a-c]|)+/u and /(?:[a-z]|[é-ÿ]|[a-c]|)+/u are reported as /(?:[a-z]|é|[a-c]|)+/u is. A branch is read through every branch of a branch reset group (?|…), and under the very options of a that stand where it does, (?aW) restricting \w to ASCII and (?aD) leaving it as it is; under a pattern’s own /D or /S, which those options spell the same, the branch is left unjudged. A class read under (?xx) drops its spaces and tabs, which (?x) alone, (?-x), (?^) and PHP’s x modifier, however many times it is written, leave in: /(?xx)(?:[a b]|\x20|\xE9)+/ is not reported, /(?:[a b]|\x20|\xE9)+/xx is. The caseless restriction (*CASELESS_RESTRICT) sets holds until (?-r) lifts it: under /iu the Kelvin sign then matches k and K, which [^k] refuses, so /(*CASELESS_RESTRICT)(?-r:\x{212A}|[^k])+/iu is not reported. With the ReDoS analysis on, a pattern the analysis proves linear drops the issue, inline options such as (?s) or (?s:…) included; like the two rules above, the issue is also left out for a pattern listed in the ignored patterns or found trivially safe.
Example:
// WARNING: Overlapping character classes inside a repetition
preg_match('/(?:[a-c]|[b-d])+$/', $input);
// SAFER: Use an atomic group to avoid backtracking
preg_match('/(?>[a-c]|[b-d])+$/', $input);
// IF EQUIVALENT: Merge ranges
preg_match('/[a-d]+$/', $input);
Try it. Open /(?:[a-c]|[b-d])+$/ in the playground: the finding is in the Diagnostics tab.