[php-src] PR #21260: Fix preg_grep() returning partial array instead of false on error
| From: | lacatoire | Date: | Thu, 19 Feb 2026 22:12:19 +0000 |
| Subject: | [php-src] PR #21260: Fix preg_grep() returning partial array instead of false on error | ||
| Groups: | php.git-pulls | ||
| Request: | Send a blank email to git-pulls+get-34963@lists.php.net to get a copy of this message | ||
Pull Request: https://github.com/php/php-src/pull/21260
Author: lacatoire
## Summary
preg_grep() returns a partial result array instead of false when a PCRE
execution error occurs (e.g. malformed UTF-8 input with /u modifier). This is
inconsistent with all other preg_* functions which return false on
execution errors.
For example:
```php
preg_match('/.*/u', hex2bin('ff')); // false (correct)
preg_grep('/.*/u', [hex2bin('ff')]); // [] (should be false)
preg_grep('/.*/u', ['foo', hex2bin('ff'), 'bar']); //
['foo'] (should be false)
```
After the error, preg_grep() also silently stops processing remaining array entries,
returning only the entries matched before the error — which is misleading.
## Fix
After the match loop in php_pcre_grep_impl(), check PCRE_G(error_code). If
an error occurred, destroy the partial result array and return false.
This follows the same pattern used by php_pcre_match_impl() (preg_match).
## Test
Added ext/pcre/tests/preg_grep_error_utf8.phpt covering:
- Consistency with preg_match() behavior
- Single invalid entry
- Valid entries before an invalid one (no partial array)
- PREG_GREP_INVERT flag with error
- Normal operation still returns an array
Fixes GH-11936