Skip to content

Commit 8fdef98

Browse files
andrewnesterweltling
authored andcommitted
Fixed #75539 and #74183 - preg_last_error not returning error code after error
1 parent 702ef27 commit 8fdef98

File tree

3 files changed

+36
-0
lines changed

3 files changed

+36
-0
lines changed

ext/pcre/php_pcre.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -356,6 +356,7 @@ PHPAPI pcre_cache_entry* pcre_get_compiled_regex_cache(zend_string *regex)
356356
#endif
357357
php_error_docref(NULL, E_WARNING,
358358
p < ZSTR_VAL(regex) + ZSTR_LEN(regex) ? "Null byte in regex" : "Empty regular expression");
359+
pcre_handle_exec_error(PCRE_ERROR_INTERNAL);
359360
return NULL;
360361
}
361362

@@ -369,6 +370,7 @@ PHPAPI pcre_cache_entry* pcre_get_compiled_regex_cache(zend_string *regex)
369370
}
370371
#endif
371372
php_error_docref(NULL,E_WARNING, "Delimiter must not be alphanumeric or backslash");
373+
pcre_handle_exec_error(PCRE_ERROR_INTERNAL);
372374
return NULL;
373375
}
374376

@@ -419,6 +421,7 @@ PHPAPI pcre_cache_entry* pcre_get_compiled_regex_cache(zend_string *regex)
419421
} else {
420422
php_error_docref(NULL,E_WARNING, "No ending matching delimiter '%c' found", delimiter);
421423
}
424+
pcre_handle_exec_error(PCRE_ERROR_INTERNAL);
422425
return NULL;
423426
}
424427

@@ -467,6 +470,7 @@ PHPAPI pcre_cache_entry* pcre_get_compiled_regex_cache(zend_string *regex)
467470
} else {
468471
php_error_docref(NULL,E_WARNING, "Null byte in regex");
469472
}
473+
pcre_handle_exec_error(PCRE_ERROR_INTERNAL);
470474
efree(pattern);
471475
#if HAVE_SETLOCALE
472476
if (key != regex) {
@@ -497,6 +501,7 @@ PHPAPI pcre_cache_entry* pcre_get_compiled_regex_cache(zend_string *regex)
497501
}
498502
#endif
499503
php_error_docref(NULL,E_WARNING, "Compilation failed: %s at offset %d", error, erroffset);
504+
pcre_handle_exec_error(PCRE_ERROR_INTERNAL);
500505
efree(pattern);
501506
if (tables) {
502507
pefree((void*)tables, 1);
@@ -528,6 +533,7 @@ PHPAPI pcre_cache_entry* pcre_get_compiled_regex_cache(zend_string *regex)
528533
}
529534
if (error != NULL) {
530535
php_error_docref(NULL, E_WARNING, "Error while studying pattern");
536+
pcre_handle_exec_error(PCRE_ERROR_INTERNAL);
531537
}
532538
} else {
533539
extra = NULL;
@@ -564,6 +570,7 @@ PHPAPI pcre_cache_entry* pcre_get_compiled_regex_cache(zend_string *regex)
564570
}
565571
#endif
566572
php_error_docref(NULL, E_WARNING, "Internal pcre_fullinfo() error %d", rc);
573+
pcre_handle_exec_error(PCRE_ERROR_INTERNAL);
567574
return NULL;
568575
}
569576

@@ -575,6 +582,7 @@ PHPAPI pcre_cache_entry* pcre_get_compiled_regex_cache(zend_string *regex)
575582
}
576583
#endif
577584
php_error_docref(NULL, E_WARNING, "Internal pcre_fullinfo() error %d", rc);
585+
pcre_handle_exec_error(PCRE_ERROR_INTERNAL);
578586
return NULL;
579587
}
580588

ext/pcre/tests/bug74183.phpt

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
--TEST--
2+
Bug #74183 - preg_last_error not returning error code after error
3+
--FILE--
4+
<?php
5+
6+
$sRegex = "/([A-Z]|[a-z]|[0-9]| |Ñ|ñ|!|&quot;|%|&amp;|'|´|-|:|;|>|=|&lt;|@|_|,|\{|\}|`|~|á|é|í|ó|ú|Á|É|Í|Ó|Ú|ü|Ü){1,300}/";
7+
$sTest = "Hello world";
8+
9+
var_dump(preg_match($sRegex, $sTest));
10+
var_dump(preg_last_error() === \PREG_INTERNAL_ERROR);
11+
?>
12+
--EXPECTF--
13+
Warning: preg_match(): Compilation failed: regular expression is too large at offset %s in %s on line %s
14+
bool(false)
15+
bool(true)

ext/pcre/tests/bug75539.phpt

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
--TEST--
2+
Bug #75539 - Recursive call errors are not reported by preg_last_error()
3+
--FILE--
4+
<?php
5+
6+
var_dump(preg_match('/((?1)?z)/', ''));
7+
var_dump(preg_last_error() === \PREG_INTERNAL_ERROR);
8+
9+
?>
10+
--EXPECTF--
11+
Warning: preg_match(): Compilation failed: recursive call could loop indefinitely at offset %s in %s on line %s
12+
bool(false)
13+
bool(true)

0 commit comments

Comments
 (0)