34
34
#include "php_globals.h"
35
35
36
36
int php_tag_find (char * tag , int len , char * set );
37
+ static inline char * php_memnstr (char * haystack , char * needle , int needle_len , char * end );
37
38
38
39
/* this is read-only, so it's ok */
39
40
static char hexconvtab [] = "0123456789abcdef" ;
@@ -602,6 +603,8 @@ PHP_FUNCTION(strpos)
602
603
pval * * haystack , * * needle , * * OFFSET ;
603
604
int offset = 0 ;
604
605
char * found = NULL ;
606
+ char * endp ;
607
+ char * startp ;
605
608
606
609
switch (ARG_COUNT (ht )) {
607
610
case 2 :
@@ -615,25 +618,41 @@ PHP_FUNCTION(strpos)
615
618
}
616
619
convert_to_long_ex (OFFSET );
617
620
offset = (* OFFSET )-> value .lval ;
621
+ if (offset < 0 ) {
622
+ php_error (E_WARNING ,"offset not contained in string" );
623
+ RETURN_FALSE ;
624
+ }
618
625
break ;
619
626
default :
620
627
WRONG_PARAM_COUNT ;
621
628
}
629
+
622
630
convert_to_string_ex (haystack );
631
+
623
632
if (offset > (* haystack )-> value .str .len ) {
624
633
php_error (E_WARNING ,"offset not contained in string" );
625
634
RETURN_FALSE ;
626
635
}
627
636
637
+ startp = (* haystack )-> value .str .val ;
638
+ startp += offset ;
639
+
640
+ endp = (* haystack )-> value .str .val ;
641
+ endp += (* haystack )-> value .str .len ;
642
+
628
643
if ((* needle )-> type == IS_STRING ) {
629
644
if ((* needle )-> value .str .len == 0 ) {
630
645
php_error (E_WARNING ,"Empty delimiter" );
631
646
RETURN_FALSE ;
632
647
}
633
- found = strstr (( * haystack )-> value .str .val + offset , (* needle )-> value .str .val );
648
+ found = php_memnstr ( startp , ( * needle )-> value .str .val , (* needle )-> value .str .len , endp );
634
649
} else {
650
+ char buf ;
651
+
635
652
convert_to_long_ex (needle );
636
- found = strchr ((* haystack )-> value .str .val + offset , (char ) (* needle )-> value .lval );
653
+ buf = (char ) (* needle )-> value .lval ;
654
+
655
+ found = php_memnstr (startp , & buf , 1 , endp );
637
656
}
638
657
639
658
if (found ) {
0 commit comments