]> granicus.if.org Git - php/commitdiff
Making strrchr() binary safe.
authorAndrey Hristov <andrey@php.net>
Wed, 2 Oct 2002 18:58:09 +0000 (18:58 +0000)
committerAndrey Hristov <andrey@php.net>
Wed, 2 Oct 2002 18:58:09 +0000 (18:58 +0000)
Test case added.

ext/standard/string.c
ext/standard/tests/strings/002.phpt [new file with mode: 0644]

index 540908db960333cb7c48713409b341c9f4c1f0b0..f3d08a6bcb6118c8de3d3629a547965b0d15a893 100644 (file)
@@ -1487,6 +1487,7 @@ PHP_FUNCTION(strrchr)
 {
        zval **haystack, **needle;
        char *found = NULL;
+       long found_offset;
        
        if (ZEND_NUM_ARGS() != 2 || zend_get_parameters_ex(2, &haystack, &needle) ==
                FAILURE) {
@@ -1503,7 +1504,8 @@ PHP_FUNCTION(strrchr)
        }
 
        if (found) {
-               RETURN_STRING(found, 1);
+               found_offset = found - Z_STRVAL_PP(haystack);
+               RETURN_STRINGL(found, Z_STRLEN_PP(haystack) - found_offset, 1);
        } else {
                RETURN_FALSE;
        }
diff --git a/ext/standard/tests/strings/002.phpt b/ext/standard/tests/strings/002.phpt
new file mode 100644 (file)
index 0000000..e13593d
--- /dev/null
@@ -0,0 +1,13 @@
+--TEST--
+Test whether strstr() and strrchr() are binary safe.
+--POST--
+--GET--
+--FILE--
+<?php
+$s = "alabala nica".chr(0)."turska panica";
+var_dump(strlen(strstr($s, "nic")));
+var_dump(strlen(strrchr($s," nic")));
+?>
+--EXPECT--
+int(18)
+int(19)
\ No newline at end of file