Re: [PHP-DEV] Patch for range()

2002-11-23 Thread Jon Parise
On Sat, Nov 23, 2002 at 06:47:48PM +0900, Moriyoshi Koizumi wrote:

> BTW how about renaming it to array_range() and adding an alias for BC?
 
I think that's logical, but I'm leave it up to the QA folks to make
the call.

-- 
Jon Parise ([EMAIL PROTECTED]) :: The PHP Project (http://www.php.net/)

-- 
PHP Development Mailing List 
To unsubscribe, visit: http://www.php.net/unsub.php




Re: [PHP-DEV] Patch for range()

2002-11-23 Thread Moriyoshi Koizumi
Thanks for the review.
Also I'll add a test for this function.

BTW how about renaming it to array_range() and adding an alias for BC?

Moriyoshi

Jon Parise <[EMAIL PROTECTED]> wrote:

> On Sat, Nov 23, 2002 at 03:37:29PM +0900, Moriyoshi Koizumi wrote:
> 
> > I've just found range() behaves unexpectedly in some special cases.
> > 
> > For instance, please try the following script.
> > 
> >  > echo count(range('a', 'z', 12));
> > ?>
> > 
> > will give 45 while it should return an array that consists of 3 elements. 
> > That is because the counting may exceed the upper limit of positive char 
> > value during the loop.
> > 
> > The attached patch is a fix for this issue. I'll commit this if there are 
> > no objections.
>  
> No objections (although I haven't actually applied and run your
> patch).  Thanks for investigating this.  I should have tested a wider
> set of step values in my original tests.
> 
> -- 
> Jon Parise ([EMAIL PROTECTED]) :: The PHP Project (http://www.php.net/)


-- 
PHP Development Mailing List 
To unsubscribe, visit: http://www.php.net/unsub.php




Re: [PHP-DEV] Patch for range()

2002-11-22 Thread Jon Parise
On Sat, Nov 23, 2002 at 03:37:29PM +0900, Moriyoshi Koizumi wrote:

> I've just found range() behaves unexpectedly in some special cases.
> 
> For instance, please try the following script.
> 
>  echo count(range('a', 'z', 12));
> ?>
> 
> will give 45 while it should return an array that consists of 3 elements. 
> That is because the counting may exceed the upper limit of positive char 
> value during the loop.
> 
> The attached patch is a fix for this issue. I'll commit this if there are 
> no objections.
 
No objections (although I haven't actually applied and run your
patch).  Thanks for investigating this.  I should have tested a wider
set of step values in my original tests.

-- 
Jon Parise ([EMAIL PROTECTED]) :: The PHP Project (http://www.php.net/)

-- 
PHP Development Mailing List 
To unsubscribe, visit: http://www.php.net/unsub.php




[PHP-DEV] Patch for range()

2002-11-22 Thread Moriyoshi Koizumi
Hi,

I've just found range() behaves unexpectedly in some special cases.

For instance, please try the following script.



will give 45 while it should return an array that consists of 3 elements. 
That is because the counting may exceed the upper limit of positive char 
value during the loop.

The attached patch is a fix for this issue. I'll commit this if there are 
no objections.

Moriyoshi


Index: ext/standard/array.c
===
RCS file: /repository/php4/ext/standard/array.c,v
retrieving revision 1.201
diff -u -r1.201 array.c
--- ext/standard/array.c15 Nov 2002 02:16:41 -  1.201
+++ ext/standard/array.c23 Nov 2002 06:20:16 -
@@ -1435,19 +1435,29 @@
 
/* If the range is given as strings, generate an array of characters. */
if (Z_TYPE_P(zlow) == IS_STRING && Z_TYPE_P(zhigh) == IS_STRING) {
-   char *low, *high;
+   unsigned char *low, *high;
 
convert_to_string_ex(&zlow);
convert_to_string_ex(&zhigh);
-   low = Z_STRVAL_P(zlow);
-   high = Z_STRVAL_P(zhigh);
+   low = (unsigned char *)Z_STRVAL_P(zlow);
+   high = (unsigned char *)Z_STRVAL_P(zhigh);
 
if (*low > *high) { /* Negative steps */
-   for (; *low >= *high; (*low) -= step) {
+   if (*low - *high < step || step < 0) {
+   php_error_docref(NULL TSRMLS_CC, E_WARNING, "step 
+exceeds the specified range");
+   zval_dtor(return_value);
+   RETURN_FALSE;
+   }
+   for (; *low >= *high; (*low) -= (unsigned int)step) {
add_next_index_stringl(return_value, low, 1, 1);
}
} else {/* Positive steps */
-   for (; *low <= *high; (*low) += step) {
+   if (*high - *low < step || step < 0) {
+   php_error_docref(NULL TSRMLS_CC, E_WARNING, "step 
+exceeds the specified range");
+   zval_dtor(return_value);
+   RETURN_FALSE;
+   }
+   for (; *low <= *high; (*low) += (unsigned int)step) {
add_next_index_stringl(return_value, low, 1, 1);
}
}
@@ -1460,10 +1470,20 @@
high = Z_LVAL_P(zhigh);
 
if (low > high) {   /* Negative steps */
+   if (low - high < step || step < 0) {
+   php_error_docref(NULL TSRMLS_CC, E_WARNING, "step 
+exceeds the specified range");
+   zval_dtor(return_value);
+   RETURN_FALSE;
+   }
for (; low >= high; low -= step) {
add_next_index_long(return_value, low);
}   
} else {/* Positive steps */
+   if (high - low < step || step < 0) {
+   php_error_docref(NULL TSRMLS_CC, E_WARNING, "step 
+exceeds the specified range");
+   zval_dtor(return_value);
+   RETURN_FALSE;
+   }
for (; low <= high; low += step) {
add_next_index_long(return_value, low);
}   

-- 
PHP Development Mailing List 
To unsubscribe, visit: http://www.php.net/unsub.php