Skip to content

Commit

Permalink
civix inspect:fun - More guarded handling of regex
Browse files Browse the repository at this point in the history
  • Loading branch information
totten committed Feb 26, 2025
1 parent 360154f commit 9be97fb
Showing 1 changed file with 39 additions and 4 deletions.
43 changes: 39 additions & 4 deletions src/CRM/CivixBundle/Command/InspectFunctionCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,13 @@ protected function configure() {

protected function execute(InputInterface $input, OutputInterface $output): int {
$functionNamePattern = $input->getOption('name');
if ($functionNamePattern) {
$this->assertRegex($functionNamePattern, '--name');
}
$codePattern = $input->getOption('code');
if ($codePattern) {
$this->assertRegex($codePattern, '--code');
}
$printer = [$this, 'printMatch'];
if ($input->getOption('files-with-matches')) {
$printer = [$this, 'printFileName'];
Expand Down Expand Up @@ -82,15 +88,26 @@ protected function printMatch($file, ?string $functionName, string $signature, s
}

/**
* @param string $code
* @param $hi
* @param $matches
* Split a block of $code into highlighted and non-highlighted sections.
*
* @param string $code
* The code to search/highlight
* @param string $hi
* Regex identifying the expressions to highlight
* @return array
*/
protected function splitHighlights(string $code, $hi): array {
$buf = $code;
$hiPat = $hi[0] . '^(.*)(' . substr($hi, 1, -1) . ')' . $hi[0] . 'msU';
$delimQuot = preg_quote($hi[0], ';');
if (preg_match(';' . $delimQuot . '([a-zA-Z]+)$;', $hi, $m)) {
$modifiers = $m[1];
$hi = substr($hi, 0, -1 * strlen($modifiers));
}
else {
$modifiers = '';
}

$hiPat = $hi[0] . '^(.*)(' . substr($hi, 1, -1) . ')' . $hi[0] . 'msU' . $modifiers;
$hiParts = [];
while (!empty($buf)) {
if (preg_match($hiPat, $buf, $matches)) {
Expand All @@ -106,4 +123,22 @@ protected function splitHighlights(string $code, $hi): array {
return $hiParts;
}

/**
* Assert that $regex is a plausible-looking regular expression.
*
* @param string $regex
* @param string $regexOption
*/
protected function assertRegex(string $regex, string $regexOption): void {
$delim = $regex[0];
$delimQuote = preg_quote($delim, ';');
$allowDelim = '/|:;,.#';
if (strpos($allowDelim, $delim) === FALSE) {
throw new \Exception("Option \"$regexOption\" should have a symbolic delimiter, such as: $allowDelim");
}
if (!preg_match(';^' . $delimQuote . '.*' . $delimQuote . '[a-zA-Z]*$;', $regex)) {
throw new \Exception("Option \"$regexOption\" should be well-formed");
}
}

}

0 comments on commit 9be97fb

Please sign in to comment.