]> granicus.if.org Git - php/commitdiff
fix #39032 (strcspn() stops on null character)
authorAntony Dovgal <tony2001@php.net>
Wed, 4 Oct 2006 11:12:21 +0000 (11:12 +0000)
committerAntony Dovgal <tony2001@php.net>
Wed, 4 Oct 2006 11:12:21 +0000 (11:12 +0000)
ext/standard/string.c
ext/standard/tests/strings/bug39032.phpt [new file with mode: 0644]

index 9ad5f74e1f2b139e17493710bf679fcc207a57d5..9d148c7e893ca3de1b8fcb37c9aaf478b95c81e3 100644 (file)
@@ -2306,11 +2306,12 @@ PHPAPI int php_u_strcspn(UChar *s1, UChar *s2, UChar *s1_end, UChar *s2_end)
        int codepts;
        UChar32 ch;
 
-       for (i = 0, codepts = 0 ; i < len1 ; codepts++) {
+       for (i = 0, codepts = 0 ; i < len1 ; ) {
                U16_NEXT(s1, i, len1, ch);
-               if (u_memchr32(s2, ch, len2)) {
+               if (!len2 || u_memchr32(s2, ch, len2)) {
                        break;
                }
+               codepts++;
        }
        return codepts;
 }
@@ -2329,7 +2330,7 @@ PHPAPI size_t php_strcspn(char *s1, char *s2, char *s1_end, char *s2_end)
                        if (*spanp == c || p == s1_end) {
                                return p - s1;
                        }
-               } while (spanp++ < s2_end);
+               } while (spanp++ < (s2_end - 1));
                c = *++p;
        }
        /* NOTREACHED */
diff --git a/ext/standard/tests/strings/bug39032.phpt b/ext/standard/tests/strings/bug39032.phpt
new file mode 100644 (file)
index 0000000..dbd39ec
--- /dev/null
@@ -0,0 +1,18 @@
+--TEST--
+Bug #39032 (strcspn() stops on null character)
+--FILE--
+<?php
+
+var_dump(strcspn(chr(0),"x"));
+var_dump(strcspn(chr(0),""));
+var_dump(strcspn(chr(0),"qweqwe"));
+var_dump(strcspn(chr(1),"qweqwe"));
+
+echo "Done\n";
+?>
+--EXPECTF--    
+int(1)
+int(0)
+int(1)
+int(1)
+Done