Commit 2157055eb1c for php.net
commit 2157055eb1c2942adc2b3305148a04e261d6d44c
Author: Gina Peter Banyard <girgias@php.net>
Date: Tue Sep 1 18:30:21 2026 +0100
pcre: reduce scope of variables
diff --git a/ext/pcre/php_pcre.c b/ext/pcre/php_pcre.c
index 4fde4ed5f9b..997c20d1109 100644
--- a/ext/pcre/php_pcre.c
+++ b/ext/pcre/php_pcre.c
@@ -1155,7 +1155,6 @@ PHPAPI void php_pcre_match_impl(pcre_cache_entry *pce, zend_string *subject_str,
uint32_t num_subpats; /* Number of captured subpatterns */
int matched; /* Has anything matched */
zend_string **subpat_names; /* Array for named subpatterns */
- size_t i;
uint32_t subpats_order; /* Order of subpattern matches */
uint32_t offset_capture; /* Capture match offsets: yes/no */
zend_long unmatched_as_null; /* Null non-matches: yes/no */
@@ -1248,7 +1247,7 @@ PHPAPI void php_pcre_match_impl(pcre_cache_entry *pce, zend_string *subject_str,
/* Allocate match sets array and initialize the values. */
if (global && subpats && subpats_order == PREG_PATTERN_ORDER) {
match_sets = safe_emalloc(num_subpats, sizeof(HashTable *), 0);
- for (i=0; i<num_subpats; i++) {
+ for (uint32_t i = 0; i < num_subpats; i++) {
match_sets[i] = zend_new_array(0);
}
}
@@ -1288,7 +1287,7 @@ PHPAPI void php_pcre_match_impl(pcre_cache_entry *pce, zend_string *subject_str,
/* Try to get the list of substrings and display a warning if failed. */
if (UNEXPECTED(offsets[1] < offsets[0])) {
if (match_sets) {
- for (i = 0; i < num_subpats; i++) {
+ for (uint32_t i = 0; i < num_subpats; i++) {
zend_array_destroy(match_sets[i]);
}
efree(match_sets);
@@ -1307,13 +1306,13 @@ PHPAPI void php_pcre_match_impl(pcre_cache_entry *pce, zend_string *subject_str,
if (subpats_order == PREG_PATTERN_ORDER) {
/* For each subpattern, insert it into the appropriate array. */
if (offset_capture) {
- for (i = 0; i < count; i++) {
+ for (int i = 0; i < count; i++) {
add_offset_pair(
match_sets[i], subject, offsets[2*i], offsets[2*i+1],
NULL, unmatched_as_null);
}
} else {
- for (i = 0; i < count; i++) {
+ for (int i = 0; i < count; i++) {
zval val;
populate_match_value(
&val, subject, offsets[2*i], offsets[2*i+1], unmatched_as_null);
@@ -1437,7 +1436,7 @@ PHPAPI void php_pcre_match_impl(pcre_cache_entry *pce, zend_string *subject_str,
/* Add the match sets to the output array and clean up */
if (match_sets) {
if (subpat_names) {
- for (i = 0; i < num_subpats; i++) {
+ for (uint32_t i = 0; i < num_subpats; i++) {
zval wrapper;
ZVAL_ARR(&wrapper, match_sets[i]);
if (subpat_names[i]) {
@@ -1447,7 +1446,7 @@ PHPAPI void php_pcre_match_impl(pcre_cache_entry *pce, zend_string *subject_str,
zend_hash_next_index_insert_new(Z_ARRVAL_P(subpats), &wrapper);
}
} else {
- for (i = 0; i < num_subpats; i++) {
+ for (uint32_t i = 0; i < num_subpats; i++) {
zval wrapper;
ZVAL_ARR(&wrapper, match_sets[i]);
zend_hash_next_index_insert_new(Z_ARRVAL_P(subpats), &wrapper);
@@ -2092,7 +2091,6 @@ static zend_string *php_pcre_replace_array(HashTable *regex,
zend_string *tmp_regex_str;
zend_string *regex_str = zval_get_tmp_string(regex_entry, &tmp_regex_str);
zend_string *replace_entry_str, *tmp_replace_entry_str;
- zval *zv;
/* Get current entry */
while (1) {
@@ -2101,7 +2099,7 @@ static zend_string *php_pcre_replace_array(HashTable *regex,
tmp_replace_entry_str = NULL;
break;
}
- zv = ZEND_HASH_ELEMENT(replace_ht, replace_idx);
+ zval *zv = ZEND_HASH_ELEMENT(replace_ht, replace_idx);
replace_idx++;
if (Z_TYPE_P(zv) != IS_UNDEF) {
replace_entry_str = zval_get_tmp_string(zv, &tmp_replace_entry_str);
@@ -2181,14 +2179,13 @@ static zend_string *php_replace_in_subject_func(zend_string *regex_str, const Ha
return result;
} else {
/* If regex is an array */
- zval *regex_entry;
ZEND_ASSERT(regex_ht != NULL);
zend_string_addref(subject);
/* For each entry in the regex array, get the entry */
- ZEND_HASH_FOREACH_VAL(regex_ht, regex_entry) {
+ ZEND_HASH_FOREACH_VAL(regex_ht, zval *regex_entry) {
/* Make sure we're dealing with strings. */
zend_string *tmp_regex_entry_str;
zend_string *regex_entry_str = zval_try_get_tmp_string(regex_entry, &tmp_regex_entry_str);
@@ -2230,10 +2227,6 @@ static size_t php_preg_replace_func_impl(zval *return_value,
}
} else {
/* if subject is an array */
- zval *subject_entry, zv;
- zend_string *string_key;
- zend_ulong num_key;
-
ZEND_ASSERT(subject_ht != NULL);
array_init_size(return_value, zend_hash_num_elements(subject_ht));
@@ -2241,7 +2234,7 @@ static size_t php_preg_replace_func_impl(zval *return_value,
/* For each subject entry, convert it to string, then perform replacement
and add the result to the return_value array. */
- ZEND_HASH_FOREACH_KEY_VAL(subject_ht, num_key, string_key, subject_entry) {
+ ZEND_HASH_FOREACH_KEY_VAL(subject_ht, zend_ulong num_key, zend_string *string_key, zval *subject_entry) {
zend_string *tmp_subject_entry_str;
zend_string *subject_entry_str = zval_try_get_tmp_string(subject_entry, &tmp_subject_entry_str);
if (UNEXPECTED(subject_entry_str == NULL)) {
@@ -2252,6 +2245,7 @@ static size_t php_preg_replace_func_impl(zval *return_value,
regex_str, regex_ht, fci, fcc, subject_entry_str, limit_val, &replace_count, flags);
if (result != NULL) {
/* Add to return array */
+ zval zv;
ZVAL_STR(&zv, result);
if (string_key) {
zend_hash_add_new(return_value_ht, string_key, &zv);
@@ -2301,10 +2295,6 @@ static void _preg_replace_common(
}
} else {
/* if subject is an array */
- zval *subject_entry, zv;
- zend_string *string_key;
- zend_ulong num_key;
-
ZEND_ASSERT(subject_ht != NULL);
array_init_size(return_value, zend_hash_num_elements(subject_ht));
@@ -2312,7 +2302,7 @@ static void _preg_replace_common(
/* For each subject entry, convert it to string, then perform replacement
and add the result to the return_value array. */
- ZEND_HASH_FOREACH_KEY_VAL(subject_ht, num_key, string_key, subject_entry) {
+ ZEND_HASH_FOREACH_KEY_VAL(subject_ht, zend_ulong num_key, zend_string *string_key, zval *subject_entry) {
old_replace_count = replace_count;
zend_string *tmp_subject_entry_str;
zend_string *subject_entry_str = zval_get_tmp_string(subject_entry, &tmp_subject_entry_str);
@@ -2322,6 +2312,7 @@ static void _preg_replace_common(
if (result != NULL) {
if (!is_filter || replace_count > old_replace_count) {
/* Add to return array */
+ zval zv;
ZVAL_STR(&zv, result);
if (string_key) {
zend_hash_add_new(return_value_ht, string_key, &zv);
@@ -2434,9 +2425,9 @@ PHP_FUNCTION(preg_replace_callback)
/* {{{ Perform Perl-style regular expression replacement using replacement callback. */
PHP_FUNCTION(preg_replace_callback_array)
{
- zval *replace, *zcount = NULL;
+ zval *zcount = NULL;
HashTable *pattern, *subject_ht;
- zend_string *subject_str, *str_idx_regex;
+ zend_string *subject_str;
zend_long limit = -1, flags = 0;
size_t replace_count = 0;
@@ -2456,7 +2447,7 @@ PHP_FUNCTION(preg_replace_callback_array)
GC_TRY_ADDREF(subject_str);
}
- ZEND_HASH_FOREACH_STR_KEY_VAL(pattern, str_idx_regex, replace) {
+ ZEND_HASH_FOREACH_STR_KEY_VAL(pattern, zend_string *str_idx_regex, zval *replace) {
if (!str_idx_regex) {
zend_argument_type_error(1, "must contain only string patterns as keys");
goto error;
@@ -2924,12 +2915,9 @@ PHP_FUNCTION(preg_grep)
PHPAPI void php_pcre_grep_impl(pcre_cache_entry *pce, zval *input, zval *return_value, zend_long flags) /* {{{ */
{
- zval *entry; /* An entry in the input array */
uint32_t num_subpats; /* Number of captured subpatterns */
int count; /* Count of matched subpatterns */
uint32_t options; /* Execution options */
- zend_string *string_key;
- zend_ulong num_key;
bool invert; /* Whether to return non-matching
entries */
bool old_mdata_used;
@@ -2960,7 +2948,7 @@ PHPAPI void php_pcre_grep_impl(pcre_cache_entry *pce, zval *input, zval *return
options = (pce->compile_options & PCRE2_UTF) ? 0 : PCRE2_NO_UTF_CHECK;
/* Go through the input array */
- ZEND_HASH_FOREACH_KEY_VAL(Z_ARRVAL_P(input), num_key, string_key, entry) {
+ ZEND_HASH_FOREACH_KEY_VAL(Z_ARRVAL_P(input), zend_ulong num_key, zend_string *string_key, zval *entry) {
zend_string *tmp_subject_str;
zend_string *subject_str = zval_get_tmp_string(entry, &tmp_subject_str);