php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login

Patch php-5-3-strncasecmp-supporting-negative-length for Strings related Bug #36944

Patch version 2011-07-25 11:06 UTC

Return to Bug #36944 | Download this patch
This patch is obsolete

Obsoleted by patches:

Patch Revisions:

Developer: laruence@php.net

Index: Zend/zend_builtin_functions.c
===================================================================
--- Zend/zend_builtin_functions.c	(revision 313645)
+++ Zend/zend_builtin_functions.c	(working copy)
@@ -493,11 +493,6 @@
 		return;
 	}
 
-	if (len < 0) {
-		zend_error(E_WARNING, "Length must be greater than or equal to 0");
-		RETURN_FALSE;
-	}
-
 	RETURN_LONG(zend_binary_strncmp(s1, s1_len, s2, s2_len, len));
 }
 /* }}} */
@@ -531,11 +526,6 @@
 		return;
 	}
 
-	if (len < 0) {
-		zend_error(E_WARNING, "Length must be greater than or equal to 0");
-		RETURN_FALSE;
-	}
-
 	RETURN_LONG(zend_binary_strncasecmp(s1, s1_len, s2, s2_len, len));
 }
 /* }}} */
Index: Zend/zend_operators.c
===================================================================
--- Zend/zend_operators.c	(revision 313645)
+++ Zend/zend_operators.c	(working copy)
@@ -1910,15 +1910,27 @@
 }
 /* }}} */
 
-ZEND_API int zend_binary_strncmp(const char *s1, uint len1, const char *s2, uint len2, uint length) /* {{{ */
+ZEND_API int zend_binary_strncmp(const char *s1, uint len1, const char *s2, uint len2, int length) /* {{{ */
 {
 	int retval;
+	if (length < 0) {
+		int m, n;
+		m = len1 + length;
+		n = len2 + length;
+		if (m < 0 || n < 0) {
+			return (m - n);
+		}
 
-	retval = memcmp(s1, s2, MIN(length, MIN(len1, len2)));
-	if (!retval) {
-		return (MIN(length, len1) - MIN(length, len2));
+		retval = memcmp(s1 + m, s2 + n, -length);
+
+		return retval;
 	} else {
-		return retval;
+		retval = memcmp(s1, s2, MIN(length, MIN(len1, len2)));
+		if (!retval) {
+			return (MIN(length, len1) - MIN(length, len2));
+		} else {
+			return retval;
+		}
 	}
 }
 /* }}} */
@@ -1942,22 +1954,45 @@
 }
 /* }}} */
 
-ZEND_API int zend_binary_strncasecmp(const char *s1, uint len1, const char *s2, uint len2, uint length) /* {{{ */
+ZEND_API int zend_binary_strncasecmp(const char *s1, uint len1, const char *s2, uint len2, int length) /* {{{ */
 {
 	int len;
 	int c1, c2;
 
-	len = MIN(length, MIN(len1, len2));
+	if (length < 0) {
+		int m, n;
+		m = len1 + length;
+		n = len2 + length;
+		if (m < 0 || n < 0) {
+			return (m - n);
+		}
+		s1+=m;
+		s2+=n;
+		len = -length;
 
-	while (len--) {
-		c1 = zend_tolower((int)*(unsigned char *)s1++);
-		c2 = zend_tolower((int)*(unsigned char *)s2++);
-		if (c1 != c2) {
-			return c1 - c2;
+		while (len--) {
+			c1 = zend_tolower((int)*(unsigned char *)s1++);
+			c2 = zend_tolower((int)*(unsigned char *)s2++);
+			if (c1 != c2) {
+				return c1 - c2;
+			}
 		}
+
+		return 0;
+		
+	} else {
+		len = MIN(length, MIN(len1, len2));
+
+		while (len--) {
+			c1 = zend_tolower((int)*(unsigned char *)s1++);
+			c2 = zend_tolower((int)*(unsigned char *)s2++);
+			if (c1 != c2) {
+				return c1 - c2;
+			}
+		}
+
+		return MIN(length, len1) - MIN(length, len2);
 	}
-
-	return MIN(length, len1) - MIN(length, len2);
 }
 /* }}} */
 
Index: Zend/zend_operators.h
===================================================================
--- Zend/zend_operators.h	(revision 313645)
+++ Zend/zend_operators.h	(working copy)
@@ -312,9 +312,9 @@
 ZEND_API int zend_binary_zval_strcasecmp(zval *s1, zval *s2);
 ZEND_API int zend_binary_zval_strncasecmp(zval *s1, zval *s2, zval *s3);
 ZEND_API int zend_binary_strcmp(const char *s1, uint len1, const char *s2, uint len2);
-ZEND_API int zend_binary_strncmp(const char *s1, uint len1, const char *s2, uint len2, uint length);
+ZEND_API int zend_binary_strncmp(const char *s1, uint len1, const char *s2, uint len2, int length);
 ZEND_API int zend_binary_strcasecmp(const char *s1, uint len1, const char *s2, uint len2);
-ZEND_API int zend_binary_strncasecmp(const char *s1, uint len1, const char *s2, uint len2, uint length);
+ZEND_API int zend_binary_strncasecmp(const char *s1, uint len1, const char *s2, uint len2, int length);
 
 ZEND_API void zendi_smart_strcmp(zval *result, zval *s1, zval *s2);
 ZEND_API void zend_compare_symbol_tables(zval *result, HashTable *ht1, HashTable *ht2 TSRMLS_DC);
 
PHP Copyright © 2001-2014 The PHP Group
All rights reserved.
Last updated: Wed Apr 16 13:02:46 2014 UTC