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

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

Patch version 2011-07-26 11:21 UTC

Return to Bug #36944 | Download this patch
This patch renders other patches obsolete

Obsolete patches:

Patch Revisions:

Developer: laruence@php.net

Index: Zend/zend_builtin_functions.c
===================================================================
--- Zend/zend_builtin_functions.c	(revision 313720)
+++ 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 313720)
+++ Zend/zend_operators.c	(working copy)
@@ -1910,16 +1910,31 @@
 }
 /* }}} */
 
-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, len;
 
-	retval = memcmp(s1, s2, MIN(length, MIN(len1, len2)));
-	if (!retval) {
-		return (MIN(length, len1) - MIN(length, len2));
+		len = MIN(len1, MIN(len2, -length));
+
+		m = len1 - len;
+		n = len2 - len;
+
+		retval = memcmp(s1 + m, s2 + n, len);
+
+		if (!retval) {
+			return (MIN(-length, len1) - MIN(-length, len2));
+		}
+
 	} else {
-		return retval;
+		retval = memcmp(s1, s2, MIN(length, MIN(len1, len2)));
+		if (!retval) {
+			return (MIN(length, len1) - MIN(length, len2));
+		} 
 	}
+
+	return retval;
 }
 /* }}} */
 
@@ -1942,22 +1957,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;
 
-	while (len--) {
-		c1 = zend_tolower((int)*(unsigned char *)s1++);
-		c2 = zend_tolower((int)*(unsigned char *)s2++);
-		if (c1 != c2) {
-			return c1 - c2;
+		len = MIN(len1, MIN(len2, -length));
+
+		m = len1 - len;
+		n = len2 - len;
+
+		s1+=m;
+		s2+=n;
+
+		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);
+		
+	} 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 313720)
+++ 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-2024 The PHP Group
All rights reserved.
Last updated: Thu Nov 21 18:01:29 2024 UTC