Skip to content

Commit 1b6afc0

Browse files
ondrejmirtesclaude
andcommitted
Throw Nette's RegexpException from the native isValidIdentifier()
The twin matches through Nette's Strings::match(), which turns a run-time PCRE failure into a RegexpException carrying RegexpException::MESSAGES[preg_last_error()] and the pattern as the message and the error as the code; the port threw "preg_match() failed" with code 0. Build the same message and code. The identifier pattern was also interned at request time into a static nothing reset; create it as a permanent interned string at MINIT. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_018Fdg8n3eEi4ZoXa4dA2k4A
1 parent ab78d7d commit 1b6afc0

2 files changed

Lines changed: 66 additions & 14 deletions

File tree

‎turbo-ext/src/ConstantArrayType.cpp‎

Lines changed: 45 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,13 @@ bool pt_result_object_create(zval *out, zend_class_entry *ce, zval *trinary, zva
5353
#define PT_CAT_CASE_LOWER 0
5454
#define PT_CAT_CASE_UPPER 1
5555

56-
/* isValidIdentifier()'s pattern (interned once) and the internal
57-
* preg_match() it is matched with (resolved once; the pcre extension
58-
* caches the compiled pattern per request) */
56+
/* isValidIdentifier()'s pattern (a permanent interned string, made at
57+
* MINIT) and the internal preg_match() / preg_last_error() it is matched
58+
* with (internal functions, resolved once; the pcre extension caches the
59+
* compiled pattern per request) */
5960
static zend_string *pt_carr_identifier_regex = nullptr;
6061
static zend_function *pt_carr_preg_match = nullptr;
62+
static zend_function *pt_carr_preg_last_error = nullptr;
6163

6264
namespace phpstanturbo {
6365

@@ -5678,12 +5680,10 @@ class ConstantArrayType
56785680
* frames; false = pending exception */
56795681
[[nodiscard]] static bool isValidIdentifier(zend_string *value, bool &out)
56805682
{
5681-
if (UNEXPECTED(pt_carr_identifier_regex == nullptr)) {
5682-
pt_carr_identifier_regex = zend_string_init_interned(PT_LC("~^(?:[\\\\]?+[a-z_\\x80-\\xFF][0-9a-z_\\x80-\\xFF-]*+)++$~si"), 1);
5683-
}
5684-
if (UNEXPECTED(pt_carr_preg_match == nullptr)) {
5683+
if (UNEXPECTED(pt_carr_preg_match == nullptr || pt_carr_preg_last_error == nullptr)) {
56855684
pt_carr_preg_match = (zend_function *) zend_hash_str_find_ptr(EG(function_table), PT_LC("preg_match"));
5686-
if (UNEXPECTED(pt_carr_preg_match == nullptr)) {
5685+
pt_carr_preg_last_error = (zend_function *) zend_hash_str_find_ptr(EG(function_table), PT_LC("preg_last_error"));
5686+
if (UNEXPECTED(pt_carr_preg_match == nullptr || pt_carr_preg_last_error == nullptr)) {
56875687
zend_throw_error(NULL, "phpstan_turbo: preg_match() is not available");
56885688
return false;
56895689
}
@@ -5697,19 +5697,48 @@ class ConstantArrayType
56975697
return false;
56985698
}
56995699
if (UNEXPECTED(Z_TYPE(matched) != IS_LONG)) {
5700-
/* preg_match() failed (a backtracking/recursion limit) — Nette
5701-
* Strings::match() raises its RegexpException here */
57025700
zval_ptr_dtor(&matched);
5703-
zend_class_entry *exceptionCe = pt_class(PT_CLASS_NETTE_REGEXP_EXCEPTION);
5704-
if (exceptionCe != NULL) {
5705-
zend_throw_exception(exceptionCe, "preg_match() failed", 0);
5706-
}
5701+
throwRegexpException();
57075702
return false;
57085703
}
57095704
out = Z_LVAL(matched) > 0;
57105705
return true;
57115706
}
57125707

5708+
/* preg_match() failed (a backtracking/recursion limit): Nette's
5709+
* Strings::pcre() throws new RegexpException((RegexpException::MESSAGES[$code]
5710+
* ?? 'Unknown error') . ' (pattern: ' . $pattern . ')', $code) with
5711+
* $code = preg_last_error() */
5712+
static void throwRegexpException()
5713+
{
5714+
zend_class_entry *exceptionCe = pt_class(PT_CLASS_NETTE_REGEXP_EXCEPTION);
5715+
if (UNEXPECTED(exceptionCe == NULL)) return;
5716+
zval code;
5717+
zend_call_known_function(pt_carr_preg_last_error, NULL, NULL, &code, 0, NULL, NULL);
5718+
if (UNEXPECTED(EG(exception))) return;
5719+
zend_long codeValue = Z_TYPE(code) == IS_LONG ? Z_LVAL(code) : 0;
5720+
zval_ptr_dtor(&code);
5721+
5722+
const char *reason = "Unknown error";
5723+
size_t reasonLength = sizeof("Unknown error") - 1;
5724+
zend_string *constantName = zend_string_init(PT_LC("MESSAGES"), 0);
5725+
zval *messages = zend_get_class_constant_ex(exceptionCe->name, constantName, exceptionCe, ZEND_FETCH_CLASS_SILENT);
5726+
zend_string_release(constantName);
5727+
if (UNEXPECTED(EG(exception))) return;
5728+
if (messages != NULL && Z_TYPE_P(messages) == IS_ARRAY) {
5729+
zval *message = zend_hash_index_find(Z_ARRVAL_P(messages), (zend_ulong) codeValue);
5730+
if (message != NULL && Z_TYPE_P(message) == IS_STRING) {
5731+
reason = Z_STRVAL_P(message);
5732+
reasonLength = Z_STRLEN_P(message);
5733+
}
5734+
}
5735+
zend_string *text = zend_string_concat3(reason, reasonLength, PT_LC(" (pattern: "), ZSTR_VAL(pt_carr_identifier_regex), ZSTR_LEN(pt_carr_identifier_regex));
5736+
zend_string *full = zend_string_concat2(ZSTR_VAL(text), ZSTR_LEN(text), PT_LC(")"));
5737+
zend_string_release(text);
5738+
zend_throw_exception(exceptionCe, ZSTR_VAL(full), codeValue);
5739+
zend_string_release(full);
5740+
}
5741+
57135742
/* [] for an unsealed shape; else every combination of the values'
57145743
* finite types (an optional key forking a without-variant), built one
57155744
* key at a time and bounded by CALCULATE_SCALARS_LIMIT partial shapes;
@@ -7170,6 +7199,8 @@ static void ZEND_FASTCALL catTraverse(INTERNAL_FUNCTION_PARAMETERS)
71707199

71717200
void pt_register_constant_array_type()
71727201
{
7202+
pt_carr_identifier_regex = zend_string_init_interned(PT_LC("~^(?:[\\\\]?+[a-z_\\x80-\\xFF][0-9a-z_\\x80-\\xFF-]*+)++$~si"), 1);
7203+
71737204
reg::Class cls("PHPStan\\Type\\Constant\\ConstantArrayType");
71747205
ptdecl::ConstantArrayType::declareClass(cls);
71757206
/* the slots PT_CAT_PROP_*: the eight class-body properties first, the

‎turbo-ext/tests/smoke.php‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4321,6 +4321,27 @@ public function getComments(): array
43214321
// kept by PHP collaborators and called after the call returned
43224322
require __DIR__ . '/initializer-escape.php';
43234323

4324+
// ---- lane infra ----
4325+
4326+
// ConstantArrayType::isValidIdentifier() is Nette's Strings::match(): a
4327+
// run-time PCRE failure is its RegexpException with preg_last_error()'s
4328+
// message and code
4329+
$liIdentifierResults = [];
4330+
$liJit = ini_get('pcre.jit');
4331+
$liBacktrackLimit = ini_get('pcre.backtrack_limit');
4332+
ini_set('pcre.jit', '0');
4333+
ini_set('pcre.backtrack_limit', '1');
4334+
foreach (['php' => \PHPStan\Type\Constant\ConstantArrayType::class, 'native' => \PHPStanTurbo\ConstantArrayType::class] as $liSide => $liClass) {
4335+
try {
4336+
$liIdentifierResults[$liSide] = $liClass::isValidIdentifier('abcdef');
4337+
} catch (\Throwable $e) {
4338+
$liIdentifierResults[$liSide] = [get_class($e), $e->getCode(), $e->getMessage()];
4339+
}
4340+
}
4341+
ini_set('pcre.jit', $liJit);
4342+
ini_set('pcre.backtrack_limit', $liBacktrackLimit);
4343+
check($liIdentifierResults['php'] === $liIdentifierResults['native'], 'ConstantArrayType::isValidIdentifier() on a PCRE failure: ' . json_encode($liIdentifierResults));
4344+
43244345
// ---- differential coverage completeness ----
43254346
// Every shadowed class must be exercised by one of the tests/ scripts; the
43264347
// classes not covered above have their own dedicated script.

0 commit comments

Comments
 (0)