Skip to content

Commit

Permalink
Merge branch 'PHP-8.1'
Browse files Browse the repository at this point in the history
* PHP-8.1:
  Fix GH-9296: `ksort` behaves incorrectly on arrays with mixed keys
  • Loading branch information
cmb69 committed Aug 12, 2022
2 parents 0028c24 + 7908aae commit 1094a85
Show file tree
Hide file tree
Showing 7 changed files with 86 additions and 84 deletions.
53 changes: 19 additions & 34 deletions ext/standard/array.c
Original file line number Diff line number Diff line change
Expand Up @@ -166,40 +166,25 @@ static zend_never_inline ZEND_COLD int stable_sort_fallback(Bucket *a, Bucket *b

static zend_always_inline int php_array_key_compare_unstable_i(Bucket *f, Bucket *s) /* {{{ */
{
zend_uchar t;
zend_long l1, l2;
double d;

if (f->key == NULL) {
if (s->key == NULL) {
return (zend_long)f->h > (zend_long)s->h ? 1 : -1;
} else {
l1 = (zend_long)f->h;
t = is_numeric_string(s->key->val, s->key->len, &l2, &d, 1);
if (t == IS_LONG) {
/* pass */
} else if (t == IS_DOUBLE) {
return ZEND_NORMALIZE_BOOL((double)l1 - d);
} else {
l2 = 0;
}
}
} else {
if (s->key) {
return zendi_smart_strcmp(f->key, s->key);
} else {
l2 = (zend_long)s->h;
t = is_numeric_string(f->key->val, f->key->len, &l1, &d, 1);
if (t == IS_LONG) {
/* pass */
} else if (t == IS_DOUBLE) {
return ZEND_NORMALIZE_BOOL(d - (double)l2);
} else {
l1 = 0;
}
}
}
return ZEND_NORMALIZE_BOOL(l1 - l2);
zval first;
zval second;

if (f->key == NULL && s->key == NULL) {
return (zend_long)f->h > (zend_long)s->h ? 1 : -1;
} else if (f->key && s->key) {
return zendi_smart_strcmp(f->key, s->key);
}
if (f->key) {
ZVAL_STR(&first, f->key);
} else {
ZVAL_LONG(&first, f->h);
}
if (s->key) {
ZVAL_STR(&second, s->key);
} else {
ZVAL_LONG(&second, s->h);
}
return zend_compare(&first, &second);
}
/* }}} */

Expand Down
20 changes: 10 additions & 10 deletions ext/standard/tests/array/002.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,8 @@ array(8) {
-- Testing krsort() --
No second argument:
array(8) {
["test"]=>
int(27)
[16777216]=>
float(-0.3333333333333333)
[1001]=>
Expand All @@ -288,8 +290,6 @@ array(8) {
string(4) "Test"
[0]=>
string(3) "PHP"
["test"]=>
int(27)
[-1000]=>
array(2) {
[0]=>
Expand All @@ -300,6 +300,8 @@ array(8) {
}
Using SORT_REGULAR:
array(8) {
["test"]=>
int(27)
[16777216]=>
float(-0.3333333333333333)
[1001]=>
Expand All @@ -312,8 +314,6 @@ array(8) {
string(4) "Test"
[0]=>
string(3) "PHP"
["test"]=>
int(27)
[-1000]=>
array(2) {
[0]=>
Expand All @@ -334,10 +334,10 @@ array(8) {
string(27) "PHP: Hypertext Preprocessor"
[5]=>
string(4) "Test"
[0]=>
string(3) "PHP"
["test"]=>
int(27)
[0]=>
string(3) "PHP"
[-1000]=>
array(2) {
[0]=>
Expand Down Expand Up @@ -383,8 +383,6 @@ array(8) {
}
[0]=>
string(3) "PHP"
["test"]=>
int(27)
[5]=>
string(4) "Test"
[17]=>
Expand All @@ -395,6 +393,8 @@ array(8) {
string(6) "monkey"
[16777216]=>
float(-0.3333333333333333)
["test"]=>
int(27)
}
Using SORT_REGULAR:
array(8) {
Expand All @@ -407,8 +407,6 @@ array(8) {
}
[0]=>
string(3) "PHP"
["test"]=>
int(27)
[5]=>
string(4) "Test"
[17]=>
Expand All @@ -419,6 +417,8 @@ array(8) {
string(6) "monkey"
[16777216]=>
float(-0.3333333333333333)
["test"]=>
int(27)
}
Using SORT_NUMERIC:
array(8) {
Expand Down
17 changes: 17 additions & 0 deletions ext/standard/tests/array/gh9296.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
--TEST--
GH-9296: incorrect ksort(..., SORT_REGULAR) behaviour on arrays with numeric and string keys
--FILE--
<?php
$array = ["aaa" => 0, 600 => 1];
ksort($array, SORT_REGULAR);
var_dump($array);
var_dump(array_key_first($array) <=> array_key_last($array));
?>
--EXPECT--
array(2) {
[600]=>
int(1)
["aaa"]=>
int(0)
}
int(-1)
24 changes: 12 additions & 12 deletions ext/standard/tests/array/krsort_variation8.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,6 @@ echo "Done\n";
-- Testing krsort() by supplying mixed value array, 'flag' value is default --
bool(true)
array(13) {
[5]=>
string(1) "5"
[4]=>
float(4.01)
["b"]=>
string(1) "b"
["array2"]=>
Expand Down Expand Up @@ -85,23 +81,23 @@ array(13) {
string(2) "ab"
["True"]=>
string(4) "True"
[5]=>
string(1) "5"
[4]=>
float(4.01)
[0]=>
float(0.001)
["-.9"]=>
string(3) "-.9"
[""]=>
string(0) ""
[-2]=>
float(-2.98989)
[""]=>
string(0) ""
}

-- Testing krsort() by supplying mixed value array, 'flag' value is SORT_REGULAR --
bool(true)
array(13) {
[5]=>
string(1) "5"
[4]=>
float(4.01)
["b"]=>
string(1) "b"
["array2"]=>
Expand Down Expand Up @@ -142,13 +138,17 @@ array(13) {
string(2) "ab"
["True"]=>
string(4) "True"
[5]=>
string(1) "5"
[4]=>
float(4.01)
[0]=>
float(0.001)
["-.9"]=>
string(3) "-.9"
[""]=>
string(0) ""
[-2]=>
float(-2.98989)
[""]=>
string(0) ""
}
Done
8 changes: 4 additions & 4 deletions ext/standard/tests/array/krsort_variation9.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -82,22 +82,22 @@ array(5) {
- With default sort flag -
bool(true)
array(3) {
["c"]=>
string(5) "apple"
["a"]=>
string(6) "orange"
[0]=>
string(6) "banana"
["c"]=>
string(5) "apple"
}
- Sort flag = SORT_REGULAR -
bool(true)
array(3) {
["c"]=>
string(5) "apple"
["a"]=>
string(6) "orange"
[0]=>
string(6) "banana"
["c"]=>
string(5) "apple"
}

-- Iteration 3 --
Expand Down
40 changes: 20 additions & 20 deletions ext/standard/tests/array/ksort_variation8.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -40,18 +40,26 @@ echo "Done\n";
-- Testing ksort() by supplying mixed value array, 'flag' value is default --
bool(true)
array(13) {
[-2]=>
float(-2.98989)
[""]=>
string(0) ""
[-2]=>
float(-2.98989)
["-.9"]=>
string(3) "-.9"
[0]=>
float(0.001)
[4]=>
float(4.01)
[5]=>
string(1) "5"
["True"]=>
string(4) "True"
["ab"]=>
string(2) "ab"
["abcd"]=>
string(4) "abcd"
["abcd%0abcd%0abcd"]=>
string(14) "abcd%0abcd%0abcd"
["array1"]=>
array(0) {
}
Expand Down Expand Up @@ -84,31 +92,31 @@ array(13) {
}
["b"]=>
string(1) "b"
[0]=>
float(0.001)
["abcd%0abcd%0abcd"]=>
string(14) "abcd%0abcd%0abcd"
[4]=>
float(4.01)
[5]=>
string(1) "5"
}

-- Testing ksort() by supplying mixed value array, 'flag' value is SORT_REGULAR --
bool(true)
array(13) {
[-2]=>
float(-2.98989)
[""]=>
string(0) ""
[-2]=>
float(-2.98989)
["-.9"]=>
string(3) "-.9"
[0]=>
float(0.001)
[4]=>
float(4.01)
[5]=>
string(1) "5"
["True"]=>
string(4) "True"
["ab"]=>
string(2) "ab"
["abcd"]=>
string(4) "abcd"
["abcd%0abcd%0abcd"]=>
string(14) "abcd%0abcd%0abcd"
["array1"]=>
array(0) {
}
Expand Down Expand Up @@ -141,13 +149,5 @@ array(13) {
}
["b"]=>
string(1) "b"
[0]=>
float(0.001)
["abcd%0abcd%0abcd"]=>
string(14) "abcd%0abcd%0abcd"
[4]=>
float(4.01)
[5]=>
string(1) "5"
}
Done
8 changes: 4 additions & 4 deletions ext/standard/tests/array/ksort_variation9.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -81,20 +81,20 @@ array(5) {
- With default sort flag -
bool(true)
array(3) {
["a"]=>
string(6) "orange"
[0]=>
string(6) "banana"
["a"]=>
string(6) "orange"
["c"]=>
string(5) "apple"
}
- Sort flag = SORT_REGULAR -
bool(true)
array(3) {
["a"]=>
string(6) "orange"
[0]=>
string(6) "banana"
["a"]=>
string(6) "orange"
["c"]=>
string(5) "apple"
}
Expand Down

0 comments on commit 1094a85

Please sign in to comment.