@@ -540,7 +540,11 @@ static inline void add_offset_pair(zval *result, char *str, int len, int offset,
540
540
array_init_size (& match_pair , 2 );
541
541
542
542
/* 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
+ }
544
548
zend_hash_next_index_insert_new (Z_ARRVAL (match_pair ), & tmp );
545
549
ZVAL_LONG (& tmp , offset );
546
550
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
741
745
}
742
746
} else {
743
747
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
+ }
746
754
}
747
755
}
748
756
/* Add MARK, if available */
@@ -755,11 +763,11 @@ PHPAPI void php_pcre_match_impl(pcre_cache_entry *pce, char *subject, int subjec
755
763
/*
756
764
* If the number of captured subpatterns on this run is
757
765
* less than the total possible number, pad the result
758
- * arrays with empty strings .
766
+ * arrays with NULLs .
759
767
*/
760
768
if (count < num_subpats ) {
761
769
for (; i < num_subpats ; i ++ ) {
762
- add_next_index_string (& match_sets [i ], "" );
770
+ add_next_index_null (& match_sets [i ]);
763
771
}
764
772
}
765
773
} else {
@@ -776,11 +784,19 @@ PHPAPI void php_pcre_match_impl(pcre_cache_entry *pce, char *subject, int subjec
776
784
} else {
777
785
for (i = 0 ; i < count ; i ++ ) {
778
786
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 ],
780
798
offsets [(i <<1 )+ 1 ] - offsets [i <<1 ]);
781
799
}
782
- add_next_index_stringl (& result_set , (char * )stringlist [i ],
783
- offsets [(i <<1 )+ 1 ] - offsets [i <<1 ]);
784
800
}
785
801
}
786
802
} else {
@@ -791,8 +807,12 @@ PHPAPI void php_pcre_match_impl(pcre_cache_entry *pce, char *subject, int subjec
791
807
}
792
808
} else {
793
809
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
+ }
796
816
}
797
817
}
798
818
}
@@ -815,11 +835,19 @@ PHPAPI void php_pcre_match_impl(pcre_cache_entry *pce, char *subject, int subjec
815
835
} else {
816
836
for (i = 0 ; i < count ; i ++ ) {
817
837
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 ]);
820
850
}
821
- add_next_index_stringl (subpats , (char * )stringlist [i ],
822
- offsets [(i <<1 )+ 1 ] - offsets [i <<1 ]);
823
851
}
824
852
}
825
853
} else {
@@ -831,8 +859,12 @@ PHPAPI void php_pcre_match_impl(pcre_cache_entry *pce, char *subject, int subjec
831
859
}
832
860
} else {
833
861
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
+ }
836
868
}
837
869
}
838
870
}
0 commit comments