Skip to content

Commit db3cf21

Browse files
author
Thies C. Arntzen
committed
@- strpos() is now binary-safe. (Thies)
1 parent a9a5f24 commit db3cf21

File tree

1 file changed

+21
-2
lines changed

1 file changed

+21
-2
lines changed

ext/standard/string.c

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
#include "php_globals.h"
3535

3636
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);
3738

3839
/* this is read-only, so it's ok */
3940
static char hexconvtab[] = "0123456789abcdef";
@@ -602,6 +603,8 @@ PHP_FUNCTION(strpos)
602603
pval **haystack, **needle, **OFFSET;
603604
int offset = 0;
604605
char *found = NULL;
606+
char *endp;
607+
char *startp;
605608

606609
switch(ARG_COUNT(ht)) {
607610
case 2:
@@ -615,25 +618,41 @@ PHP_FUNCTION(strpos)
615618
}
616619
convert_to_long_ex(OFFSET);
617620
offset = (*OFFSET)->value.lval;
621+
if (offset < 0) {
622+
php_error(E_WARNING,"offset not contained in string");
623+
RETURN_FALSE;
624+
}
618625
break;
619626
default:
620627
WRONG_PARAM_COUNT;
621628
}
629+
622630
convert_to_string_ex(haystack);
631+
623632
if (offset > (*haystack)->value.str.len) {
624633
php_error(E_WARNING,"offset not contained in string");
625634
RETURN_FALSE;
626635
}
627636

637+
startp = (*haystack)->value.str.val;
638+
startp+= offset;
639+
640+
endp = (*haystack)->value.str.val;
641+
endp+= (*haystack)->value.str.len;
642+
628643
if ((*needle)->type == IS_STRING) {
629644
if ((*needle)->value.str.len==0) {
630645
php_error(E_WARNING,"Empty delimiter");
631646
RETURN_FALSE;
632647
}
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);
634649
} else {
650+
char buf;
651+
635652
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);
637656
}
638657

639658
if (found) {

0 commit comments

Comments
 (0)