Skip to content

Instantly share code, notes, and snippets.

@ju1ius
Last active February 22, 2021 15:52
Show Gist options
  • Save ju1ius/89fb8b19aa32f15da2cc18cd4006ae42 to your computer and use it in GitHub Desktop.
Save ju1ius/89fb8b19aa32f15da2cc18cd4006ae42 to your computer and use it in GitHub Desktop.
Testing PREG_NO_UTF8_CHECK
#!/bin/bash
if [[ ! -d "$1" ]]; then
echo "Not a directory: $1"
exit 1
fi
find "$1" -type f | while read name
do
./php-src/sapi/cli/php ./words.php "$2" < "$name" >/dev/null
if [[ $? != 0 ]]; then
echo "Crashed on: $name"
exit 255
fi
done
echo "Great success!"
exit 0
$ ./php-src/sapi/cli/php ./words.php < ./php-src/sapi/cli/php > ./words.txt
$ tail -5 ./words.txt
===============================================================================
DONE, 326 errors
> PREG_INTERNAL_ERROR (326)
===============================================================================
$ ./php-src/sapi/cli/php ./words.php '(\p{L})(\1)+' < ./php-src/sapi/cli/php > ./words.txt
$ tail -4 ./words.txt
===============================================================================
DONE, no errors
===============================================================================
<?php
(new Program($argv))->execute();
class Program
{
private $errors = [
PREG_NO_ERROR => ['name' => "PREG_NO_ERROR", 'count' => 0],
PREG_INTERNAL_ERROR => ['name' => "PREG_INTERNAL_ERROR", 'count' => 0],
PREG_BACKTRACK_LIMIT_ERROR => ['name' => "PREG_BACKTRACK_LIMIT_ERROR", 'count' => 0],
PREG_RECURSION_LIMIT_ERROR => ['name' => "PREG_RECURSION_LIMIT_ERROR", 'count' => 0],
PREG_BAD_UTF8_ERROR => ['name' => "PREG_BAD_UTF8_ERROR", 'count' => 0],
PREG_BAD_UTF8_OFFSET_ERROR => ['name' => "PREG_BAD_UTF8_OFFSET_ERROR", 'count' => 0],
];
private $errcount = 0;
public function __construct(array $argv)
{
$pattern = empty($argv[1]) ? '\w+' : $argv[1];
$this->pattern = sprintf('/%s/u', $pattern);
}
public function execute()
{
$this->printHeader();
do {
$str = fread(STDIN, 4096);
$res = preg_match_all($this->pattern, $str, $matches, PREG_NO_UTF8_CHECK);
if ($res === false) {
$this->errcount++;
$this->errors[preg_last_error()]['count']++;
} elseif ($res) {
echo implode(PHP_EOL, $matches[0]), PHP_EOL;
}
} while(!feof(STDIN));
$this->printFooter();
}
private function printHeader()
{
echo 'Searching for: ', $this->pattern, PHP_EOL;
echo str_repeat('=', 79), PHP_EOL;
}
private function printFooter()
{
echo str_repeat(PHP_EOL, 3);
echo str_repeat('=', 79), PHP_EOL;
echo 'DONE, ', $this->errcount ?: 'no', ' errors', PHP_EOL;
if ($this->errcount) {
$errors = array_filter($this->errors, function ($info) { return $info['count']; });
$errors = array_map(function ($info) {
return sprintf('%s (%d)', $info['name'], $info['count']);
}, $errors);
echo implode(PHP_EOL, $errors), PHP_EOL;
}
echo str_repeat('=', 79), PHP_EOL;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment