Skip to content

Commit 6f33594

Browse files
committed
changed preg_*() to yield NULL instead of '' for unset substrings
1 parent 911366e commit 6f33594

File tree

1 file changed

+48
-16
lines changed

1 file changed

+48
-16
lines changed

ext/pcre/php_pcre.c

Lines changed: 48 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -540,7 +540,11 @@ static inline void add_offset_pair(zval *result, char *str, int len, int offset,
540540
array_init_size(&match_pair, 2);
541541

542542
/* Add (match, offset) to the return value */
543-
ZVAL_STRINGL(&tmp, str, len);
543+
if (offset < 0) { /* unset substring */
544+
ZVAL_NULL(&tmp);
545+
} else {
546+
ZVAL_STRINGL(&tmp, str, len);
547+
}
544548
zend_hash_next_index_insert_new(Z_ARRVAL(match_pair), &tmp);
545549
ZVAL_LONG(&tmp, offset);
546550
zend_hash_next_index_insert_new(Z_ARRVAL(match_pair), &tmp);
@@ -741,8 +745,12 @@ PHPAPI void php_pcre_match_impl(pcre_cache_entry *pce, char *subject, int subjec
741745
}
742746
} else {
743747
for (i = 0; i < count; i++) {
744-
add_next_index_stringl(&match_sets[i], (char *)stringlist[i],
745-
offsets[(i<<1)+1] - offsets[i<<1]);
748+
if (offsets[i<<1] < 0) { /* unset substring */
749+
add_next_index_null(&match_sets[i]);
750+
} else {
751+
add_next_index_stringl(&match_sets[i], (char *)stringlist[i],
752+
offsets[(i<<1)+1] - offsets[i<<1]);
753+
}
746754
}
747755
}
748756
/* Add MARK, if available */
@@ -755,11 +763,11 @@ PHPAPI void php_pcre_match_impl(pcre_cache_entry *pce, char *subject, int subjec
755763
/*
756764
* If the number of captured subpatterns on this run is
757765
* less than the total possible number, pad the result
758-
* arrays with empty strings.
766+
* arrays with NULLs.
759767
*/
760768
if (count < num_subpats) {
761769
for (; i < num_subpats; i++) {
762-
add_next_index_string(&match_sets[i], "");
770+
add_next_index_null(&match_sets[i]);
763771
}
764772
}
765773
} else {
@@ -776,11 +784,19 @@ PHPAPI void php_pcre_match_impl(pcre_cache_entry *pce, char *subject, int subjec
776784
} else {
777785
for (i = 0; i < count; i++) {
778786
if (subpat_names[i]) {
779-
add_assoc_stringl(&result_set, subpat_names[i], (char *)stringlist[i],
787+
if (offsets[i<<1] < 0) { /* unset substring */
788+
add_assoc_null(&result_set, subpat_names[i]);
789+
} else {
790+
add_assoc_stringl(&result_set, subpat_names[i], (char *)stringlist[i],
791+
offsets[(i<<1)+1] - offsets[i<<1]);
792+
}
793+
}
794+
if (offsets[i<<1] < 0) { /* unset substring */
795+
add_next_index_null(&result_set);
796+
} else {
797+
add_next_index_stringl(&result_set, (char *)stringlist[i],
780798
offsets[(i<<1)+1] - offsets[i<<1]);
781799
}
782-
add_next_index_stringl(&result_set, (char *)stringlist[i],
783-
offsets[(i<<1)+1] - offsets[i<<1]);
784800
}
785801
}
786802
} else {
@@ -791,8 +807,12 @@ PHPAPI void php_pcre_match_impl(pcre_cache_entry *pce, char *subject, int subjec
791807
}
792808
} else {
793809
for (i = 0; i < count; i++) {
794-
add_next_index_stringl(&result_set, (char *)stringlist[i],
795-
offsets[(i<<1)+1] - offsets[i<<1]);
810+
if (offsets[i<<1] < 0) { /* unset substring */
811+
add_next_index_null(&result_set);
812+
} else {
813+
add_next_index_stringl(&result_set, (char *)stringlist[i],
814+
offsets[(i<<1)+1] - offsets[i<<1]);
815+
}
796816
}
797817
}
798818
}
@@ -815,11 +835,19 @@ PHPAPI void php_pcre_match_impl(pcre_cache_entry *pce, char *subject, int subjec
815835
} else {
816836
for (i = 0; i < count; i++) {
817837
if (subpat_names[i]) {
818-
add_assoc_stringl(subpats, subpat_names[i], (char *)stringlist[i],
819-
offsets[(i<<1)+1] - offsets[i<<1]);
838+
if (offsets[i<<1] < 0) { /* unset substring */
839+
add_assoc_null(subpats, subpat_names[i]);
840+
} else {
841+
add_assoc_stringl(subpats, subpat_names[i], (char *)stringlist[i],
842+
offsets[(i<<1)+1] - offsets[i<<1]);
843+
}
844+
}
845+
if (offsets[i<<1] < 0) { /* unset substring */
846+
add_next_index_null(subpats);
847+
} else {
848+
add_next_index_stringl(subpats, (char *)stringlist[i],
849+
offsets[(i<<1)+1] - offsets[i<<1]);
820850
}
821-
add_next_index_stringl(subpats, (char *)stringlist[i],
822-
offsets[(i<<1)+1] - offsets[i<<1]);
823851
}
824852
}
825853
} else {
@@ -831,8 +859,12 @@ PHPAPI void php_pcre_match_impl(pcre_cache_entry *pce, char *subject, int subjec
831859
}
832860
} else {
833861
for (i = 0; i < count; i++) {
834-
add_next_index_stringl(subpats, (char *)stringlist[i],
835-
offsets[(i<<1)+1] - offsets[i<<1]);
862+
if (offsets[i<<1] < 0) { /* unset substring */
863+
add_next_index_null(subpats);
864+
} else {
865+
add_next_index_stringl(subpats, (char *)stringlist[i],
866+
offsets[(i<<1)+1] - offsets[i<<1]);
867+
}
836868
}
837869
}
838870
}

0 commit comments

Comments
 (0)