Skip to content

Commit 725cb4e

Browse files
committed
Revert "Fix GH-9296: ksort behaves incorrectly on arrays with mixed keys"
This reverts commit cd1aed8, as discussed on internals (<https://externals.io/message/118483>).
1 parent ce42dcf commit 725cb4e

File tree

8 files changed

+52
-56
lines changed

8 files changed

+52
-56
lines changed

NEWS

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,6 @@ PHP NEWS
4646
- Standard:
4747
. Fixed bug GH-9017 (php_stream_sock_open_from_socket could return NULL).
4848
(Heiko Weber)
49-
. Fixed bug GH-9296 (`ksort` behaves incorrectly on arrays with mixed keys).
50-
(Denis Vaksman)
5149

5250
- Streams:
5351
. Fixed bug GH-8472 (The resource returned by stream_socket_accept may have

ext/standard/array.c

Lines changed: 34 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -166,25 +166,40 @@ static zend_never_inline ZEND_COLD int stable_sort_fallback(Bucket *a, Bucket *b
166166

167167
static zend_always_inline int php_array_key_compare_unstable_i(Bucket *f, Bucket *s) /* {{{ */
168168
{
169-
zval first;
170-
zval second;
171-
172-
if (f->key == NULL && s->key == NULL) {
173-
return (zend_long)f->h > (zend_long)s->h ? 1 : -1;
174-
} else if (f->key && s->key) {
175-
return zendi_smart_strcmp(f->key, s->key);
176-
}
177-
if (f->key) {
178-
ZVAL_STR(&first, f->key);
179-
} else {
180-
ZVAL_LONG(&first, f->h);
181-
}
182-
if (s->key) {
183-
ZVAL_STR(&second, s->key);
184-
} else {
185-
ZVAL_LONG(&second, s->h);
186-
}
187-
return zend_compare(&first, &second);
169+
zend_uchar t;
170+
zend_long l1, l2;
171+
double d;
172+
173+
if (f->key == NULL) {
174+
if (s->key == NULL) {
175+
return (zend_long)f->h > (zend_long)s->h ? 1 : -1;
176+
} else {
177+
l1 = (zend_long)f->h;
178+
t = is_numeric_string(s->key->val, s->key->len, &l2, &d, 1);
179+
if (t == IS_LONG) {
180+
/* pass */
181+
} else if (t == IS_DOUBLE) {
182+
return ZEND_NORMALIZE_BOOL((double)l1 - d);
183+
} else {
184+
l2 = 0;
185+
}
186+
}
187+
} else {
188+
if (s->key) {
189+
return zendi_smart_strcmp(f->key, s->key);
190+
} else {
191+
l2 = (zend_long)s->h;
192+
t = is_numeric_string(f->key->val, f->key->len, &l1, &d, 1);
193+
if (t == IS_LONG) {
194+
/* pass */
195+
} else if (t == IS_DOUBLE) {
196+
return ZEND_NORMALIZE_BOOL(d - (double)l2);
197+
} else {
198+
l1 = 0;
199+
}
200+
}
201+
}
202+
return ZEND_NORMALIZE_BOOL(l1 - l2);
188203
}
189204
/* }}} */
190205

ext/standard/tests/array/002.phpt

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -276,8 +276,6 @@ array(8) {
276276
-- Testing krsort() --
277277
No second argument:
278278
array(8) {
279-
["test"]=>
280-
int(27)
281279
[16777216]=>
282280
float(-0.3333333333333333)
283281
[1001]=>
@@ -290,6 +288,8 @@ array(8) {
290288
string(4) "Test"
291289
[0]=>
292290
string(3) "PHP"
291+
["test"]=>
292+
int(27)
293293
[-1000]=>
294294
array(2) {
295295
[0]=>
@@ -300,8 +300,6 @@ array(8) {
300300
}
301301
Using SORT_REGULAR:
302302
array(8) {
303-
["test"]=>
304-
int(27)
305303
[16777216]=>
306304
float(-0.3333333333333333)
307305
[1001]=>
@@ -314,6 +312,8 @@ array(8) {
314312
string(4) "Test"
315313
[0]=>
316314
string(3) "PHP"
315+
["test"]=>
316+
int(27)
317317
[-1000]=>
318318
array(2) {
319319
[0]=>
@@ -334,10 +334,10 @@ array(8) {
334334
string(27) "PHP: Hypertext Preprocessor"
335335
[5]=>
336336
string(4) "Test"
337-
["test"]=>
338-
int(27)
339337
[0]=>
340338
string(3) "PHP"
339+
["test"]=>
340+
int(27)
341341
[-1000]=>
342342
array(2) {
343343
[0]=>
@@ -383,6 +383,8 @@ array(8) {
383383
}
384384
[0]=>
385385
string(3) "PHP"
386+
["test"]=>
387+
int(27)
386388
[5]=>
387389
string(4) "Test"
388390
[17]=>
@@ -393,8 +395,6 @@ array(8) {
393395
string(6) "monkey"
394396
[16777216]=>
395397
float(-0.3333333333333333)
396-
["test"]=>
397-
int(27)
398398
}
399399
Using SORT_REGULAR:
400400
array(8) {
@@ -407,6 +407,8 @@ array(8) {
407407
}
408408
[0]=>
409409
string(3) "PHP"
410+
["test"]=>
411+
int(27)
410412
[5]=>
411413
string(4) "Test"
412414
[17]=>
@@ -417,8 +419,6 @@ array(8) {
417419
string(6) "monkey"
418420
[16777216]=>
419421
float(-0.3333333333333333)
420-
["test"]=>
421-
int(27)
422422
}
423423
Using SORT_NUMERIC:
424424
array(8) {

ext/standard/tests/array/gh9296.phpt

Lines changed: 0 additions & 17 deletions
This file was deleted.
0 Bytes
Binary file not shown.

ext/standard/tests/array/krsort_variation9.phpt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -82,22 +82,22 @@ array(5) {
8282
- With default sort flag -
8383
bool(true)
8484
array(3) {
85-
["c"]=>
86-
string(5) "apple"
8785
["a"]=>
8886
string(6) "orange"
8987
[0]=>
9088
string(6) "banana"
89+
["c"]=>
90+
string(5) "apple"
9191
}
9292
- Sort flag = SORT_REGULAR -
9393
bool(true)
9494
array(3) {
95-
["c"]=>
96-
string(5) "apple"
9795
["a"]=>
9896
string(6) "orange"
9997
[0]=>
10098
string(6) "banana"
99+
["c"]=>
100+
string(5) "apple"
101101
}
102102

103103
-- Iteration 3 --
0 Bytes
Binary file not shown.

ext/standard/tests/array/ksort_variation9.phpt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -81,20 +81,20 @@ array(5) {
8181
- With default sort flag -
8282
bool(true)
8383
array(3) {
84-
[0]=>
85-
string(6) "banana"
8684
["a"]=>
8785
string(6) "orange"
86+
[0]=>
87+
string(6) "banana"
8888
["c"]=>
8989
string(5) "apple"
9090
}
9191
- Sort flag = SORT_REGULAR -
9292
bool(true)
9393
array(3) {
94-
[0]=>
95-
string(6) "banana"
9694
["a"]=>
9795
string(6) "orange"
96+
[0]=>
97+
string(6) "banana"
9898
["c"]=>
9999
string(5) "apple"
100100
}

0 commit comments

Comments
 (0)