mattwil Wed Apr 1 17:04:17 2009 UTC Removed files: /php-src/ext/standard/tests/strings bug47546.phpt
Modified files: /php-src/ext/standard php_string.h string.c Log: explode() stuff: - Fixed bug #47560 (explode()'s limit parameter odd behaviour) by reverting change for bug #47546 - Changed int to long where needed (should fix memory errors from overflow seen in bug #47854) - Simplified logic a bit with limit and its default value - php_explode_negative_limit(): removed safe_emalloc (not needed; plain erealloc is used later) - Moved declarations/allocation to optimize if the delimiter isn't found - Changed ALLOC_STEP size for less realloc's (and maybe better memory block alignment?) http://cvs.php.net/viewvc.cgi/php-src/ext/standard/php_string.h?r1=1.113&r2=1.114&diff_format=u Index: php-src/ext/standard/php_string.h diff -u php-src/ext/standard/php_string.h:1.113 php-src/ext/standard/php_string.h:1.114 --- php-src/ext/standard/php_string.h:1.113 Tue Mar 10 23:39:40 2009 +++ php-src/ext/standard/php_string.h Wed Apr 1 17:04:16 2009 @@ -17,7 +17,7 @@ +----------------------------------------------------------------------+ */ -/* $Id: php_string.h,v 1.113 2009/03/10 23:39:40 helly Exp $ */ +/* $Id: php_string.h,v 1.114 2009/04/01 17:04:16 mattwil Exp $ */ /* Synced with php 3.0 revision 1.43 1999-06-16 [ssb] */ @@ -158,7 +158,7 @@ PHPAPI int php_char_to_str_ex(char *str, uint len, char from, char *to, int to_len, zval *result, int case_sensitivity, int *replace_count); PHPAPI int php_char_to_str(char *str, uint len, char from, char *to, int to_len, zval *result); PHPAPI void php_implode(zval *delim, zval *arr, zval *return_value TSRMLS_DC); -PHPAPI void php_explode(char *delim, uint delim_len, char *str, uint str_len, zend_uchar str_type, zval *return_value, int limit); +PHPAPI void php_explode(char *delim, uint delim_len, char *str, uint str_len, zend_uchar str_type, zval *return_value, long limit); PHPAPI size_t php_strspn(char *s1, char *s2, char *s1_end, char *s2_end); PHPAPI size_t php_strcspn(char *s1, char *s2, char *s1_end, char *s2_end); http://cvs.php.net/viewvc.cgi/php-src/ext/standard/string.c?r1=1.696&r2=1.697&diff_format=u Index: php-src/ext/standard/string.c diff -u php-src/ext/standard/string.c:1.696 php-src/ext/standard/string.c:1.697 --- php-src/ext/standard/string.c:1.696 Thu Mar 26 22:16:48 2009 +++ php-src/ext/standard/string.c Wed Apr 1 17:04:16 2009 @@ -18,7 +18,7 @@ +----------------------------------------------------------------------+ */ -/* $Id: string.c,v 1.696 2009/03/26 22:16:48 felipe Exp $ */ +/* $Id: string.c,v 1.697 2009/04/01 17:04:16 mattwil Exp $ */ /* Synced with php 3.0 revision 1.193 1999-06-16 [ssb] */ @@ -1137,7 +1137,7 @@ /* {{{ php_explode */ -PHPAPI void php_explode(char *delim, uint delim_len, char *str, uint str_len, zend_uchar str_type, zval *return_value, int limit) +PHPAPI void php_explode(char *delim, uint delim_len, char *str, uint str_len, zend_uchar str_type, zval *return_value, long limit) { char *p1, *p2, *endp; @@ -1152,7 +1152,7 @@ add_next_index_stringl(return_value, p1, p2-p1, 1); p1 = p2 + delim_len; } while ( (p2 = php_memnstr(p1, delim, delim_len, endp)) != NULL && - (limit == -1 || --limit > 1) ); + --limit > 1 ); if ( p1 <= endp ) { add_next_index_stringl(return_value, p1, endp-p1, 1); @@ -1163,12 +1163,10 @@ /* {{{ php_explode_negative_limit */ -PHPAPI void php_explode_negative_limit(char *delim, uint delim_len, char *str, uint str_len, zend_uchar str_type, zval *return_value, int limit) +PHPAPI void php_explode_negative_limit(char *delim, uint delim_len, char *str, uint str_len, zend_uchar str_type, zval *return_value, long limit) { -#define EXPLODE_ALLOC_STEP 50 +#define EXPLODE_ALLOC_STEP 64 char *p1, *p2, *endp; - int allocated = EXPLODE_ALLOC_STEP, found = 0, i = 0, to_return = 0; - char **positions = safe_emalloc(allocated, sizeof(char *), 0); endp = str + str_len; p1 = str; @@ -1180,6 +1178,10 @@ by doing nothing we return empty array */ } else { + int allocated = EXPLODE_ALLOC_STEP, found = 0; + long i, to_return; + char **positions = emalloc(allocated * sizeof(char *)); + positions[found++] = p1; do { if ( found >= allocated ) { @@ -1195,8 +1197,8 @@ add_next_index_stringl(return_value, positions[i], (positions[i+1]-delim_len) - positions[i], 1); } + efree(positions); } - efree(positions); #undef EXPLODE_ALLOC_STEP } /* }}} */ @@ -1204,7 +1206,7 @@ /* {{{ php_u_explode * Unicode capable version of php_explode() */ -static void php_u_explode(UChar *delim, uint delim_len, UChar *str, uint str_len, zval *return_value, int limit) +static void php_u_explode(UChar *delim, uint delim_len, UChar *str, uint str_len, zval *return_value, long limit) { UChar *p1, *p2, *endp; @@ -1219,7 +1221,7 @@ add_next_index_unicodel(return_value, p1, p2-p1, 1); p1 = (UChar *)p2 + delim_len; } while ((p2 = zend_u_memnstr(p1, delim, delim_len, endp)) != NULL && - (limit == -1 || --limit > 1) ); + --limit > 1 ); if ( p1 <= endp ) { add_next_index_unicodel(return_value, p1, endp-p1, 1); @@ -1231,12 +1233,10 @@ /* {{{ php_u_explode_negative_limit * Unicode capable version of php_explode_negative_limit() */ -static void php_u_explode_negative_limit(UChar *delim, uint delim_len, UChar *str, uint str_len, zval *return_value, int limit) +static void php_u_explode_negative_limit(UChar *delim, uint delim_len, UChar *str, uint str_len, zval *return_value, long limit) { -#define EXPLODE_ALLOC_STEP 50 +#define EXPLODE_ALLOC_STEP 64 UChar *p1, *p2, *endp; - int allocated = EXPLODE_ALLOC_STEP, found = 0, i = 0, to_return = 0; - UChar **positions = safe_emalloc(allocated, sizeof(UChar *), 0); endp = str + str_len; p1 = str; @@ -1248,6 +1248,10 @@ by doing nothing we return empty array */ } else { + int allocated = EXPLODE_ALLOC_STEP, found = 0; + long i, to_return; + UChar **positions = emalloc(allocated * sizeof(UChar *)); + positions[found++] = p1; do { if ( found >= allocated ) { @@ -1263,8 +1267,8 @@ add_next_index_unicodel(return_value, positions[i], (positions[i+1]-delim_len) - positions[i], 1); } + efree(positions); } - efree(positions); #undef EXPLODE_ALLOC_STEP } /* }}} */ @@ -1276,10 +1280,9 @@ void *str, *delim; int str_len, delim_len; zend_uchar str_type, delim_type; - long limit = -1; - int argc = ZEND_NUM_ARGS(); + long limit = LONG_MAX; /* No limit */ - if (zend_parse_parameters(argc TSRMLS_CC, "TT|l", &delim, &delim_len, &delim_type, + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "TT|l", &delim, &delim_len, &delim_type, &str, &str_len, &str_type, &limit) == FAILURE) { return; } @@ -1292,7 +1295,7 @@ array_init(return_value); if ( str_len == 0 ) { - if (limit >= 0 || argc == 2) { + if (limit >= 0) { if ( str_type == IS_UNICODE ) { add_next_index_unicodel(return_value, USTR_MAKE(""), sizeof("")-1, 0); } else { @@ -1302,14 +1305,13 @@ return; } - - if (limit == 0 || limit == 1) { + if (limit > 1) { if ( str_type == IS_UNICODE ) { - add_index_unicodel(return_value, 0, (UChar *)str, str_len, 1); + php_u_explode((UChar *)delim, delim_len, (UChar *)str, str_len, return_value, limit); } else { - add_index_stringl(return_value, 0, (char *)str, str_len, 1); + php_explode((char *)delim, delim_len, (char *)str, str_len, str_type, return_value, limit); } - } else if (limit < -1 && argc == 3) { + } else if (limit < 0) { if ( str_type == IS_UNICODE ) { php_u_explode_negative_limit((UChar *)delim, delim_len, (UChar *)str, str_len, return_value, limit); } else { @@ -1317,9 +1319,9 @@ } } else { if ( str_type == IS_UNICODE ) { - php_u_explode((UChar *)delim, delim_len, (UChar *)str, str_len, return_value, limit); + add_index_unicodel(return_value, 0, (UChar *)str, str_len, 1); } else { - php_explode((char *)delim, delim_len, (char *)str, str_len, str_type, return_value, limit); + add_index_stringl(return_value, 0, (char *)str, str_len, 1); } } } -- PHP CVS Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php