Missing error check after regex #1

Closed
opened 2026-01-24 11:24:42 +01:00 by admin · 1 comment
Owner

Originally created by @JimmyAx on GitHub (Aug 7, 2016).

An error check is missing after doing a preg_match() here. The code just ignores any errors, such as exceeding pcre.backtrack_limit. This is also true for the current master branch.
Found the missing error check when getting this Drupal error.

Something like this should be added after. Stolen from the Symfony YAML parser.

if (preg_match("/\A.*^\s*((abstract|final)\s+)?class\s+{$this->shortClassName}\s+/sm", $contents, $matches)) {
    $contents = $matches[0];
}
switch (preg_last_error()) {
    case PREG_INTERNAL_ERROR:
        $error = 'Internal PCRE error.';
        break;
    case PREG_BACKTRACK_LIMIT_ERROR:
        $error = 'pcre.backtrack_limit reached.';
        break;
    case PREG_RECURSION_LIMIT_ERROR:
        $error = 'pcre.recursion_limit reached.';
        break;
    case PREG_BAD_UTF8_ERROR:
        $error = 'Malformed UTF-8 data.';
        break;
    case PREG_BAD_UTF8_OFFSET_ERROR:
        $error = 'Offset doesn\'t correspond to the begin of a valid UTF-8 code point.';
        break;
    default:
        $error = FALSE;
}

if ($error) throw new \Exception("Wow we did it! $error");
Originally created by @JimmyAx on GitHub (Aug 7, 2016). An error check is missing after doing a [preg_match() here](https://github.com/doctrine/common/blob/0009b8f0d4a917aabc971fb089eba80e872f83f9/lib/Doctrine/Common/Reflection/StaticReflectionParser.php#L134). The code just ignores any errors, such as exceeding `pcre.backtrack_limit`. This is also true for the current master branch. Found the missing error check when getting [this Drupal error](https://www.drupal.org/node/2716827). Something like this should be added after. Stolen from the [Symfony YAML parser](https://github.com/symfony/symfony/blob/02f59fe20b52dc95e022bd010b747a2be36a27f9/src/Symfony/Component/Yaml/Parser.php#L299). ``` php if (preg_match("/\A.*^\s*((abstract|final)\s+)?class\s+{$this->shortClassName}\s+/sm", $contents, $matches)) { $contents = $matches[0]; } switch (preg_last_error()) { case PREG_INTERNAL_ERROR: $error = 'Internal PCRE error.'; break; case PREG_BACKTRACK_LIMIT_ERROR: $error = 'pcre.backtrack_limit reached.'; break; case PREG_RECURSION_LIMIT_ERROR: $error = 'pcre.recursion_limit reached.'; break; case PREG_BAD_UTF8_ERROR: $error = 'Malformed UTF-8 data.'; break; case PREG_BAD_UTF8_OFFSET_ERROR: $error = 'Offset doesn\'t correspond to the begin of a valid UTF-8 code point.'; break; default: $error = FALSE; } if ($error) throw new \Exception("Wow we did it! $error"); ```
admin closed this issue 2026-01-24 11:24:42 +01:00
Author
Owner

@derrabus commented on GitHub (Aug 1, 2023):

I'm closing this issue since we're not actively maintaining this library anymore.

@derrabus commented on GitHub (Aug 1, 2023): I'm closing this issue since we're not actively maintaining this library anymore.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/reflection#1