]> granicus.if.org Git - php/commitdiff
substr_compare(): Allow zero length comparison
authordatibbaw <datibbaw@php.net>
Fri, 21 Feb 2014 02:24:52 +0000 (10:24 +0800)
committerTjerk Meesters <datibbaw@php.net>
Fri, 28 Feb 2014 15:45:04 +0000 (23:45 +0800)
Treat zero length comparison as always equal.

ext/standard/string.c
ext/standard/tests/strings/bug33605.phpt
ext/standard/tests/strings/substr_compare.phpt

index 009479b1914164c52a821eb5d18037c34d1a8d77..10d5ed976dd82331c1240de3fa68d428f171c526 100644 (file)
@@ -5590,14 +5590,13 @@ PHP_FUNCTION(substr_compare)
        int s1_len, s2_len;
        long offset, len=0;
        zend_bool cs=0;
-       uint cmp_len;
 
        if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ssl|lb", &s1, &s1_len, &s2, &s2_len, &offset, &len, &cs) == FAILURE) {
                RETURN_FALSE;
        }
 
-       if (ZEND_NUM_ARGS() >= 4 && len <= 0) {
-               php_error_docref(NULL TSRMLS_CC, E_WARNING, "The length must be greater than zero");
+       if (ZEND_NUM_ARGS() >= 4 && len < 0) {
+               php_error_docref(NULL TSRMLS_CC, E_WARNING, "The length must be greater than or equal to zero");
                RETURN_FALSE;
        }
 
@@ -5611,12 +5610,10 @@ PHP_FUNCTION(substr_compare)
                RETURN_FALSE;
        }
 
-       cmp_len = (uint) (len ? len : MAX(s2_len, (s1_len - offset)));
-
        if (!cs) {
-               RETURN_LONG(zend_binary_strncmp(s1 + offset, (s1_len - offset), s2, s2_len, cmp_len));
+               RETURN_LONG(zend_binary_strncmp(s1 + offset, (s1_len - offset), s2, s2_len, (uint)len));
        } else {
-               RETURN_LONG(zend_binary_strncasecmp_l(s1 + offset, (s1_len - offset), s2, s2_len, cmp_len));
+               RETURN_LONG(zend_binary_strncasecmp_l(s1 + offset, (s1_len - offset), s2, s2_len, (uint)len));
        }
 }
 /* }}} */
index f0c49eb18fb5a571ba7331fb70ac4f44a63f80a7..7ba38f94f50d27e34b11772c9e9e28ec6bcc67f5 100644 (file)
@@ -2,10 +2,10 @@
 Bug #33605 (substr_compare crashes)
 --FILE--
 <?php
-$res = substr_compare("aa", "a", -99999999, 0, 0);
+$res = substr_compare("aa", "a", -99999999, -1, 0);
 var_dump($res);
 
 ?>
 --EXPECTF--
-Warning: substr_compare(): The length must be greater than zero in %s on line %d
+Warning: substr_compare(): The length must be greater than or equal to zero in %s on line %d
 bool(false)
index c647506b53736a7ee244e1e98127aa9f9fccb1ab..84777711e63e3a0e5cf606232aa268343eecc4f8 100644 (file)
@@ -10,9 +10,10 @@ var_dump(substr_compare("abcde", "bc", 1, 3));
 var_dump(substr_compare("abcde", "cd", 1, 2));
 var_dump(substr_compare("abcde", "abc", 5, 1));
 var_dump(substr_compare("abcde", "abcdef", -10, 10));
-
+var_dump(substr_compare("abcde", "abc", 0, 0));
 var_dump(substr_compare("abcde", -1, 0, NULL, new stdClass));
 echo "Test\n";
+var_dump(substr_compare("abcde", "abc", 0, -1));
 var_dump(substr_compare("abcde", "abc", -1, NULL, -5));
 var_dump(substr_compare("abcde", -1, 0, "str", new stdClass));
 
@@ -28,13 +29,15 @@ int(-1)
 Warning: substr_compare(): The start position cannot exceed initial string length in %s on line %d
 bool(false)
 int(-1)
+int(0)
 
 Warning: substr_compare() expects parameter 5 to be boolean, object given in %s on line %d
 bool(false)
 Test
 
-Warning: substr_compare(): The length must be greater than zero in %s on line %d
+Warning: substr_compare(): The length must be greater than or equal to zero in %s on line %d
 bool(false)
+int(0)
 
 Warning: substr_compare() expects parameter 4 to be long, string given in %s on line %d
 bool(false)