{"record":{"id":"d1cf16ffefd1bb5c","repo":"rectorphp/rector","slug":"a-literal-null-byte-in-the-regex","errorCode":null,"errorMessage":"a literal null byte in the regex","messagePattern":"a literal null byte in the regex","errorType":"validation","errorClass":"InvalidEregException","httpStatus":null,"severity":"error","filePath":"rules/Php70/EregToPcreTransformer.php","lineNumber":241,"sourceCode":"                    }\n                    if (ord($a) > ord($b)) {\n                        $errorMessage = sprintf('an invalid character range %d-%d\"', (int) $a, (int) $b);\n                        throw new InvalidEregException($errorMessage);\n                    }\n                    $cls .= $this->_ere2pcre_escape($a) . '-' . $this->_ere2pcre_escape($b);\n                    ++$i;\n                } else {\n                    $cls .= $this->_ere2pcre_escape($a);\n                }\n            }\n            $start = \\false;\n        } while ($i < $l && $s[$i] !== ']');\n        return [$cls, $i];\n    }\n    private function _ere2pcre_escape(string $content): string\n    {\n        if ($content === \"\\x00\") {\n            throw new InvalidEregException('a literal null byte in the regex');\n        }\n        if (strpos('\\^$.[]|()?*+{}-/', $content) !== \\false) {\n            return '\\\\' . $content;\n        }\n        return $content;\n    }\n    /**\n     * @param array<int, mixed> $r\n     */\n    private function processCurlyBracket(string $s, int $i, array &$r, int $rr): int\n    {\n        $ii = strpos($s, '}', $i);\n        if ($ii === \\false) {\n            throw new InvalidEregException('\"{\" does not have a matching \"}\"');\n        }\n        $start = $i + 1;\n        $length = $ii - ($i + 1);\n        $bound = Strings::substring($s, $start, $length);","sourceCodeStart":223,"sourceCodeEnd":259,"githubUrl":"https://github.com/rectorphp/rector/blob/408fcb0ff1833e3d26cbc9b04c23a77565814a16/rules/Php70/EregToPcreTransformer.php#L223-L259","documentation":"_ere2pcre_escape() refuses to emit a literal NUL byte: when a pattern character is \"\\x00\", EregToPcreTransformer throws InvalidEregException('a literal null byte in the regex') (EregToPcreTransformer.php:241). A raw null byte cannot appear safely in a PCRE pattern string, so the ereg-to-preg conversion is aborted instead of producing a broken pattern.","triggerScenarios":"Rector (Php70 set / EregToPregMatchRector, target PHP >= 7.0) encounters an ereg-family call whose string-literal pattern contains an actual \"\\x00\" byte — e.g. source written as \"a\\x00b\" (double-quoted, real NUL), or a binary/legacy file where a NUL ended up inside the literal. Applies both in normal atoms and inside character classes, since _ere2pcre_escape() is used for both.","commonSituations":"Code migrated from binary-processing scripts (fixed-width records separated by NUL) where ereg/split was fed \"\\x00\"-separated patterns; literals corrupted by an encoding conversion or editor that inserted NULs; copy-paste of binary data into pattern strings.","solutions":["Remove the raw NUL byte from the literal in the file Rector reported.","If matching a NUL separator is intentional, rewrite that call by hand to str_replace/explode with chr(0) or a preg pattern using an escaped sequence, instead of relying on rector's ereg conversion.","Re-run rector; preflight-scan ereg/split literals for \"\\x00\" (grep -P '\"\\x00\"|\\x00') before bulk migrations."],"exampleFix":"// before - literal NUL byte inside the pattern (shown as \\x00)\nereg(\"a\\x00b\", $subject);\n\n// after - handle binary separator explicitly, no raw NUL in the regex\nexplode(\"\\x00\", $subject); // or preg_match('/a\\x00b/', $subject) written by hand","handlingStrategy":"validation","validationCode":"if (strpos($pattern, \"\\x00\") !== false) {\n    throw new InvalidArgumentException('pattern contains a literal null byte; rewrite the call without ereg');\n}","typeGuard":null,"tryCatchPattern":"try {\n    $pcre = $eregToPcreTransformer->transform($pattern, $ignoreCase);\n} catch (InvalidEregException $e) {\n    // raw NUL in the pattern — remove it or handle binary splitting outside regex\n    continue;\n}","preventionTips":["Never embed raw \"\\x00\" bytes in regex literals; use explode/str_replace with chr(0) for NUL-separated data.","Scan migrated files for NUL bytes (grep -P '\\x00') before running rector over them.","Treat binary data and regex as separate concerns during PHP 7 migrations."],"tags":["rector","php","regex","posix-ere","binary","null-byte","migration"],"backgroundTag":"invalid-regex-syntax","analyzedSha":"408fcb0ff1833e3d26cbc9b04c23a77565814a16","analyzedAt":"2026-08-21T05:11:02.643Z","schemaVersion":2},"datasetVersion":"2026-08-21T11:28:35.574Z"}