diff options
author | Joe Watkins <krakjoe@php.net> | 2017-01-06 05:46:10 +0000 |
---|---|---|
committer | Joe Watkins <krakjoe@php.net> | 2017-01-06 05:48:31 +0000 |
commit | ee6aaee83a4a72791b0750c495538dfcf6ce7ce8 (patch) | |
tree | 9b748bf5d0113902c0227d43f643a9f6458596fb /ext/pcre/php_pcre.c | |
parent | 1db0569514858523e83a7ddeee36f51b5f1c2266 (diff) | |
parent | 5ca664abbf839708950d57c962438d8878459ec5 (diff) | |
download | php-git-ee6aaee83a4a72791b0750c495538dfcf6ce7ce8.tar.gz |
Merge branch 'pull-request/1303'
* pull-request/1303:
Distinguish between unmatched subpatterns and empty matches in preg_*()
news entry for PR #1303
Diffstat (limited to 'ext/pcre/php_pcre.c')
-rw-r--r-- | ext/pcre/php_pcre.c | 64 |
1 files changed, 48 insertions, 16 deletions
diff --git a/ext/pcre/php_pcre.c b/ext/pcre/php_pcre.c index b7d2b6f119..0890e5247e 100644 --- a/ext/pcre/php_pcre.c +++ b/ext/pcre/php_pcre.c @@ -646,7 +646,11 @@ static inline void add_offset_pair(zval *result, char *str, int len, int offset, array_init_size(&match_pair, 2); /* Add (match, offset) to the return value */ - ZVAL_STRINGL(&tmp, str, len); + if (offset < 0) { /* unset substring */ + ZVAL_NULL(&tmp); + } else { + ZVAL_STRINGL(&tmp, str, len); + } zend_hash_next_index_insert_new(Z_ARRVAL(match_pair), &tmp); ZVAL_LONG(&tmp, offset); zend_hash_next_index_insert_new(Z_ARRVAL(match_pair), &tmp); @@ -847,8 +851,12 @@ PHPAPI void php_pcre_match_impl(pcre_cache_entry *pce, char *subject, int subjec } } else { for (i = 0; i < count; i++) { - add_next_index_stringl(&match_sets[i], (char *)stringlist[i], - offsets[(i<<1)+1] - offsets[i<<1]); + if (offsets[i<<1] < 0) { /* unset substring */ + add_next_index_null(&match_sets[i]); + } else { + add_next_index_stringl(&match_sets[i], (char *)stringlist[i], + offsets[(i<<1)+1] - offsets[i<<1]); + } } } /* Add MARK, if available */ @@ -861,11 +869,11 @@ PHPAPI void php_pcre_match_impl(pcre_cache_entry *pce, char *subject, int subjec /* * If the number of captured subpatterns on this run is * less than the total possible number, pad the result - * arrays with empty strings. + * arrays with NULLs. */ if (count < num_subpats) { for (; i < num_subpats; i++) { - add_next_index_string(&match_sets[i], ""); + add_next_index_null(&match_sets[i]); } } } else { @@ -882,11 +890,19 @@ PHPAPI void php_pcre_match_impl(pcre_cache_entry *pce, char *subject, int subjec } else { for (i = 0; i < count; i++) { if (subpat_names[i]) { - add_assoc_stringl(&result_set, subpat_names[i], (char *)stringlist[i], + if (offsets[i<<1] < 0) { /* unset substring */ + add_assoc_null(&result_set, subpat_names[i]); + } else { + add_assoc_stringl(&result_set, subpat_names[i], (char *)stringlist[i], + offsets[(i<<1)+1] - offsets[i<<1]); + } + } + if (offsets[i<<1] < 0) { /* unset substring */ + add_next_index_null(&result_set); + } else { + add_next_index_stringl(&result_set, (char *)stringlist[i], offsets[(i<<1)+1] - offsets[i<<1]); } - add_next_index_stringl(&result_set, (char *)stringlist[i], - offsets[(i<<1)+1] - offsets[i<<1]); } } } else { @@ -897,8 +913,12 @@ PHPAPI void php_pcre_match_impl(pcre_cache_entry *pce, char *subject, int subjec } } else { for (i = 0; i < count; i++) { - add_next_index_stringl(&result_set, (char *)stringlist[i], - offsets[(i<<1)+1] - offsets[i<<1]); + if (offsets[i<<1] < 0) { /* unset substring */ + add_next_index_null(&result_set); + } else { + add_next_index_stringl(&result_set, (char *)stringlist[i], + offsets[(i<<1)+1] - offsets[i<<1]); + } } } } @@ -921,11 +941,19 @@ PHPAPI void php_pcre_match_impl(pcre_cache_entry *pce, char *subject, int subjec } else { for (i = 0; i < count; i++) { if (subpat_names[i]) { - add_assoc_stringl(subpats, subpat_names[i], (char *)stringlist[i], - offsets[(i<<1)+1] - offsets[i<<1]); + if (offsets[i<<1] < 0) { /* unset substring */ + add_assoc_null(subpats, subpat_names[i]); + } else { + add_assoc_stringl(subpats, subpat_names[i], (char *)stringlist[i], + offsets[(i<<1)+1] - offsets[i<<1]); + } + } + if (offsets[i<<1] < 0) { /* unset substring */ + add_next_index_null(subpats); + } else { + add_next_index_stringl(subpats, (char *)stringlist[i], + offsets[(i<<1)+1] - offsets[i<<1]); } - add_next_index_stringl(subpats, (char *)stringlist[i], - offsets[(i<<1)+1] - offsets[i<<1]); } } } else { @@ -937,8 +965,12 @@ PHPAPI void php_pcre_match_impl(pcre_cache_entry *pce, char *subject, int subjec } } else { for (i = 0; i < count; i++) { - add_next_index_stringl(subpats, (char *)stringlist[i], - offsets[(i<<1)+1] - offsets[i<<1]); + if (offsets[i<<1] < 0) { /* unset substring */ + add_next_index_null(subpats); + } else { + add_next_index_stringl(subpats, (char *)stringlist[i], + offsets[(i<<1)+1] - offsets[i<<1]); + } } } } |