Skip to content

Commit 9b48fe7

Browse files
authored
array_change_key_case(): Throw ValueError on invalid argument (php#15883)
1 parent d34c09f commit 9b48fe7

File tree

4 files changed

+21
-184
lines changed

4 files changed

+21
-184
lines changed

ext/standard/array.c

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4799,21 +4799,26 @@ PHP_FUNCTION(array_change_key_case)
47994799
zend_string *string_key;
48004800
zend_string *new_key;
48014801
zend_ulong num_key;
4802-
zend_long change_to_upper=0;
4802+
zend_long change_to_upper = PHP_CASE_LOWER;
48034803

48044804
ZEND_PARSE_PARAMETERS_START(1, 2)
48054805
Z_PARAM_ARRAY(array)
48064806
Z_PARAM_OPTIONAL
48074807
Z_PARAM_LONG(change_to_upper)
48084808
ZEND_PARSE_PARAMETERS_END();
48094809

4810+
if (change_to_upper != PHP_CASE_LOWER && change_to_upper != PHP_CASE_UPPER) {
4811+
zend_argument_value_error(2, "must be either CASE_LOWER or CASE_UPPER");
4812+
RETURN_THROWS();
4813+
}
4814+
48104815
array_init_size(return_value, zend_hash_num_elements(Z_ARRVAL_P(array)));
48114816

48124817
ZEND_HASH_FOREACH_KEY_VAL(Z_ARRVAL_P(array), num_key, string_key, entry) {
48134818
if (!string_key) {
48144819
entry = zend_hash_index_update(Z_ARRVAL_P(return_value), num_key, entry);
48154820
} else {
4816-
if (change_to_upper) {
4821+
if (change_to_upper == PHP_CASE_UPPER) {
48174822
new_key = zend_string_toupper(string_key);
48184823
} else {
48194824
new_key = zend_string_tolower(string_key);
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
--TEST--
2+
array_change_key_case(): invalid $case argument
3+
--FILE--
4+
<?php
5+
6+
try {
7+
array_change_key_case([], -10);
8+
} catch (Throwable $e) {
9+
echo $e::class, ': ', $e->getMessage(), PHP_EOL;
10+
}
11+
12+
?>
13+
--EXPECT--
14+
ValueError: array_change_key_case(): Argument #2 ($case) must be either CASE_LOWER or CASE_UPPER

ext/standard/tests/array/array_change_key_case_variation.phpt

Lines changed: 0 additions & 50 deletions
This file was deleted.

ext/standard/tests/array/array_change_key_case_variation4.phpt

Lines changed: 0 additions & 132 deletions
This file was deleted.

0 commit comments

Comments
 (0)